dotnet-sdk/samples/AspNetCore/GrpcServiceSample
Yongguang Zhu 1253cbdf71
Includes a gRPC sample (#382)
* add GrpcSample, GrpcClient; update DaprClient

* disable TLS for grpc server

* implement deposit and withdraw method for grpcsample

* implement event handler for deposit and withdraw

* edit readme for grpcsample

* improve readme

* fix @amanbha requested changes

* fix or improve readme @vinayada1 reviewed

* use parameter in DaprClient to switch invocation of routing or grpcsample service

* add separate editorconfig file into samples directory

* add license header into grpcsample

* fix TypeConverters  to use internal access modifier; remove useless code; fix Console to logger; fix typo; fix using statement location

* throw a exception when account is not found for BankingService Withdraw invocation

* remove TypeConverters dependence

* use config for rpc-exception argument

* fix comment of @vinayada1

* updated documentation for parameters usage

* fix additional comments from @vinayada1

* Update Readme.md

* fix DaprClient's compile error

Co-authored-by: Yongguang Zhu <Yongguang.Zhu@microsoft.com>
Co-authored-by: vinayada1 <28875764+vinayada1@users.noreply.github.com>
Co-authored-by: Ryan Nowak <nowakra@gmail.com>
2020-12-02 17:52:43 -08:00
..
Models Includes a gRPC sample (#382) 2020-12-02 17:52:43 -08:00
Properties Includes a gRPC sample (#382) 2020-12-02 17:52:43 -08:00
Services Includes a gRPC sample (#382) 2020-12-02 17:52:43 -08:00
GrpcServiceSample.csproj Includes a gRPC sample (#382) 2020-12-02 17:52:43 -08:00
Program.cs Includes a gRPC sample (#382) 2020-12-02 17:52:43 -08:00
Readme.md Includes a gRPC sample (#382) 2020-12-02 17:52:43 -08:00
Startup.cs Includes a gRPC sample (#382) 2020-12-02 17:52:43 -08:00
appsettings.Development.json Includes a gRPC sample (#382) 2020-12-02 17:52:43 -08:00
appsettings.json Includes a gRPC sample (#382) 2020-12-02 17:52:43 -08:00

Readme.md

ASP.NET Core Grpc Service Sample

This sample shows using Dapr with ASP.NET Core Grpc Service. This application is a simple and not-so-secure banking application. The application uses the Dapr state-store for its data storage.

It exposes the following endpoints over GRPC:

  • /getaccount: Get the account information for the account specified by id
  • /deposit: Accepts a Protobuf payload to deposit money to an account
  • /withdraw: Accepts a Protobuf payload to withdraw money from an account

The application also registers for pub/sub with the deposit and withdraw topics.

Running the Sample

To run the sample locally run this command in this project root directory:

dapr run --app-id grpcsample --app-port 5050 --app-protocol grpc -- dotnet run

The application will listen on port 5050 for GRPC.

NOTE: Because of this bug, only can use port 5050 without TLS.

Client Examples

See InvokeGrpcBalanceServiceOperationAsync, InvokeGrpcDepositServiceOperationAsync and InvokeGrpcWithdrawServiceOperationAsync on DaprClient project.

Code Samples

All of the interesting code in this sample is in Startup.cs and Services/BankingService.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddGrpc();

    services.AddDaprClient();
}

AddDaprClient() registers the Dapr integration with grpc service. This also registers the DaprClient service with the dependency injection container. This service can be used to interact with the Dapr state-store.


app.UseEndpoints(endpoints =>
{
    endpoints.MapGrpcService<BankingService>();

    ...
});

MapGrpcService() exposes BankingService grpc service into ASP.NET Core route endpoints.


public class BankingService : AppCallback.AppCallbackBase
{
    ...
}

You need to inherit AppCallback.AppCallbackBase that will be called by the Dapr runtime to invoke method, register for pub/sub topics and register bindings.


public override async Task<InvokeResponse> OnInvoke(InvokeRequest request, ServerCallContext context)
{
    ...
}

You need to implement this overridable method to support Dapr's service invocation.


public override Task<ListTopicSubscriptionsResponse> ListTopicSubscriptions(Empty request, ServerCallContext context)
{
    ...
}

You need to implement this overridable method to support Dapr's pub/sub topic register.


public override async Task<TopicEventResponse> OnTopicEvent(TopicEventRequest request, ServerCallContext context)
{
    ...
}

You need to implement this overridable method to support Dapr's pub/sub topic handle.