我们从客户端 Blazor 解决方案开始,并向其添加了 GRPC 接口。这可能会导致您想要的结果。顺便说一句,我们还为它添加了招摇。
在 CreateHostBuilder (program.cs) 中,我们添加了 UseUrls("https://localhost:44326", "https://localhost:6000")。HTML 从 44326 提供,GRPC 从端口 6000 提供。
private static IHostBuilder CreateHostBuilder(string[] args, WebApiClient webApiClient) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseKestrel()
.UseUrls("https://localhost:44326", "https://localhost:6000");
}).ConfigureServices((IServiceCollection services) => {
services.AddSingleton(webApiClient);
});
Startup 添加了 Grpc 和 RazorPages 等。在 Configure 中,我们将 GRPC 实现映射到端口 6000:
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ServerUpdatesHub>("/ServerUpdatesHub");
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
endpoints.MapGrpcService<MyGRPCService>().RequireHost($"*:6000");
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
});
});
}