首页 > 解决方案 > 使用 gRPC-Web 和 protobuf-net 在工作服务中添加新方法时出现异常

问题描述

我收到以下异常:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常呈现组件:“DefaultProxyCache 1' threw an exception. System.TypeInitializationException: The type initializer for 'DefaultProxyCache1”的类型初始化程序引发异常。---> 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 的原因。尽管如此,它仍然无法正常工作。非常感谢任何帮助

标签: asp.net-coreprotobuf-netgrpc-webblazor-webassemblyprotobuf-net.grpc

解决方案


那是……奇怪。我不能在这里复制它,所以我猜它是 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 )


推荐阅读