asp.net-core - 使用 gRPC-Web 和 protobuf-net 在工作服务中添加新方法时出现异常
问题描述
我收到以下异常:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常呈现组件:“DefaultProxyCache
1' threw an exception. System.TypeInitializationException: The type initializer for 'DefaultProxyCache
1”的类型初始化程序引发异常。---> System.ArgumentException:无效的通用参数参数名称:typeArguments at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.MakeGenericMethod_impl(System.Reflection.RuntimeMethodInfo,System.Type[]) at System.Reflection.RuntimeMethodInfo .MakeGenericMethod(System.Type[] methodInstantiation) <0x342def8 + 0x000d6> in :0 at ProtoBuf.Grpc.Internal.ContractOperation.TryGetClientHelper() [0x0001b] in //src/protobuf-net.Grpc/Internal/ContractOperation.cs: 291 在 ProtoBuf.Grpc.Internal.ProxyEmitter.EmitFactory[TService] (ProtoBuf.Grpc.Configuration.BinderConfiguration binderConfig) [0x00477] 在 //src/protobuf-net.Grpc/Internal/ProxyEmitter.cs:238 在 ProtoBuf.Grpc.Internal.ProxyEmitter.CreateFactory[TService] (ProtoBuf.Grpc.Configuration.BinderConfiguration binderConfig) [0x0006d] in //src/protobuf-net .Grpc/Internal/ProxyEmitter.cs:123 at ProtoBuf.Grpc.Configuration.ClientFactory+DefaultProxyCache`1[TService]..cctor()[0x00000] in //src/protobuf-net.Grpc/Configuration/ClientFactory.cs: 81
我的项目使用 gRPC-Web、Blazor Web 程序集和 protobuf-net
这是我的服务合同:
[ServiceContract(Name = "Services.Customer")]
public interface ICustomerService
{
ValueTask<Customer> CreateCustomer(Customer customerDTO);
ValueTask<CustomerResultSet> GetCustomers();
}
实现是:
public class CustomerService : ICustomerService
{
private readonly CustomerUseCases customerLogic;
public CustomerService(CustomerUseCases customerLogic)
{
this.customerLogic = customerLogic;
}
public async ValueTask<Customer> CreateCustomer(Customer customerDTO)
{
var result = await customerLogic.CreateCustomer(customerDTO);
return customerDTO;
}
public async ValueTask<CustomerResultSet> GetCustomers()
{
CustomerResultSet result = new CustomerResultSet { Customers = await customerLogic.GetCustomer() };
return result;
}
}
至于数据合同:
[DataContract]
public class CustomerResultSet
{
[DataMember(Order = 1)]
public IEnumerable<Customer> Customers { get; set; }
}
和,
[DataContract]
public partial class Customer
{
[DataMember(Order = 1)]
public int CustomerId { get; set; }
[DataMember(Order = 2)]
public string CustomerName { get; set; }
}
在我返回服务中的客户列表之前,但我意识到我需要一个类来对消息进行建模,以便 protobuf-net 能够序列化,这就是 CustomerResultSet 的原因。尽管如此,它仍然无法正常工作。非常感谢任何帮助
解决方案
那是……奇怪。我不能在这里复制它,所以我猜它是 Blazor 特有的。我已经检查了代码在“常规”框架中的作用,至少对我而言,它似乎做了正确的事情 - 使用UnaryValueTaskAsync<Customer, Customer>()
and UnaryValueTaskAsync<Empty, CustomerResultSet>()
,这是我所期望的。我已经改进了该代码路径中的异常处理,至少给我们一个线索它正在尝试做什么,所以我的建议是:
- 更新到
protobuf-net.Grpc
版本 >= 1.0.119(我会在 CI 完成后立即部署它) - 重试,让我知道它现在说的是什么
或者,如果你有一个最小的 repro,包括 blazor 位,比如 GitHub repo,我可以很高兴地去那里看看。
(提示:我尝试同时关注 Stack Overflow 和 GitHub,但 GitHub 可能更适合此类问题 - 我很高兴地说这是一个错误,所以:https ://github.com/protobuf -net/protobuf-net.Grpc/issues )
推荐阅读
- python-3.x - 尝试从 Python3 全局类中获取文件名或目录,然后使用 bash 脚本执行
- flutter - 应用关闭时的 Onesignal 通知
- pandas - fasttext 错误:预测一次处理一行(删除 '\n')
- node.js - 从弹性beantalk上的docker-compose部署查看node.js日志(console.log)
- c# - 无法还原 NuGet 包访问被拒绝
- python - 数据重组问题
- python - Matplotlib.plot_date 自动在我的数据系列中添加日期,为什么以及如何避免?
- angularjs - 比较打字稿中的两个对象并在其中一个对象中进行赋值操作
- python - Python - 在嵌套的 JSON 中获取列表
- javascript - 返回 Promise 或 Observable 的验证器:自定义异步验证器:Angular 8