Stack Overflow Asked by Rich Shipley on January 3, 2022
I followed the instructions at:
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?
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
app.UseHttpsRedirection()
and app.UseHsts()
in the StartUp
class ConfigureServices(IApplicationBuilder app)
;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,
{
"profiles": {
"DemoService": {
"commandName": "Project",
"launchBrowser": false,
"applicationUrl": "http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Or
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http2"
},
"EndPoints": {
"Http": {
"Url": "http://localhost:5001"
}
}
}
Answered by Gopichandar on January 3, 2022
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP