同一应用中的Blazor服务器和GRPC服务器

时间:2020-07-10 07:23:05

标签: grpc blazor

我正在尝试将GRPC服务器功能添加到现有的Blazor服务器应用程序中。目的是该应用程序将提供用于人工操作的Web界面,并提供GRPC服务器以供其他GRPC客户端应用程序访问。我已经看过有关.NET GRPC的教程和文档,并且我了解这些概念。我不了解的是如何或什至可以将Kestrel服务器配置为执行此操作。

在Blazor应用程序中,我添加了实现Visual Studio“ gRPC Service”项目模板随附的基本“ Greeter”服务所需的功能。该应用程序会构建并运行,但是当我尝试从浏览器访问Blazor网站时,出现“必须通过gRPC客户端与与gRPC终结点的通信”消息,该消息告诉我GRPC服务器现在可以使用,但Blazor服务器现在无法使用。

是否可以为Blazor和GRPC位配置不同的URL,如果可以,如何配置?还是我应该做些其他事情?任何想法将不胜感激。

编辑

以下显示了我为添加gRPC内容而编辑的代码:

csproj文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <Version>1.0.3</Version>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Grpc.AspNetCore" Version="2.29.0" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\S34021.Edm\S34021.Edm.csproj" />
    </ItemGroup>

    <ItemGroup>
        <Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
    </ItemGroup>

</Project>

appsettings.json文件:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }
  }

}

Startup.cs文件:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddServerSideBlazor();
    services.AddSingleton<WeatherForecastService>();
    services.AddGrpc();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{

  if (env.IsDevelopment())
  {
      app.UseDeveloperExceptionPage();
  }
  else
  {
      app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
      app.UseHsts();
  }

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

  app.UseRouting();

  app.UseEndpoints(endpoints =>
  {
      endpoints.MapBlazorHub();
      endpoints.MapFallbackToPage("/_Host");

      // Added for GRPC
      endpoints.MapGrpcService<GreeterService>();

      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");
      });

    });

}

0 个答案:

没有答案
相关问题