Formalizing API Workflow in .NET Microservices
Published: 2024-05-24 10:27:05

We work with IT products in the fields of logistics and e-commerce. Most of these projects are architecturally large, including many services essential for the proper operation of entire systems.

Let's talk about how to organize the interaction of microservices in a large, long-lived product, both synchronously and asynchronously.

The microservice approach involves creating a microservice for each feature within a large product. For example, a microservice that handles a specific function in logistics processes:

- warehousing (collection/placement/movement of commodity units)
- sorting of cargo spaces
- labeling of cargo spaces
- consolidation of cargo spaces
- other

Each microservice has its codebase, database, and API for interacting with other services. This allows us to write them in different programming languages and use different technologies. 
All new microservices are written on new versions of frameworks, while all outdated ones are gradually migrated. The goal is to provide the most efficient and standardized approach to ensuring interoperability between microservices.  Creating a new microservice and integrating it into the overall system should be as quick and painless as possible for both its developer and the developers who use this microservice.

## Synchronous interaction between microservices

Synchronous interaction occurs when one system sends a message to another and waits for an acknowledgment or response before continuing. This type of interaction is common when the requesting system needs information to proceed with its actions. To organize such interaction, various protocols like (g)RPC, SOAP, and the architectural style REST are widely used.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arsti40n0pzc4hh8n7jd.jpg)

The diagram shows the approach to developing an API for a new microservice. First, we create an API specification in OpenAPI format and get it approved by the architecture department. Based on this specification, we create a contract library containing API interfaces and data structures. Then, we use this library to create a client's API that will interact with this API. Usually, we use the Refit library for this purpose.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gq6qduh48o9srjm5cjpp.jpg)

This is what the OpenAPI specification looks like. IDE Rider has a plugin that allows you to edit it, and Swagger generates the specification description. All methods of this API, along with request and response structures, are described here. Once this specification is approved, we begin developing the contract library.

## Sync contract library

The Sync contracts library is a NuGet package based on the specification. It contains:

- Interfaces of each API controller
- The client interface, which combines all controller interfaces
- Request/Response models used in controllers

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t40ie2p2dv2ptaodpe8d.jpg)

For example, in one of the libraries we have two interfaces: ITaskController and IConsolidationCargoUnitsController. They define all the necessary methods, which will later be implemented by both the corresponding controllers and clients. 

Since we use the Refit library to generate clients, we also define the types of requests and their routes using attributes like [Get(...)], [Post(...)], etc. It's important to note that these are Refit attributes, not ASP.NET attributes. Accordingly, our contract library may not depend on ASP.NET at all.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sotb6ci9z9j64hrh41ql.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2aefc22ric7l35odbwg.jpg)

Next, we describe the client interface, which does not contain its own methods but simply inherits all controller interfaces and is marked it with a special attribute that implements versioning. The implementation of this interface (using the Refit library) will be the client that external systems use to interact with it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfbp7d9oedn6qlgqvx5n.jpg)

This is what the library of contracts ultimately looks like: one client interface, several controller interfaces, and data models. This is all completely based on the specification. 

Once the development of the contract library is complete, we publish a NuGet package. Once the package is published, we can start implementing the API itself, as well as the client for this API. Since the specification has already been approved and the contract library has been published, API and client development can be done in parallel.

## API development

This is what the API controller class looks like, where we implement the controller interface with the necessary business logic. It’s worth noting that here ASP.NET attributes are used, which is one of the disadvantages of this approach - you have to duplicate routes both in the contract library and in the controllers themselves. 

For simple cases, where the routes have no restrictions (for example, {id}/{sortingCenterId}), they can be made into constants and reused. But when restrictions are included in the routes (for example, {id:int}/{sortingCenterId}), such routes have to be duplicated, as the semantics inherent in ASP.NET are not supported by Refit (and vice versa).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzm06ikugefhybjtikoh.jpg)

## Client development

The client for the API implements using the Refit library. We wrote the following extension method to register an API client. We pass the client interface from the contract library, along with configuration arguments, to the method below. As a result, a dynamically generated Refit class containing HTTP client calls is registered in the DI container for this interface.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dd4e770rcweqib2xd8l5.jpg)

Accordingly, all we need to do is register the client interface from the contract library in one line. Next, we implement this interface using DI into the necessary classes and address external services. Also, this approach makes it easy to test code that has dependencies on the client API.

## Asynchronous interaction between microservices

Asynchronous communication occurs when one system sends a message to another and continues its work without waiting for an acknowledgment or response. The response can be received later through messages or callback functions. This type of interaction is common when the requesting system does not require information to continue its actions.

In our case, asynchronous communication of microservices is implemented through Kafka (message broker). We are writing the Async API specification, which is a similar standard to OpenAPI but is used for describing an asynchronous interaction protocol.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11k2pijjqb9cfmcbogws.jpg)

The principle is similar to synchronous one. We describe the specification, approve it, create a library of asynchronous contracts, and then write producers that will publish messages to the queue and consumers that will read and process them.

The specification is slightly different—  we describe not methods, but message types and channels. In each channel, we describe the types of messages that are sent to the corresponding channels.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/462rn3wqsaiwcmcalw1x.jpg)

## Async contract library

The Async contracts library is a NuGet package based on the specification. It contains message models that are sent to or read from the queue.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gewb774vpzvmcplfch5q.jpg)

In our case, all messages are events. An event is a wrapper for a data object with the necessary parameters defined within the event. 

For example, the “Delivery created” event, in addition to information about the event itself (identifier, type, date, and time), will contain information about the delivery. The process looks like this: we created a library of contracts, added all the events provided for in the specification, and created a producer that will generate events.

The screenshot shows a piece of business logic code that completes the execution of the task. Starting from line 5, a transaction is opened, within which data is written to the microservice database. The next three lines are responsible for sending the message to the queue.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu13ck2b0brfcrez1ns5.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9zkg95oyxiuo2ev8uqg.jpg)

The screenshot above shows a consumer. This is the handler to which all messages arrive. It includes filtering and usually processes the business logic that should be triggered when a message is received.

When a new microservice appears, the approach has already been worked out, and all the processes for creating a library of contracts and working with Kafka are debugged. As a result, everything is efficient and there are no discrepancies in development.

Of course, the methods of organizing the interaction of microservices described in this article are not the only possible ones. For instance, gRPC is another powerful alternative worth exploring. We might talk about that in a future article, using an example from one of our other projects. Until then, happy coding!

Author: Artyom Chernenko