【发布时间】: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