20

I followed the instructions at:

https://learn.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.1&tabs=visual-studio

to create a gRPC service and client in .NET Core and everything worked great.

Next, I need to have a legacy .NET framework app access the service. I found some NuGet packages that install, but haven't found anything that tell you how to use them to make a gRPC client. I'm sure it is out there somewhere, but is currently being drowned out by documentation for the .NET Core version.

I tried creating a .NET Standard project to bridge the gap, but the .Net Core packages require .Net Standard 2.1, which leave out referencing it with any version of .Net Framework.

Can anyone tell me how to get this going or point me in the right direction?

edit: So I found some code for .Net Framework to work with gRPC. The .Net Framework examples default to an insecure connection while the .Net Core examples default to secure connections. And there's no clear path on how to change either one. I've tried generating a certificate to get the client to connect, but that didn't work.

So my new question is: Does anyone know how to convince a .Net Core gRPC service to accept insecure (http:) connections?

Rich Shipley
  • 317
  • 1
  • 2
  • 6
  • Hi that's interesting, I wonder if you had any luck with the `GrpcGreeterClient` sample project in that documentation? – IronMan Mar 20 '20 at 00:15
  • 3
    I created a service and client based on the Greeter in .Net Core. I haven't been able to find something like that for .Net Framework. – Rich Shipley Mar 20 '20 at 01:51
  • Have a look at this project targeting .net 4.5 - https://github.com/grpc/grpc/tree/master/src/csharp/Grpc.Examples.MathServer – weichch Mar 20 '20 at 03:15
  • 1
    I didn't see a grpc client there – Rich Shipley Mar 22 '20 at 18:22
  • Ended up solving the issue by making the server with .Net Framework. If I can figure out how to get the Framework client to work in secure mode, I'll switch back. The .Net Core service implementation does not seem to support insecure connections. – Rich Shipley Mar 26 '20 at 19:11
  • @RichShipley I hope the answer help you. Can you share the link for .Netframework client that connects to .Net core Grpc? – Gopichandar Apr 09 '20 at 13:03
  • @RichShipley You can have a Insecure connection with .NET Core Server + .NET Framework Client or a secure connection with the same combination. Try my answer. – Jins Peter Jul 24 '20 at 20:42
  • I am trying to do the same. I have a grpc greeter service, which can be accessed sucessfully from a .net core grpc cleu – Rachna Mehta Sep 08 '20 at 13:08
  • Is the .NET framework based grpc client running in Win11 ? I see that as a requirement in https://learn.microsoft.com/en-us/aspnet/core/grpc/supported-platforms?view=aspnetcore-6.0#net-grpc-client-requirements – Prabhakaran Rajagopal Aug 30 '22 at 04:57

2 Answers2

11

Over SSL or not, you need to turn on Http2 in ASP.NET Core server. So in appsettings.json, do this.

"Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }

Insecure .NET Framework Client + ASP.NET Core Server

  • ASP.NET Core Server
    1. Remove app.UseHttpsRedirection() and app.UseHsts() in the StartUp class ConfigureServices(IApplicationBuilder app);
    2. Expose the insecure port, typically 80 or 5000 during development.
    3. Use the code below to create insecure channel in .NET Framework client.
var channel = new Channel("localhost", 5000, ChannelCredentials.Insecure);

Secure SSL connection .NET Framework Client + ASP.NET Core Server

I got it working with SSL port by using the same Server's certificate in .pem format in the client.

SslCredentials secureCredentials = new SslCredentials(File.ReadAllText("certificate.pem"));
var channel = new Channel("localhost", 5001, secureCredentials);

A bit of explanation. An ASP.NETCore template in VS 2019 uses a development certificate with pfx file at %AppData%\ASP.NET\Https\ProjectName.pfx. The password of the certificate will be available at %AppData%\Microsoft\UserSecrets\{UserSecretsId}\secrets.json You can get the UserSecretsId id from the ProjectName.csproj. This will be different for each ASP.NET Core Project.

We just need the public key of the certificate as a certificate.pem file to communicate securely over gRPC. Use the command below to extract publickey from pfx

openssl pkcs12 -in "<DiskLocationOfPfx>\ProjectName.pfx" -nokeys -out "<TargetLocation>\certifcate.pem"

Copy this cerificate.pem for the gRPC .NET Framework client to use.

SslCredentials secureCredentials = new SslCredentials(File.ReadAllText("<DiskLocationTo the Folder>/certificate.pem"))
var channel = new Channel("localhost", 5001, secureCredentials);

Note that port 5001 I used is the SSL port of my ASP.NET Core application.

For Production Scenarios

Use a valid certificate from certificate signing authority and use same certificate in ASP.NET Core Server and .NET Framework client as pfx and pem respectively.

Using Self signed certificate

Using Self signed certificates are a valid option for most microservices that communicate between our own microservices. We may not need an authority signed certificate. One problem we may face with using self signed certificate is that the certificate may be issued to some target DNS name and our gRPC server may be running somewhere else and secure connection cannot be established.

Use gRPC Target Name override keys to override the ssl target name validation.

   List<ChannelOption> channelOptions = new List<ChannelOption>()
   {
       new ChannelOption("grpc.ssl_target_name_override", <DNS to which our certificate is issued to>),
   };
   SslCredentials secureCredentials = new SslCredentials(File.ReadAllText("certificate.pem"));

   var channel = new Channel("localhost", 5001, secureCredentials, channelOptions);

Jins Peter
  • 2,368
  • 1
  • 18
  • 37
  • 1
    I found a way without needing to store the pem on the client side, maybe it's useful for other people as well: https://stackoverflow.com/a/66041094/727250 – rene_r Feb 04 '21 at 07:45
  • I can only call the service insecurely using this, using my cert on the client dont work. – schh Sep 02 '21 at 18:45
  • There is a section for insecure, and there is one for secure in my answer. @schh. Please read fully – Jins Peter Sep 03 '21 at 11:05
  • I got it working for both secure and insecure now with with a core client and a ,net framework client. I regenerated my self signed certificates from scratch and it works fine now. thanks – schh Sep 06 '21 at 23:57
  • Under which assembly the `Channel` class is available ? @JinsPeter – NeWi-SL Nov 17 '21 at 11:41
  • @NeWi-SL You do not need to use this mechanism anymore in .NET framework. You can directly use [Grpc.Net.Client](https://www.nuget.org/packages/Grpc.Net.Client) now. I wrote this answer at a time when we did not have that option – Jins Peter Nov 18 '21 at 05:49
  • @NeWi-SL to answer your question the Channel class is from [Grpc.Core](https://www.nuget.org/packages/Grpc.Core) which entered maintenance mode and will be [deprecated by May 2022](https://grpc.io/blog/grpc-csharp-future/) – Jins Peter Nov 18 '21 at 05:52
9

you can configure the .Net core grpc server on insecure through config

There are 2 ways,

launchSettings,json

{
  "profiles": {
    "DemoService": {
      "commandName": "Project",
      "launchBrowser": false,
      "applicationUrl": "http://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Or

appsettings,json

"Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    },
    "EndPoints": {
      "Http": {
        "Url": "http://localhost:5001"
      }
    }
  }
Gopichandar
  • 2,742
  • 2
  • 24
  • 54