Blazor server and GRPC server in the same app

Viewed 140

I am trying to add GRPC server capability to an existing Blazor server app. The intention is that the app will provide a web interface for human operation and also a GRPC server for access by other GRPC client apps. I have been through tutorials and docs on .NET GRPC and I understand the concepts. What I do not understand is how, or even if, the Kestrel server can be configured to do this.

To the Blazor app I have added what I think is needed to implement the basic “Greeter” service that comes with the Visual Studio “gRPC Service” project template. The app builds and runs, but when I try to access the Blazor web site from the browser I get the “Communication with gRPC endpoints must be made through a gRPC client” message which tells me that the GRPC server is now working but the Blazor server is now not working.

Is it possible to configure different URLs for the Blazor and GRPC bits, and if so how? Or is there something else I should be doing? Any ideas will be much appreciated.

EDIT

The following shows the bits of code I edited to add the gRPC stuff:

The csproj file:

<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>

The appsettings.json file:

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

}

The Startup.cs file:

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 Answers
Related