protobuf-net / protobuf-net.Grpc

GRPC bindings for protobuf-net and grpc-dotnet
Other
846 stars 106 forks source link

Access the underlying GrpcChannel instance when it is instantiated with the factory pattern #286

Open bdovaz opened 1 year ago

bdovaz commented 1 year ago

@mgravell if I do it manually I have no problem as in this example: https://protobuf-net.github.io/protobuf-net.Grpc/gettingstarted#2-implement-the-client

But on the other hand, if I do it with the factory I lose access to this instance? There is no way to get some kind of reference?

services.AddCodeFirstGrpcClient<IMyService>()

I am using gRPC with Unity and because of the nature of Unity, I need to manually dispose the channel instance when exiting play mode.

gRPC executes code in other threads and this generates problems in Unity when exiting play mode.

bdovaz commented 1 year ago

At the moment I have solved it with reflection, I don't like it but unless there is a public way to access it, I have no choice....

# Get the instance
IMyService myService = null;

object interceptingCallInvoker = myService.GetType()
    .GetProperty("CallInvoker", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(myService);

object httpClientCallInvoker = interceptingCallInvoker.GetType()
    .GetField("invoker", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(interceptingCallInvoker);

GrpcChannel grpcChannel = httpClientCallInvoker.GetType()
    .GetProperty("Channel", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(httpClientCallInvoker) as GrpcChannel;

grpcChannel.Dispose();
mgravell commented 1 year ago

Ok. I propose I add a new interface, IGrpcClient or IGrpcClientProxy or something (naming is hard) that exposes the underlying channel etc; all our clients will then implement that - sound workable?

menaheme commented 1 year ago

IGrpcClientProxy sounds redundant, no? Client or Proxy, but not both

bdovaz commented 1 year ago

@mgravell sounds great! Thanks!

mgravell commented 1 year ago

@menaheme "naming is hard" :)

menaheme commented 1 year ago

That and the halting problem