ASP.NET Core gRPC JSON transcoding throws InvalidOperationException.

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

ASP.NET Core gRPC JSON transcoding throws InvalidOperationException

问题

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

  1. System.InvalidOperationException: 绑定 gRPC 服务时出错 'SomeGrpcService'
  2. ---> System.Reflection.TargetInvocationException: 调用目标的异常已被抛出。
  3. ---> 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'
  4. Google.Protobuf.ExtensionSet.Get[TTarget,TValue](ExtensionSet`1& set, Extension`2 extension)
  5. Google.Protobuf.Reflection.MethodOptions.GetExtension[TValue](Extension`2 extension)
  6. 在 Grpc.Shared.ServiceDescriptorHelpers.TryGetHttpRule(MethodDescriptor methodDescriptor, HttpRule& httpRule)
  7. 在 Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingProviderServiceBinder`1.AddMethod[TRequest,TResponse](Method`2 method, UnaryServerMethod`2 handler)
  8. WBD.Bolt.OneTimePassword.gRPC.API.PhoneOtpService.BindService(ServiceBinderBase serviceBinder, PhoneOtpServiceBase serviceImpl) in /.../obj/Debug/net7.0/SomeGrpc.cs:line 187
  9. InvokeStub_PhoneOtpService.BindService(Object, Object, IntPtr*)
  10. System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
  11. --- 内部异常堆栈跟踪的结尾 ---
  12. System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
  13. System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  14. Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingServiceMethodProvider`1.OnServiceMethodDiscovery(ServiceMethodProviderContext`1 context)
  15. --- 内部异常堆栈跟踪的结尾 ---
  16. Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingServiceMethodProvider`1.OnServiceMethodDiscovery(ServiceMethodProviderContext`1 context)
  17. Grpc.AspNetCore.Server.Model.Internal.ServiceRouteBuilder`1.Build(IEndpointRouteBuilder endpointRouteBuilder)
  18. 在 Microsoft.AspNetCore.Builder.GrpcEndpointRouteBuilderExtensions.MapGrpcService[TService](IEndpointRouteBuilder builder)
  19. 在 SomeApplication.Program.Main(String[] args) in /.../SomeApplication/Program.cs:line 97

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

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

文件夹结构如下:

  1. Protos
  2. google
  3. api
  4. annotations.proto
  5. http.proto
  6. Some.proto

我相信主要问题是:

  1. 存储的扩展值的类型为 '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:

  1. System.InvalidOperationException: Error binding gRPC service &#39;SomeGrpcService&#39;.
  2. ---&gt; System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
  3. ---&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;.
  4. at Google.Protobuf.ExtensionSet.Get[TTarget,TValue](ExtensionSet`1&amp; set, Extension`2 extension)
  5. at Google.Protobuf.Reflection.MethodOptions.GetExtension[TValue](Extension`2 extension)
  6. at Grpc.Shared.ServiceDescriptorHelpers.TryGetHttpRule(MethodDescriptor methodDescriptor, HttpRule&amp; httpRule)
  7. at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingProviderServiceBinder`1.AddMethod[TRequest,TResponse](Method`2 method, UnaryServerMethod`2 handler)
  8. at WBD.Bolt.OneTimePassword.gRPC.API.PhoneOtpService.BindService(ServiceBinderBase serviceBinder, PhoneOtpServiceBase serviceImpl) in /.../obj/Debug/net7.0/SomeGrpc.cs:line 187
  9. at InvokeStub_PhoneOtpService.BindService(Object, Object, IntPtr*)
  10. at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
  11. --- End of inner exception stack trace ---
  12. at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
  13. at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  14. at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingServiceMethodProvider`1.OnServiceMethodDiscovery(ServiceMethodProviderContext`1 context)
  15. --- End of inner exception stack trace ---
  16. at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Binding.JsonTranscodingServiceMethodProvider`1.OnServiceMethodDiscovery(ServiceMethodProviderContext`1 context)
  17. at Grpc.AspNetCore.Server.Model.Internal.ServiceRouteBuilder`1.Build(IEndpointRouteBuilder endpointRouteBuilder)
  18. at Microsoft.AspNetCore.Builder.GrpcEndpointRouteBuilderExtensions.MapGrpcService[TService](IEndpointRouteBuilder builder)
  19. 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:

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

and folder structure is:

  1. Protos
  2. google
  3. api
  4. annotations.proto
  5. http.proto
  6. Some.proto

I believe the main issue is:

  1. The stored extension value has a type of &#39;Google.Api.HttpRule, Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&#39;.
  2. 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

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

  1. google
  2. api
  3. annotations.proto
  4. http.proto
  5. Protos
  6. Some.proto

项目目录

英文:

You must change your directory like this:

  1. google
  2. api
  3. annotations.proto
  4. http.proto
  5. Protos
  6. Some.proto

Project Directory

答案2

得分: 0

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

更新 *.csproj 文件:

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

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

英文:

The following changes are fixed the issue for me:

update *.csproj file:

  1. &lt;ItemGroup&gt;
  2. &lt;Protobuf Include=&quot;Protos\Some.proto&quot; ProtoRoot=&quot;Protos\&quot; GrpcServices=&quot;Both&quot; /&gt;
  3. &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:

确定