【问题标题】:Use protobuf-net RuntimeTypeModel with GrpcClient (AddCodeFirstGrpcClient)将 protobuf-net RuntimeTypeModel 与 GrpcClient (AddCodeFirstGrpcClient) 一起使用
【发布时间】:2020-09-22 09:41:15
【问题描述】:

我尝试将自定义 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。

这种方法有什么问题?

谢谢。 托尼

【问题讨论】:

    标签: c# .net-core dependency-injection protobuf-net protobuf-net.grpc


    【解决方案1】:

    你需要将ClientFactory注册到DI层:

    services.AddSingleton<ClientFactory>(clientFactory)
    

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

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

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多