Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering ASP.NET Web API
Mastering ASP.NET Web API

Mastering ASP.NET Web API: Build powerful HTTP services and make the most of the ASP.NET Core Web API platform

By Mithun Pattankar
€37.99
Book Aug 2017 330 pages 1st Edition
eBook
€28.99
Print
€37.99
Subscription
€14.99 Monthly
eBook
€28.99
Print
€37.99
Subscription
€14.99 Monthly

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Black & white paperback book shipped to your address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now
Table of content icon View table of contents Preview book icon Preview Book

Mastering ASP.NET Web API

Introduction to Microservices and Service-Oriented Architecture

With the increase in internet availability, there is an ongoing evolution in data communication techniques. The architectural improvements have been very innovative, scalable, and adoptable across environments. There was a need for software components to be available across the internet with a common interface for communication across different platforms and programming languages.

This led to the concept of creating services easily deployable with scalability, and exposing them over the internet.

Designing functionalities in terms of service was widely adopted; it was a great idea to provide features in the form of services to heterogeneous clients. This concept of using services led to SOA (Service-Oriented Architecture).

In this chapter, we will be looking at the following topics:

  • Service in SOA
  • Monolithic architecture
  • Introduction to Microservices

Services in SOA

A service is a piece of software which provides a functionality to other pieces of software within your system or outside the system.

The other pieces of software (clients) could be anything from a web application (website) to a mobile app (native or hybrid), or a desktop app, or even another service which uses another service in order to carry out a particular type of functionality.

In an e-commerce website context, when a user places an order, the web application communicates with the service to carry out the create, read, update, and delete (CRUD) operations on the database.

The communication between the software components (clients) and the service normally happens over a network with some kind of a communication protocol, for example, a mobile app communicating to a service via internet.

A system which uses a service or multiple services in this fashion, is known to have a Service-Oriented Architecture.

The main idea behind this architecture is that, instead of using modules within each client application, it lets us use a service(s) to provide functionality to them. This allows us to have many client applications using the same functionality.

SOA was successful, because of its following characteristics:

  • It allows us to scale our software when the demand increases by enabling it to have a copy of the service on multiple servers, so when the traffic comes in, a load balancer redirects that request to a specific instance of the service, and we can have multiple instances of the service. Thus, when the demand increases, increasing the number of instances on the servers helps us scale it.
  • SOA boasts of having standardized contracts or interfaces. When a client application calls the service, it calls the service by calling a method. The signature of that method normally doesn't change when the service changes, so we can upgrade our service without having to upgrade our clients as long as the contract and the interface do not change.
  • Services are, in fact, stateless, so when a request comes in from a website to our service, that instance of the service does not have to remember the previous request from that specific customer. It, basically, has all the information from the request that it needs in order to retrieve all the data associated with the previous requests within the service, so, the service does not have to remember the previous calls a client has made to that particular instance of the service.

Service implementation

SOA gained popularity due to its implementation of services, which are accessible over standard internet protocols that are independent of OS platforms and programming languages.

Services from a developer POV are nothing but web services hosted on a web server, and which use SOAP (Simple Object Access Protocol) or JSON for communication. It's interesting to know that a web service can be used as a wrapper for legacy systems for making them network-enabled.

Some of the popular technologies implementing services (SOA) are as follows:

  • Web services based on WSDL (Web Service Description Language) and SOAP
  • Messaging, for example, with ActiveMQ, JMS, and RabbitMQ
  • WCF (Microsoft's implementation of Web services)
  • Apache Thrift
  • SORCER
  • RESTful HTTP

Service-Oriented Architecture started gaining momentum when the Monolithic architectural approach experience proved to be more painful than thought earlier. Let's briefly understand what Monolithic systems are and their drawbacks that led to adoption of SOA.

Monolithic architecture

Monolithic architecture-based systems existed before the SOA or Microservices movement. These types of systems are exactly the opposite of what SOA tries to achieve.

A typical Monolithic system is an enterprise-based application, and this application might be in the form of a large website with all the working modules packaged in together into one single package, or it might be in the form of a service which talks to a website. It might be packaged as a large executable that is deployed on a machine.

In these systems, we added different components to an application to keep growing; there's no restriction in size, and there's no division. There's always one package which contains everything, and therefore, we end up with a large code base.

The high-level architecture diagram of a Monolithic system would look as follows:

Typical Monolithic architecture

Overheads of Monolithic architecture

In the long run, enterprises faced these shortcomings when they applied Monolithic architecture to their systems:

  • Due to the code base being so large, it took the teams longer to develop a new functionality within the application.
  • Deployment of a large system can also be challenging, because even for a small bug fix, we have to deploy a new version of the entire system, and therefore, that creates greater risk.
  • It's one large code base, so, we're also stuck with one technology stack.
  • It makes the overall system less competitive, because we can't easily adopt new technologies which might give us a competitive edge.
  • Since the code is in one large package, we might also have high levels of coupling, which means that if a change is made in one part of the system, it might affect another part of the system, because the code is intertwined. This kind of coupling might be present between modules, and also between different services.
  • Scaling up this service to meet the demand is quite inefficient. For example, if the Orders module of the system is in demand, we would have to create a copy of the whole package, of the whole service, in order to scale up just the Orders section.
  • More powerful servers need to be bought to work efficiently for a large footprint of monolithic apps.
  • Unit testing for such a large code base takes time, and regression testing by QA is also a time-consuming process.
The only one advantage that a Monolithic system has is the fact that we can run the entire code base on one machine, so, when developing and testing, we could probably replicate the entire environment on a machine.

An example of a Monolithic system could be an ASP.NET MVC site where the website itself is the UI layer, and then in the Business layer, you have business logic along with the data access layer. Over the years, if we continue with the same approach, then it will become a Monolithic system.

Introducing Microservices

The Microservices architecture is, basically, service-oriented architecture done well. After years of working with Service-Oriented Architecture, software developers have realized what Service-Oriented Architecture should be like, and this is basically what Microservices architecture is--it's an evolution of the Service-Oriented Architecture.

Microservices are small, autonomous services that perform one function well while working with other services as well.

Microservices introduces a new set of additional design principles, which teach us how to size a service correctly. Previously, there was no guidance on how to size a service, and what to include in a service. The traditional Service-Oriented Architecture resulted in monolithic large services, and because of the size of the service, these services became inefficient to scale up.

Let's look into the advantages of using Microservices.

Lightweight yet scalable

Microservices provide services which are more efficiently scalable, flexible, and which can provide high performance in the areas where performance is required.

An application which is based on the Microservices architecture is, normally, an application which is powered by multiple Microservices, and each one of these provide a set of functions, or a set of related functions, to a specific part of the application. A Microservices architecture normally provides a set of related functions to applications, to client applications, and client services.

Microservices architecture also uses a lightweight communication mechanism between clients and services or between two or more services. The communication mechanism has to be lightweight and quick, because when a Microservices-architected system carries out a transaction, it is a distributed transaction which is completed by multiple services. Therefore, the services need to communicate to each other in a quick and efficient way over the network.

Technology agnostic

The application interface for a Microservice, or the way we communicate to a Microservice, also needs to be technology agnostic. It means the service needs to use an open communication protocol so that it does not dictate the technology that the client application needs to use. And by using open communication protocols, for example, like HTTP REST (JSON based), we could easily have a .NET client application which talks to a Java-based Microservice.

Independently changeable

Another key characteristic of a Microservice is that it is independently changeable. We can upgrade, enhance, or fix a specific Microservice without changing any of the clients or any of the other services within the system.

In the Microservices architecture, each microservice has its own data storage. By modifying one Microservice, we should then be able to deploy that change within the system independently without deploying anything else.

Sample Microservices architecture app

The preceding image depicts a high-level architecture diagram for a Microservices system. This is an example of a typical e-commerce system, and as you can see on the left-hand side, there's a shopping website running in the customer's browser, or it could be a mobile app using the API gateway.

The browser connects to the demo shopping website via the internet--the demo shopping website might be an ASP.NET MVC website running on IIS. All the processing required for all the interactions with the website is actually carried out by a number of Microservices which are running in the background.

Each Microservice has a single focus, or a single set of related functions, has its own data storage, and it's also independently changeable and deployable. So, for example, we could upgrade the Orders service without upgrading any other part of this system.

There might also be multiple instances for each type of Microservice. For example, if the Orders service is in demand, we might have several instances of the Orders service in order to satisfy the demand. And in order to direct a request from the shopping website to the correct instance of an order service, we have an API Gateway which manages and routes a request to the correct Microservice within the system.

So, in this example, when a customer places an order, the shopping website might use multiple services and multiple functions within those services in order to satisfy that transaction. And this is why, in the Microservices architecture, a transaction is normally a distributed transaction, because the transaction is actually satisfied by multiple pieces of software, that is, Microservices.

Benefits of Microservices

The following are the benefits of Microservices:

  • Microservices architecture satisfies the need to respond to change quickly. The software market is really competitive nowadays. If your product can't provide a feature that's in demand, it will lose its market share very quickly.
  • It fulfills the need for a business-domain-driven design. The architecture of an application needs to match the organization structure, or the structure of the business functions within the organization.
  • The Microservices architecture makes use of automated test tools. We've already seen that in a Microservices architecture, transactions are distributed, and therefore, a transaction will be processed by several services before it's complete. The integration between those services needs to be tested, and testing these Microservices together manually might be quite a complex task. Automated test tools help us to perform this integration testing, reducing the manual burden.
  • Cloud-compliant Microservices can reduce the burden of deployment and release management.
  • The Microservices architecture provides a platform to adopt new technology. Because the systems are made of several moving parts, we can easily change one part, that is, a Microservice from one technology stack to another technology stack in order to get a competitive edge.
  • By using asynchronous communication, the distributed transaction does not have to wait for individual services to complete their tasks before it's complete.
  • Microservices have shorter development times. Because the system is split up into smaller moving parts, we can work on a moving part individually, can have teams working on different parts concurrently, and because Microservices are small in size and they have a single focus, the teams have less to worry about in terms of scope.
  • The Microservices architecture also offers us increased uptime, because when it comes to upgrading the system, we will probably deploy one Microservice at a time without affecting the rest of the system.
Netflix adopted the Microservices architecture; the lessons learnt on architectural designs are summarized in this link along with a video: https://www.nginx.com/blog/microservices-at-netflix-architectural-best-practices/.

Summary

The evolution of building services has seen many changes in the past decade with improvements in the internet bandwidth, machine processing power, better frameworks, and so on.

From a developer's point of view, Microservices are REST-based Web APIs either using ASP.NET, Java, PHP, or others. In the upcoming chapters, we will learn the various aspects of developing an ASP.NET Core-based Web API application.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Get a comprehensive analysis of the latest specification of ASP.NET Core and all the changes to the underlying platform that you need to know to make the most of the web API
  • See an advanced coverage of ASP.NET Core Web API to create robust models for your data, create controllers, and handle routing and security
  • This book is packed with key theoretical and practical concepts that can be instantly applied to build professional applications using API with Angular 4, Ionic, and React

Description

Microsoft has unified their main web development platforms. This unification will help develop web applications using various pieces of the ASP.NET platform that can be deployed on both Windows and LINUX. With ASP.NET Core (Web API), it will become easier than ever to build secure HTTP services that can be used from any client. Mastering ASP.NET Web API starts with the building blocks of the ASP.NET Core, then gradually moves on to implementing various HTTP routing strategies in the Web API. We then focus on the key components of building applications that employ the Web API, such as Kestrel, Middleware, Filters, Logging, Security, and Entity Framework.Readers will be introduced to take the TDD approach to write test cases along with the new Visual Studio 2017 live unit testing feature. They will also be introduced to integrate with the database using ORMs. Finally, we explore how the Web API can be consumed in a browser as well as by mobile applications by utilizing Angular 4, Ionic and ReactJS. By the end of this book, you will be able to apply best practices to develop complex Web API, consume them in frontend applications and deploy these applications to a modern hosting infrastructure.

What you will learn

[*] Acquire conceptual and hands-on knowledge of ASP.NET Core (MVC & Web API) [*] Learn about HTTP methods, the structure of HTTP content, internet media types, and how servers respond to HTTP requests and their associated HTTP codes [*] Explore middleware, filters, routing, and unit testing [*] Optimize Web API implementations [*] Develop a secure Web API interface [*] Deploy Web API projects to various platforms [*] Consume your web API in front end application based on Angular 4, Bootstrap, and Ionic [*] Implement and explore the current trends in service architecture
Estimated delivery fee Deliver to Malta

Premium delivery 7 - 10 business days

€39.95
(Includes tracking information)

Product Details

Country selected

Publication date : Aug 11, 2017
Length 330 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781786463951
Vendor :
Microsoft
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Black & white paperback book shipped to your address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now
Estimated delivery fee Deliver to Malta

Premium delivery 7 - 10 business days

€39.95
(Includes tracking information)

Product Details


Publication date : Aug 11, 2017
Length 330 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781786463951
Vendor :
Microsoft
Category :
Languages :

Table of Contents

14 Chapters
Preface Chevron down icon Chevron up icon
1. Introduction to Microservices and Service-Oriented Architecture Chevron down icon Chevron up icon
2. Understanding HTTP and REST Chevron down icon Chevron up icon
3. Anatomy of ASP.NET Core Web API Chevron down icon Chevron up icon
4. Controllers, Actions, and Models Chevron down icon Chevron up icon
5. Implementing Routing Chevron down icon Chevron up icon
6. Middleware and Filters Chevron down icon Chevron up icon
7. Perform Unit and Integration Testing Chevron down icon Chevron up icon
8. Web API Security Chevron down icon Chevron up icon
9. Integration with Database Chevron down icon Chevron up icon
10. Error Handling, Tracing, and Logging Chevron down icon Chevron up icon
11. Optimization and Performance Chevron down icon Chevron up icon
12. Hosting and Deployment Chevron down icon Chevron up icon
13. Modern Web Frontends Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Top Reviews
No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela