dotnet-sdk icon indicating copy to clipboard operation
dotnet-sdk copied to clipboard

Include Samples for Grpc Proxy

Open Sen-Gupta opened this issue 3 years ago • 3 comments

Include Samples for Experimental Grpc Proxy

The sample contains examples of invoking grpc and uses InvokeGrpcAsync

Will it be possible to include sample based on Grpc Proxy as shown here.

https://docs.dapr.io/developing-applications/building-blocks/service-invocation/howto-invoke-services-grpc/

Sen-Gupta avatar Jan 29 '22 12:01 Sen-Gupta

I'd also like to know how to properly do this with dependency injection in ASP.NET Core.

I'm currently using the following code:

// invoker should be singleton according to docs
var otherServiceInvoker = DaprClient.CreateInvocationInvoker("other-service"); 

builder.Services.AddTransient(_ => new OtherService.ServiceClient(otherServiceInvoker));

I'm wondering though if I should/could use .AddGrpcClient() from Grpc.Net.ClientFactory? I somehow couldn't make it work with that yet.

cwe1ss avatar Sep 08 '22 12:09 cwe1ss

I use DaprClient.CreateInvocationInvoker with protobuf-net.Grpc for a code-first approach.

I could not find a way to use the protobuf-net.Grpc.ClientFactory. The documentation of DaprClient.CreateInvocationInvoker suggests the returned CallInvoker can be registered as a singleton. And the protobuf-net.Grpc client instance is a thin veneer over the CallInvoker. See https://github.com/protobuf-net/protobuf-net.Grpc/issues/172. So I have

builder.Services.AddSingleton(_ => DaprClient.CreateInvocationInvoker("greeter-service").CreateGrpcService<IGreeterService>());

RemcoBlok avatar Dec 30 '22 14:12 RemcoBlok

For anybody who'd like to see a working example (based on code in the above post), here's a quick way to add a client with the ASP.NET 6 DI container, assuming you have daprPort defined for the correct gRPC port and daprToken for token authentication. Not sure if this is the "right" or "best" way, but it works:

services.AddGrpcClient<OtherServiceClient>(
    options =>
    {
        options.Address = new Uri( $"http://localhost:{daprPort}" );
        options.Creator = _ => new OtherServiceClient( DaprClient.CreateInvocationInvoker( "other-service", null, daprToken ) );
    } );

Now... if anybody could show me how to set channel options on this, my day would be made!

Flern avatar Feb 16 '23 17:02 Flern