gpt4 book ai didi

gRPC 的 HTTP2 .Net 框架问题

转载 作者:行者123 更新时间:2023-12-04 22:39:52 25 4
gpt4 key购买 nike

我需要使用 gRPC 进行双向流式传输,其中客户端是 .Net Framework 项目,由于遗留问题,无法升级到 .NET5+。
在阅读 microsoft 文档时,我可以看到应该使用 WinHttpHandler (https://docs.microsoft.com/en-us/aspnet/core/grpc/netstandard?view=aspnetcore-5.0)。
但我似乎在此设置中使用 SSL 时遇到问题,有人对如何解决此问题有建议吗?
我收到以下错误:

"InvalidOperationException: SslCredentials with non-null arguments is not supported by GrpcChannel. GrpcChannel uses HttpClient to make gRPC calls and HttpClient automatically loads root certificates from the operating system certificate store. Client certificates should be configured on HttpClient. See https://aka.ms/aspnet/grpc/certauth for details."


我的服务器使用以下设置进行设置:
let cacert = File.ReadAllText(@"ca.crt");
let servercert = File.ReadAllText(@"server.crt");
let serverkey = File.ReadAllText(@"server.key");

let certificatePair = new KeyCertificatePair(servercert, serverkey);
let certList = new System.Collections.Generic.List<KeyCertificatePair>()
certList.Add(certificatePair)
let server = new Server()
server.Services.Add(EventSubscriberService.EventSubscriberServiceMethodBinder.BindService(new EventSubscriber()))
server.Ports.Add(new ServerPort("localhost", 5001,SslServerCredentials(certList,cacert,false)))
|> ignore'''
客户端使用此设置:
let cacert = File.ReadAllText(@"ca.crt");
let clientcert = File.ReadAllText(@"client.crt");
let clientkey = File.ReadAllText(@"client.key");
let ssl = new SslCredentials(cacert, new KeyCertificatePair(clientcert, clientkey))

let channelOptions = GrpcChannelOptions()
channelOptions.HttpHandler <- new WinHttpHandler()
channelOptions.Credentials <- ssl


let channel = GrpcChannel.ForAddress("http://127.0.0.1:5001",channelOptions)
它是用 F# 编写的,但类似于此 C# 代码,如果引用有助于提高可读性的话。 How to enable server side SSL for gRPC?

最佳答案

可能类似于以下几行(粗略)的东西应该起作用:

open System.Net.Http
open Grpc.Net.Client
open System.Security.Cryptography.X509Certificates

let clientCert = new X509Certificate() // create according to your needs
let handler = new HttpClientHandler()
handler.ClientCertificates.Add(clientCert) |> ignore
// insecure, check your custom server cert
handler.ServerCertificateCustomValidationCallback <- fun msg cert chain e -> true
let client = new HttpClient(handler)
let channelOptions = GrpcChannelOptions(HttpClient = client)
let channel = GrpcChannel.ForAddress("http://127.0.0.1:5001", channelOptions)

关于gRPC 的 HTTP2 .Net 框架问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69825958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com