本文介绍了如何在.NET Framework中创建gRPC客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照以下指示进行操作:

I followed the instructions at:

https://docs.microsoft.com/zh-CN/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.1&tabs=visual-studio

在.NET Core中创建gRPC服务和客户端,一切运行良好.

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

接下来,我需要一个旧的.NET Framework应用程序来访问该服务.我找到了一些已安装的NuGet软件包,但没有发现任何可以告诉您如何使用它们制作gRPC客户端的软件包.我确定它在某处,但目前已被.NET Core版本的文档淹没.

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.

我试图创建一个.NET Standard项目来弥合差距,但是.Net Core软件包需要.Net Standard 2.1,因此在任何版本的.Net Framework中都没有引用它.

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?

因此,我找到了.Net Framework与gRPC一起使用的一些代码. .Net Framework示例默认为不安全连接,而.Net Core示例默认为安全连接.而且,关于如何更改任何一种方法,也没有明确的途径.我尝试生成证书以使客户端连接,但这没用.

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.

所以我的新问题是:有谁知道如何说服.Net Core gRPC服务接受不安全的(http :)连接?

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

推荐答案

是否通过SSL,您需要在ASP.NET Core服务器中打开Http2.因此,在appsettings.json中,执行此操作.

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

不安全的.NET Framework客户端+ ASP.NET Core服务器

  • ASP.NET Core服务器
  • ASP.NET Core Server
  1. 删除StartUpConfigureServices(IApplicationBuilder app)中的app.UseHttpsRedirection()app.UseHsts();
  2. 暴露不安全的端口,在开发过程中通常为80或5000.
  3. 使用下面的代码在.NET Framework客户端中创建不安全的通道.
  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", 5001, secureCredentials);

安全的SSL连接.NET Framework客户端+ ASP.NET Core服务器

我通过在客户端中使用.pem格式的同一服务器证书来使用SSL端口.

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

一些解释. VS 2019中的ASP.NETCore模板使用开发证书在%AppData%\ASP.NET\Https\ProjectName.pfx上使用pfx文件并密码= %AppData%\Microsoft\UserSecrets\{UserSecretsId}\secrets.json {:Kestrel:Certificates:Development:Password} Value您可以从ProjectName.csproj获取UserSecretsId id.对于每个ASP.NET Core项目,这都是不同的.

A bit of explanation. An ASP.NETCore template in VS 2019 uses a development certificatewith pfx file at %AppData%\ASP.NET\Https\ProjectName.pfx andpassword = %AppData%\Microsoft\UserSecrets\{UserSecretsId}\secrets.json {:Kestrel:Certificates:Development:Password} ValueYou can get the UserSecretsId id from the ProjectName.csproj. This will be different for each ASP.NET Core Project.

我使用以下命令将pfx +密码组合转换为certificate.pem文件.

I used the below command to convert the pfx + password combination to a certificate.pem file.

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

这将提示您输入pfx密码.使用上面的secrets.json中的密码.

This will prompt for the pfx password. Use the password from the above secrets.json.

为生成的certificate.pem提供一些密码(至少4个字母).

Give some passphrase for the certificate.pem to be generated(At least 4 letter).

复制此cerificate.pem以便gRPC .NET Framework客户端访问并在其中使用

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

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

请注意,我使用的端口5001是ASP.NET Core应用程序的SSL端口.

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

用于生产方案

使用来自证书签名机构的有效证书,并在ASP.NET Core Server和.NET Framework客户端中分别使用与pfxpem相同的证书.

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.

这篇关于如何在.NET Framework中创建gRPC客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:19