AnswerBun.com

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

Stack Overflow Asked by Rich Shipley on January 3, 2022

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?

2 Answers

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);

Answered by Jins Peter on January 3, 2022

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"
      }
    }
  }

Answered by Gopichandar on January 3, 2022

Add your own answers!

Related Questions

Animation to play once per session

0  Asked on January 15, 2021 by alejandro-vargas

       

postgres random text in jsonb column

2  Asked on January 15, 2021 by ed1t

         

How to make TestNG print detail message about the failure

2  Asked on January 15, 2021 by gelin-luo

 

Strange igraph behaviour, generating duplicate vertices

1  Asked on January 15, 2021 by simon-mills

   

C++ printing float as nan

1  Asked on January 14, 2021 by acarter

     

Webclient isn’t dowloading everything from URL c#

1  Asked on January 14, 2021 by shynex

       

How to change the entire div content on button click

2  Asked on January 14, 2021 by liel-barouch

   

Sorting and paging nested documents

2  Asked on January 14, 2021 by taras-kohut

 

How to adjust for loop so that it prints list only once?

1  Asked on January 14, 2021 by benito-cano

         

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir