AsyncAPI is an open source initiative with as goal to document event-driven architectures in a similar that OpenAPI documents REST APIs. If you want to get a good introduction chec k out my previous posts about AsyncAPI:
- AsyncApi–Share your message contracts in a language agnostic manner - Part 1
- AsyncApi–Share your message contracts in a language agnostic manner - Part 2
Today I want to talk about that allows you to integrate an AsyncAPI more easily into your ASP.NET Core application; Neuroglia AsyncAPI.
Let me walk you through a small example I created.
- First add the following nuget package to your ASP.NET Core project:
- Neuroglia.AsyncApi.AspNetCore.UI
- Now we can register the necessary configuration:
- Notice in the code above that I’m referring to an
OrdersController
class. This ASP.NET core controller publishes an event to create a new order: - The library scans the provided class to find specific attributes to build up the AsyncAPI schema. We also need to register the necessary middleware:
- If we now run the application and browse to the asyncapi endpoint, you can see the AsyncAPI information:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
builder.Services.AddAsyncApiUI(); | |
builder.Services.AddAsyncApiGeneration(builder => | |
builder.WithMarkupType<OrdersController>() | |
.UseDefaultConfiguration(asyncApi => | |
{ | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AsyncApi("Order API", "1.0.0")] | |
[Route("api/[controller]")] | |
[ApiController] | |
public class OrdersController : ControllerBase | |
{ | |
private readonly IPublishEndpoint _publishEndpoint; | |
public OrdersController(IPublishEndpoint publishEndpoint) | |
{ | |
_publishEndpoint = publishEndpoint; | |
} | |
// POST api/createorder | |
[Channel("order/create"), PublishOperation(OperationId = "PlaceOrder", Summary = "Place a new order")] | |
[HttpPost] | |
[ProducesResponseType(StatusCodes.Status200OK)] | |
[ProducesResponseType(StatusCodes.Status400BadRequest)] | |
public async Task<ActionResult> CreateOrder(OrderProductCommand command) | |
{ | |
await _publishEndpoint.Publish(new PlaceOrderMessage { OrderId = command.ProductId }); | |
return Ok(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Adds the middleware used to serve AsyncAPI documents | |
app.UseAsyncApiGeneration(); |
Remark: Be aware that to make this work you need to enable RazorPages (which is not mentioned in the documentation):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Required for AsyncAPI | |
builder.Services.AddRazorPages(); |