1

我尝试将自定义 protobuf-net RuntimeTypeModel 与 protobuf-net grpc 客户端库一起使用。到目前为止我所了解的,我需要使用 ClientFactory 并设置一个引用我的 RuntimeTypeModel-Instance 的 binder-configuration。

var binderConfig = BinderConfiguration.Create(new List<MarshallerFactory> {
    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);
   };
});

我可以在我的控制器类中使用 IMyGrpcService 并获得一个有效的实例。但是永远不会调用 o.Creator 委托,并且使用了错误的 runtimeTypeModel。

这种方法有什么问题?

谢谢。托尼

4

1 回答 1

1

您需要向ClientFactoryDI 层注册:

services.AddSingleton<ClientFactory>(clientFactory)

现在它应该正确地发现它。您不需要设置Creator.

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

于 2020-09-22T09:53:49.467 回答