通过 Grpc 预渲染和访问数据的 Blazor Web 程序集项目未在本地 IIS 服务器中运行

问题描述

我在以下链接中有两个具有 Grpc 服务的简单 Web 程序集项目。

without pre-rendering

with pre-rendering

在 Visual Studio 中运行时,这两种解决方案都可以正常工作。但是,当每个解决方案都通过本地 IIS 服务器发布和托管时,没有预渲染的一个可以正常工作,而另一个则不行。

The output of the web assembly project (no prerendering) when hosted thru local IIS.

The output of the pre-rendered web assembly project when hosted thru local IIS.

错误简介:

RpcException: Status(StatusCode="Internal",Detail="Errorstarting gRPC call. HttpRequestException: 发送请求时出错。Http2ConnectionException: HTTP/2 服务器在连接上发送了无效数据。HTTP/2 错误代码'PROTOCOL_ERROR' (0x1).",DebugException="System.Net.Http.HttpRequestException: 发送请求时出错。 ---> System.Net.Http.Http2ConnectionException: HTTP/2 服务器在连接上发送了无效数据。 HTTP/2 错误代码“PROTOCOL_ERROR”(0x1)。 在 System.Net.Http.Http2Connection.ThrowProtocolError(Http2ProtocolErrorCode errorCode) 在 System.Net.Http.dll:token 0x60005f3+0x6 在 System.Net.Http.Http2Connection.ReadFrameAsync(Boolean initialFrame) 中 System.Net.Http.dll:token 0x60005bc+0x1ea 在 System.Net.Http.Http2Connection.ProcessIncomingFramesAsync() 在 System.Net.Http.dll:token 0x60005bd+0x93

预渲染项目解决方案中的代码片段,用于将 Grpc 通道注册为 startup.cs 文件中的服务:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddRazorPages();
        services.AddGrpc();
        services.AddSingleton<HttpClient>(sp =>
        {
            // Get the address that the app is currently running at
            var server = sp.GetrequiredService<IServer>();
            var addressFeature = server.Features.Get<IServerAddressesFeature>();
            string baseAddress = addressFeature.Addresses.First();
            return new HttpClient { BaseAddress = new Uri(baseAddress) };
        });

        services.AddSingleton(services =>
        {
            var server = services.GetrequiredService<IServer>();
            var addressFeature = server.Features.Get<IServerAddressesFeature>();
            string baseAddress = addressFeature.Addresses.First();
            Console.WriteLine("first base in server -- " + addressFeature.Addresses.First());
            Console.WriteLine("last base in server -- " + addressFeature.Addresses.Last());
            var httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb,new httpclienthandler());
            return GrpcChannel.ForAddress(baseAddress,new GrpcChannelOptions { HttpHandler = httpHandler });
        });
    }

代码对比截图:

Startup.cs code in non-prerendering solution vs Startup.cs code in the prerendering solution

Program.cs code in non-prerendering solution vs Program.cs code in prerendering solution

配置:

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)