C# 将protobuf net RuntimeTypeModel与GRPClient(AddCodeFirstGRPClient)一起使用

C# 将protobuf net RuntimeTypeModel与GRPClient(AddCodeFirstGRPClient)一起使用,c#,.net-core,dependency-injection,protobuf-net,protobuf-net.grpc,C#,.net Core,Dependency Injection,Protobuf Net,Protobuf Net.grpc,我尝试在protobuf net grpc客户端库中使用自定义protobuf net RuntimeTypeModel。 据我目前所知,我需要使用ClientFactory并设置一个引用RuntimeTypeModel实例的绑定器配置 var binderConfig = BinderConfiguration.Create(new List<MarshallerFactory> { ProtoBufMarshallerFactory.Create(_runtimeType

我尝试在protobuf net grpc客户端库中使用自定义protobuf net RuntimeTypeModel。 据我目前所知,我需要使用ClientFactory并设置一个引用RuntimeTypeModel实例的绑定器配置

var binderConfig = BinderConfiguration.Create(new List<MarshallerFactory> {
    ProtoBufMarshallerFactory.Create(_runtimeTypeModel)
}); 
var clientFactory = ClientFactory.Create(binderConfig)
var binderConfig=BinderConfiguration.Create(新建列表{
ProtoBufMarshallerFactory.Create(_runtimeTypeModel)
}); 
var clientFactory=clientFactory.Create(binderConfig)
如果每次我自己创建客户机和底层grpc通道,这是可行的

现在,我想通过nuget包protobuf net.Grpc.ClientFactory提供的DI使用GrpcClientFactory

我不知道如何配置ClientFactory以使用我的自定义RuntimeTypeModel。在我的创业过程中,我尝试了以下方法:

.AddCodeFirstGrpcClient<IMyGrpcService>(o =>
{
   o.Address = new Uri("https://localhost:5001");
   o.Creator = (callInvoker) =>
   {
       var servicesProvider = services.BuildServiceProvider(); 
       var binderConfig = BinderConfiguration.Create(new List<MarshallerFactory> {
            ProtoBufMarshallerFactory.Create(servicesProvider.GetService<RuntimeTypeModel>())
        });

       var clientFactory = ClientFactory.Create(binderConfig);
       return clientFactory.CreateClient<IMyGrpcService>(callInvoker);
   };
});
.AddCodeFirstGrpcClient(o=>
{
o、 地址=新Uri(“https://localhost:5001");
o、 创建者=(callInvoker)=>
{
var servicesProvider=services.BuildServiceProvider();
var binderConfig=BinderConfiguration.Create(新建列表{
ProtoBufMarshallerFactory.Create(servicesProvider.GetService())
});
var clientFactory=clientFactory.Create(binderConfig);
返回clientFactory.CreateClient(callInvoker);
};
});
我可以在我的控制器类中使用IMyGrpcService并获得一个有效实例。但是从未调用o.Creator委托,并且使用了错误的runtimeTypeModel

这种方法有什么问题

谢谢。
Toni

您需要向DI层注册
ClientFactory

services.AddSingleton(客户端工厂)
现在它应该正确地发现它。您不需要设置
创建者

(注意:它并不特别需要是一个单例,但是:应该可以)