ASP.NET Core gRPC JSON transcoding throws InvalidOperationException.

huangapple go评论85阅读模式
英文:

ASP.NET Core gRPC JSON transcoding throws InvalidOperationException

问题

我有一个基本的应用程序,并使用了 在 ASP.NET Core 中使用 gRPC JSON 转码 文章来集成 gRPC JSON 转码。然而,当我尝试运行我的应用程序时,它给了我一个错误消息:

System.InvalidOperationException: 绑定 gRPC 服务时出错 'SomeGrpcService'。
 ---> System.Reflection.TargetInvocationException: 调用目标的异常已被抛出。
 ---> System.InvalidOperationException: 存储的扩展值的类型为 'Google.Api.HttpRule, Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'。这与请求的类型不同 'Google.Api.HttpRule, Google.Api.CommonProtos, Version=2.5.0.0,Culture=neutral, PublicKeyToken=3ec5ea7f18953e47'。
   在 Google.Protobuf.ExtensionSet.Get[TTarget,TValue](ExtensionSet`1& set, Extension`2 extension)
   在 Google.Protobuf.Reflection.MethodOptions.GetExtension[TValue](Extension`2 extension)
   在 Grpc.Shared.ServiceDescriptorHelpers.TryGetHttpRule(MethodDescriptor methodDescriptor, HttpRule& httpRule)
   在 Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingProviderServiceBinder`1.AddMethod[TRequest,TResponse](Method`2 method, UnaryServerMethod`2 handler)
   在 WBD.Bolt.OneTimePassword.gRPC.API.PhoneOtpService.BindService(ServiceBinderBase serviceBinder, PhoneOtpServiceBase serviceImpl) in /.../obj/Debug/net7.0/SomeGrpc.cs:line 187
   在 InvokeStub_PhoneOtpService.BindService(Object, Object, IntPtr*)
   在 System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   在 Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingServiceMethodProvider`1.OnServiceMethodDiscovery(ServiceMethodProviderContext`1 context)
   --- 内部异常堆栈跟踪的结尾 ---
   在 Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingServiceMethodProvider`1.OnServiceMethodDiscovery(ServiceMethodProviderContext`1 context)
   在 Grpc.AspNetCore.Server.Model.Internal.ServiceRouteBuilder`1.Build(IEndpointRouteBuilder endpointRouteBuilder)
   在 Microsoft.AspNetCore.Builder.GrpcEndpointRouteBuilderExtensions.MapGrpcService[TService](IEndpointRouteBuilder builder)
   在 SomeApplication.Program.Main(String[] args) in /.../SomeApplication/Program.cs:line 97

我的设置如下:
我在 *.csproj 文件中有以下行:

<ItemGroup>
      <Protobuf Include="Protos\**\*.proto" ProtoRoot="Protos\" GrpcServices="Both" />
</ItemGroup>

文件夹结构如下:

Protos
  google
    api
      annotations.proto
      http.proto
  Some.proto

我相信主要问题是:

存储的扩展值的类型为 'Google.Api.HttpRule, Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'。这与请求的类型不同 'Google.Api.HttpRule, Google.Api.CommonProtos, Version=2.5.0.0,Culture=neutral, PublicKeyToken=3ec5ea7f18953e47'。

我正在使用 Microsoft.AspNetCore.Grpc.JsonTranscoding 版本 7.0.9。

有人能提供上述问题的解决方案吗?

英文:

I have a basic application and used the gRPC JSON transcoding in ASP.NET Core article to incorporate gRPC JSON transcoding. However, when I try to run my application, it gives me an error message:

System.InvalidOperationException: Error binding gRPC service &#39;SomeGrpcService&#39;.
 ---&gt; System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---&gt; System.InvalidOperationException: The stored extension value has a type of &#39;Google.Api.HttpRule, Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&#39;. This a different from the requested type of &#39;Google.Api.HttpRule, Google.Api.CommonProtos, Version=2.5.0.0,Culture=neutral, PublicKeyToken=3ec5ea7f18953e47&#39;.
   at Google.Protobuf.ExtensionSet.Get[TTarget,TValue](ExtensionSet`1&amp; set, Extension`2 extension)
   at Google.Protobuf.Reflection.MethodOptions.GetExtension[TValue](Extension`2 extension)
   at Grpc.Shared.ServiceDescriptorHelpers.TryGetHttpRule(MethodDescriptor methodDescriptor, HttpRule&amp; httpRule)
   at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingProviderServiceBinder`1.AddMethod[TRequest,TResponse](Method`2 method, UnaryServerMethod`2 handler)
   at WBD.Bolt.OneTimePassword.gRPC.API.PhoneOtpService.BindService(ServiceBinderBase serviceBinder, PhoneOtpServiceBase serviceImpl) in /.../obj/Debug/net7.0/SomeGrpc.cs:line 187
   at InvokeStub_PhoneOtpService.BindService(Object, Object, IntPtr*)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingServiceMethodProvider`1.OnServiceMethodDiscovery(ServiceMethodProviderContext`1 context)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingServiceMethodProvider`1.OnServiceMethodDiscovery(ServiceMethodProviderContext`1 context)
   at Grpc.AspNetCore.Server.Model.Internal.ServiceRouteBuilder`1.Build(IEndpointRouteBuilder endpointRouteBuilder)
   at Microsoft.AspNetCore.Builder.GrpcEndpointRouteBuilderExtensions.MapGrpcService[TService](IEndpointRouteBuilder builder)
   at SomeApplication.Program.Main(String[] args) in /.../SomeApplication/Program.cs:line 97

My setup is the following:
I have the following line in the *.csproj file:

&lt;ItemGroup&gt;
      &lt;Protobuf Include=&quot;Protos\**\*.proto&quot; ProtoRoot=&quot;Protos\&quot; GrpcServices=&quot;Both&quot; /&gt;
&lt;/ItemGroup&gt;

and folder structure is:

Protos
  google
    api
      annotations.proto
      http.proto
  Some.proto

I believe the main issue is:

The stored extension value has a type of &#39;Google.Api.HttpRule, Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&#39;. 
This a different from the requested type of &#39;Google.Api.HttpRule, Google.Api.CommonProtos, Version=2.5.0.0,Culture=neutral, PublicKeyToken=3ec5ea7f18953e47&#39;.

and I am using Microsoft.AspNetCore.Grpc.JsonTranscoding version 7.0.9.

Can someone offer a solution to the problem mentioned above?

答案1

得分: 1

你必须像这样更改你的目录:

  google
    api
      annotations.proto
      http.proto
  Protos
    Some.proto

项目目录

英文:

You must change your directory like this:

  google
    api
      annotations.proto
      http.proto
  Protos
    Some.proto

Project Directory

答案2

得分: 0

以下更改已解决了我的问题:

更新 *.csproj 文件:

<ItemGroup>
    <Protobuf Include="Protos\Some.proto" ProtoRoot="Protos\" GrpcServices="Both" />
</ItemGroup>

并将 Google.Api.CommonProtos 库添加到项目中。

英文:

The following changes are fixed the issue for me:

update *.csproj file:

&lt;ItemGroup&gt;
      &lt;Protobuf Include=&quot;Protos\Some.proto&quot; ProtoRoot=&quot;Protos\&quot; GrpcServices=&quot;Both&quot; /&gt;
&lt;/ItemGroup&gt;

and added the Google.Api.CommonProtos library to the project.

huangapple
  • 本文由 发表于 2023年7月20日 15:27:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定