This project was created to demonstrate advanced implementation of .NET Core gRPC services including unit tests and integration tests
It is created as a set of microsevices communicating via gRPC.
The entrypoint is Grpc.Dotnet.Todos.Api
REST API with a single TodoController
.
This project is using MediatR, AutoMapper, EntityFramework Core InMemory
, custom MessageOrchestrator
and custom IServiceClient
wrapper.
It also shows how to create unit tests, integration test and mock gRPC clients.
There is also a custom configuration for gRPC ports that *.Server
projects are starting on: in the appsettings.json
, there is RpcServer:Port
value, which is used inside the Program.cs
by extensions method webBuilder.ConfigureGrpcServer()
MediatR for bussiness logic and calling external gRPC services.
AutoMapper for auto-mapping gRPC request/response classes to MediatR commands/queries and DTO results. It is also used for .ProjectTo() in EntityFramework Core InMemory
The MessageOrchestrator
is used in the implementation of gRPC services, e.g. PermissionsServiceV1
and is responsible for mapping (AutoMapper) incomming gRPC request into MediatR query/command and invoking corresponding handler. This is a convenient way of automated calling MediatR handlers based on AutoMapper configuration.
There is a custom implementation for gRPC clients which are wrapped in IServiceClient
to easly add them to project and then mock them in unit tests or integration tests.
To add a gRPC client, e.g. XMyGrpcService
to project:
- add reference to
XMyGrpcService.RpcApi
project. - in the
Startup.ConfigureServices()
addservices.AddRpcClients(this.Configuration).AddClient<XMyGrpcService.XMyGrpcServiceClient>()
- in the
appsettings.json
add section:
"RpcClients": {
"XMyGrpcServiceClient": {
"Host": "127.0.0.1",
"Port": "44444"
}
}
The XMyGrpcServiceClient
configuration name must be the same as your generated gRPC client name class from XMyGrpcService.RpcApi
project.
- HTTP request goes to
Todos.Api
- The request is mapped to MediatR query or command
- The query or command is then processed by a MeditaR handler
- The handler checks permissions by calling
Grpc.Dotnet.Permissions.Server
gRPC server. - The handler optionally send a request to gRPC server
Grpc.Dotnet.Notifications.Server
cd <this repo directory>
dotnet run --project .\src\Notifications\Grpc.Dotnet.Notifications.Server\Grpc.Dotnet.Notifications.Server.csproj
dotnet run --project .\src\Permissions\Grpc.Dotnet.Permissions.Server\Grpc.Dotnet.Permissions.Server.csproj
dotnet run --project .\src\Todos\Grpc.Dotnet.Todos.Api\Grpc.Dotnet.Todos.Api.csproj
dotnet run --project .\src\Todos\Grpc.Dotnet.Todos.Server\Grpc.Dotnet.Todos.Server.csproj
- Install recommended VSCode extension REST Client:
humao.rest-client
and open<this repo directory>\test-api.http
to make some test requests. - Or use Postman and create requests to http://localhost:5000/api/todos/[...]
Recommended and easy to use tool is BloomRPC