Grpc use clientfactory to register gRPC clients in .NET core


Scenario:

Create a Sign on service to return list of users in out system. Register the gRPC client in the .NET core web app.

Solution:

Add nuget package Grpc.Net.ClientFactory
  1. In startup.cs, ConfigureServices method, register the service as below:
  2. 1
    2
    3
    4
     public void ConfigureServices(IServiceCollection services)
            {
               services.AddGrpcClient<SignOn.SignOnClient>(options => { options.Address = new Uri(ConfigurationManager.AppSetting["gRPC:url"]); });
            }

  3. In controller invoke the client as below:

  4.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
     private readonly SignOn.SignOnClient _signOnClient;
    
            public HomeController(ILogger<HomeController> logger, SignOn.SignOnClient signOnClient)
            {
                _logger = logger;
                _signOnClient = signOnClient;
            }
    
    ..
    ..
    
     using (var call = _signOnClient.GetAllUsers(new UserRequest()))
                {
    ...

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...