4

I am trying to connect to a gRPC service in a .NetFramework 4.5 windows application. I am currently using metapackage nuget Grpc which I know is only in maintenance mode now...

I need to access the service which is not at a host:port location, but has a subpath, subdirectory. My service is located here "https://example.com/mySubpath"

Is there a way to specify a subdirectory, subpath for gRPC calls in .NetFramework?

  • For .NetFramework4.5, if I specify the full path (which contains the "mySubpath" part) it doesn't work
var channel = new Channel("example.com/mySubpath", ChannelCredentials.SecureSsl);

I saw that I could also add a List<ChannelOption> parameter to the Channel constructor, but don't know if there is one option which I could use for that.

I have found a solution here https://github.com/grpc/grpc-dotnet/issues/880 for .NetCore and I am trying to see if I can use that to the .NetFramework 4.6. I used the SubdirectoryHandler class as it is defined in the link to change the RequestUri and assigned a WinHttpHandler

var handler = new SubdirectoryHandler(new WinHttpHandler(), subpath);
var httpClient = new HttpClient(handler);
GrpcChannel channel = GrpcChannel.ForAddress(address, new GrpcChannelOptions { HttpClient = httpClient });
            

but I am getting an exception related to assembly System.Buffers, Version=4.0.2.0.

StatusCode="Unavailable",
Detail="Error starting gRPC call. HttpRequestException: Error while copying content to a stream. FileLoadException: Could not load file or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)",

I think this is because Grpc.Net.Client references version=4.0.2.0, while the System.Net.Http.WinHttpHandler references version 4.0.3.0.

Any idea what I need to change to be able to use the subdirectory,subpath for .NetFramework 4.6.1 or for .NetFramework4.5?

AndreeaGr
  • 41
  • 2
  • To resolve the System.Buffers problem you should add BindingRedirects to your web.config and that should resolve that, Binding redirects such as everything between versions 0.0.0.0 to 4.0.3.0 go to 4.0.3.0. I had exactly the same problem and this resolved it. But I am very interested in that Grpc.Core solution. I have exactly the same problem, we are using Grpc.Net for newer apps, but GrpcCore for legacy ones that we cannot yet upgrade, GrpcNet you can resolve by the link you posted, but GrpcCore and ChannelOption, I ams truglling with that as well. – Vojtěch Mráz Mar 25 '22 at 10:52
  • Thank you. I will try your sugestion with BindingRedirects. Unfortunatelly, I wasn't able to make it work with Grpc.Core for path with subdirectories.... But I am able to upgrade my application to .NetFramework 4.6.1 where I can use the Grpc.Net, but I understood that it's a limited support and only works with that WinHttpHandler. – AndreeaGr Mar 28 '22 at 05:52
  • I tried the BindingRedirects. I don't get that error anymore. Now I get something else: Status(StatusCode="Internal", Detail="Bad gRPC response. Response protocol downgraded to HTTP/1.1.") – AndreeaGr Mar 28 '22 at 08:30
  • I had this error while trying to use Grpc.Core on HTTP, I moved everything to HTTPS with certificate. I did not see this problem while using GRpc.NetClient, but it will be something very similar, so I suggest you to use HTTPS. – Vojtěch Mráz Mar 31 '22 at 07:27
  • My call is with https, but I am using Bearer token which I am sending in the Authorization header....no certificate. – AndreeaGr Apr 01 '22 at 08:16
  • For Grpc.Core (the legacy gRPC C# implementation), there is no way to override the URL for the invoked RPCs (e.g. to put a service in a "subdirectory" on a HTTP server). The reason for that is that the service/method path for RPCs is standardized and changing it would cause incompatibilities between gRPC implementations, so we chose not to support that use case. – Jan Tattermusch Apr 13 '22 at 14:41
  • As per the Grpc.Net.Client over WinHttpHandler, it seems that you just need to set your binding redirects correctly. (In this specific case to make sure that `System.Buffers` is resolved correctly). Grpc.Net.Client happens to support overriding the path for RPCs, so your use case might work after that (even though, as said earlier, it's not something that's generally supported by gRPC). – Jan Tattermusch Apr 13 '22 at 14:44

0 Answers0