gpt4 book ai didi

azure - 将服务器托管在 Azure 上后,gRPC 客户端无法与 gRPC 服务器通信

转载 作者:行者123 更新时间:2023-12-03 02:36:32 34 4
gpt4 key购买 nike

以下代码片段展示了如何在 gRPC AspNet 核心应用程序中启用 gRPC Web:

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseDefaultFiles();
app.UseStaticFiles();

app.UseRouting();

app.UseGrpcWeb();

app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<SubmissionService>().EnableGrpcWeb();
});
}
}

客户端应用程序的代码如下所示:

public static async Task SendSomethingAsync(string payload, ILoggerFactory loggerFactory)
{
const string url = "https://MASKED.azurewebsites.net/";
//const string url = "https://localhost:44308";
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var handler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler());
using var channel = GrpcChannel.ForAddress(url, new GrpcChannelOptions
{
HttpClient = new HttpClient(handler),
LoggerFactory = loggerFactory
});

var client = new TestgRPC.gRPC.PayloadSubmission.PayloadSubmissionClient(channel);
var submitted = await client.SubmitAsync(new SubmissionRequest
{
ClientKey = "678",
ConnectionId = "abcdef",
UserId = "3",
MyObjectPayload= payload,
});

Console.WriteLine($"Submitted: {submitted}");
}

当客户端代码(上面)托管在本地主机(开发计算机)上时,它会接收来自 gRPC 服务器的回复。相同的客户端代码无法与 Azure 上托管的服务器实例通信,并记录以下失败消息:

dbug: Grpc.Net.Client.Internal.GrpcCall[18]Sending message. fail: Grpc.Net.Client.Internal.GrpcCall[20]Error sending message. System.Threading.Tasks.TaskCanceledException: A task was canceled.
atSystem.Net.Http.TaskCompletionSourceWithCancellation1.WaitWithCancellationAsync(CancellationToken cancellationToken) at System.Net.Http.Http2Connection.Http2Stream.SendDataAsync(ReadOnlyMemory1buffer, CancellationToken cancellationToken) atGrpc.Net.Client.Web.Internal.Base64RequestStream.WriteAsync(ReadOnlyMemory1 data, CancellationToken cancellationToken) at Grpc.Net.Client.StreamExtensions.WriteMessageAsync[TMessage](Stream stream, ILogger logger, TMessage message, Action2 serializer, StringgrpcEncoding, Nullable1 maximumMessageSize, Dictionary2compressionProviders, CallOptions callOptions) fail:Grpc.Net.Client.Internal.GrpcCall[6]Error starting gRPC call. System.Threading.Tasks.TaskCanceledException: The operation wascanceled. atSystem.Net.Http.CancellationHelper.ThrowOperationCanceledException(ExceptioninnerException, CancellationToken cancellationToken) atSystem.Net.Http.CancellationHelper.ThrowIfCancellationRequested(CancellationTokencancellationToken) atSystem.Net.Http.Http2Connection.Http2Stream.GetCancelableWaiterTask(CancellationTokencancellationToken) atSystem.Net.Http.Http2Connection.Http2Stream.ReadResponseHeadersAsync(CancellationTokencancellationToken) atSystem.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) atSystem.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessagerequest, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessagerequest, CancellationToken cancellationToken) atSystem.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessagerequest, CancellationToken cancellationToken) atGrpc.Net.Client.Web.GrpcWebHandler.SendAsyncCore(HttpRequestMessagerequest, CancellationToken cancellationToken) atSystem.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at Grpc.Net.Client.Internal.GrpcCall2.RunCall(HttpRequestMessagerequest, Nullable`1 timeout) info:Grpc.Net.Client.Internal.GrpcCall[3]Call failed with gRPC error status. Status code: 'Cancelled', Message: ''. dbug: Grpc.Net.Client.Internal.GrpcCall[4]Finished gRPC call.

我无法找出是什么阻碍了这种沟通的发生以及要实现什么解决方案。有什么想法吗?

顺便说一句,我用尽了 this github ticket 中的所有内容。但一点运气都没有。

最佳答案

问题是我们需要将应用程序部署为独立的应用程序。显然,Azure 应用服务上安装的默认 .net core 3.1 无法容纳 gRPC-web 应用。

关于azure - 将服务器托管在 Azure 上后,gRPC 客户端无法与 gRPC 服务器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63020334/

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