Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

c# - How do you create a gRPC client in .NET Framework?

I followed the instructions at:

https://docs.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.NETHttpsProjectName.pfx. The password of the certificate will be available at %AppData%MicrosoftUserSecrets{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);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...