Getting started with Asp.Net microservice

Microservice is not a new word anymore, it has been there here for a while, but developers still wonder if it is suitable architecture to implement for their application. This article discusses the need, suitable situations to use it along with merits/demerits and ways of communication.

What is a microservice architecture and why do we need it? It is a way of developing software using small independent, loosely coupled services where each service performs a single function. Monolithic architecture has been serving developers for a very long period because of its simplicity. In the early days of the application, it would be easy to develop, test and deploy. But, as the application size get bigger, it becomes harder to make changes, deploying the application needs involvement all the teams and needs the entire application testing. Where these drawbacks can be overcome by microservice architecture.

Figure 1: Monolithic service vs Microservice
Figure 2: Monolithic service development to deployment flow

What are the advantages and disadvantages? Microservices can bring advantages like independent development, deployment and it is easier to find an issue as compared to a bulk monolith service. Implementing microservice architecture require additional configuration for calls in between them and there is a greater chance of having redundant functionality either due to lack of communication within teams or to avoid internal service calls.

When to implement microservice architecture?

  • When you want to deploy your new features independently and quickly. As microservices have a specific business requirement, developing, testing and deploying new features need lesser effort and time.
  • When your monolith service can be split into independent functionality services. Converting a monolith service into multiple microservices can be complicated when there are lot of dependencies. So, to proceed further making sure that developers will not encounter any of those is a good idea.
  • When you want to adapt continuous integration, delivery and deployment (CI/CD). As microservices are independent components it can achieve CI/CD with less hassles.
Figure 3: Microservice development to deployment flow

What are the ways of communicating a microservice? One way is direct client to service communication, here microservice’s public endpoint is consumed directly by the client for communication. The limitation here would be it is only good for small applications, if the application contains multiple microservices it would increase the number of round trips between client and service which may result in decreased performance as well as complex application. This limitation of direct client to service communication can be overcome by API gateway pattern. This not only reduce the round trips but also add the benefits like having separate API gateways for different type of clients and take the responsibility of which microservice to be invoked. But nothing comes without downside, this implementation incorporate complexity by involving extra development and deployment.

Figure 4: Direct Communication vs API gateway pattern

How to implement microservice and invoke using direct client to service communication? Below are the steps to create a microservice and invoke it from a web application (client).

Step 1: Create an ASP.NET web application

Figure 5: Selection window with web project frameworks

Step 2: Select Web API as template

Figure 6: Selection window for web project templates

Step 3: Once the application is created, add a new API controller with an action method (ref below). Then start the service.


Step 4: Once the service is up, Invoke the service from the client application as below.


Result: Below is the screenshot of client application user interface. Here the header and footer are retrieved from content microservice.

Figure 7: Client user interface

The above implementation has followed direct client to service communication where the client directly invokes the service which is of many ways. Since, direct communication involves too many service calls for applications with larger microservices, it may have performance issues. API gateway is one of the choices available for applications involving multiple microservices. There are lot of architectural practices followed by developers around, not all of them might be suitable for microservice implementation. But careful study of benefits and challenges give us a good idea on planning to proceed further.




Building Microservices by Sam Newman



About The Author

Leave a Reply