1

以下代码片段显示了如何在 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}");
        }

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

dbug:Grpc.Net.Client.Internal.GrpcCall[18] 发送消息。失败:Grpc.Net.Client.Internal.GrpcCall[20] 发送消息时出错。System.Threading.Tasks.TaskCanceledException:任务被取消。
在 System.Net.Http.TaskCompletionSourceWithCancellation 1.WaitWithCancellationAsync(CancellationToken cancellationToken) at System.Net.Http.Http2Connection.Http2Stream.SendDataAsync(ReadOnlyMemory1 缓冲区,CancellationToken cancelToken) 在 Grpc.Net.Client.Web.Internal.Base64RequestStream.WriteAsync(ReadOnlyMemory 1 data, CancellationToken cancellationToken) at Grpc.Net.Client.StreamExtensions.WriteMessageAsync[TMessage](Stream stream, ILogger logger, TMessage message, Action2 序列化程序,字符串 grpcEncoding,Nullable1 maximumMessageSize, Dictionary2 compressionProviders, CallOptions callOptions) 失败:Grpc.Net.Client.Internal.GrpcCall[6] 启动 gRPC 调用时出错。System.Threading.Tasks.TaskCanceledException:操作已取消。在 System.Net.Http.CancellationHelper.ThrowOperationCanceledException(Exception innerException, CancellationToken cancelToken) 在 System.Net.Http.CancellationHelper.ThrowIfCancellationRequested(CancellationToken cancelToken) 在 System.Net.Http.Http2Connection.Http2Stream.GetCancelableWaiterTask(CancellationToken cancelToken) 在 System. Net.Http.Http2Connection.Http2Stream.ReadResponseHeadersAsync(CancellationToken cancelToken) at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, CancellationToken cancelToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request,
在 System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage 请求,CancellationToken cancelToken) 在 System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage 请求,CancellationToken cancelToken) 在 Grpc.Net.Client.Web.GrpcWebHandler.SendAsyncCore(HttpRequestMessage 请求, System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(任务1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at Grpc.Net.Client.Internal.GrpcCall2.RunCall(HttpRequestMessage 请求,Nullable`1 超时)处的 CancellationToken 取消令牌)信息:Grpc.Net.Client.Internal.GrpcCall[3] 调用失败,出现 gRPC 错误状态。状态码:'已取消',消息:''。dbug:Grpc.Net.Client.Internal.GrpcCall[4] 已完成 gRPC 调用。

我不知道是什么阻碍了这种交流的发生以及要实施什么解决方案。任何想法为什么?

作为旁注,我用尽了这张 github 票中的所有内容,但一点运气都没有。

4

1 回答 1

1

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

于 2020-07-22T17:20:31.583 回答