Skip to content

Commit 72544c5

Browse files
Convert hosted app to use gRPC for weather forecast
1 parent 73278a9 commit 72544c5

11 files changed

+103
-52
lines changed

Diff for: Hosted/Client/BlazorGrpcHosted.Client.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Grpc.Net.Client.Web" Version="2.27.0-dev202001100801" />
910
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.1.0-preview4.19579.2" />
1011
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.1.0-preview4.19579.2" PrivateAssets="all" />
1112
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.1.0-preview4.19579.2" />
1213
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.1.0-preview4.19579.2" PrivateAssets="all" />
14+
<PackageReference Include="Microsoft.AspNetCore.Blazor.Mono" Version="3.2.0-preview1.20052.1" />
1315
</ItemGroup>
1416
<ItemGroup>
1517
<ProjectReference Include="..\Shared\BlazorGrpcHosted.Shared.csproj" />

Diff for: Hosted/Client/Pages/FetchData.razor

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@page "/fetchdata"
22
@using BlazorGrpcHosted.Shared
3-
@inject HttpClient Http
3+
@inject WeatherForecasts.WeatherForecastsClient WeatherForecastsClient
44

55
<h1>Weather forecast</h1>
66

@@ -36,11 +36,11 @@ else
3636
}
3737

3838
@code {
39-
private WeatherForecast[] forecasts;
39+
private IList<WeatherForecast> forecasts;
4040

4141
protected override async Task OnInitializedAsync()
4242
{
43-
forecasts = await Http.GetJsonAsync<WeatherForecast[]>("WeatherForecast");
43+
forecasts = (await WeatherForecastsClient.GetWeatherAsync(new Empty())).Forecasts;
4444
}
4545

4646
}

Diff for: Hosted/Client/Startup.cs

+15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1+
using BlazorGrpcHosted.Shared;
2+
using Grpc.Net.Client;
3+
using Grpc.Net.Client.Web;
4+
using Microsoft.AspNetCore.Components;
15
using Microsoft.AspNetCore.Components.Builder;
26
using Microsoft.Extensions.DependencyInjection;
7+
using System.Net.Http;
38

49
namespace BlazorGrpcHosted.Client
510
{
611
public class Startup
712
{
813
public void ConfigureServices(IServiceCollection services)
914
{
15+
services.AddSingleton(services =>
16+
{
17+
// Create a gRPC-Web channel pointing to the backend server
18+
var httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler()));
19+
var baseUri = services.GetRequiredService<NavigationManager>().BaseUri;
20+
var channel = GrpcChannel.ForAddress(baseUri, new GrpcChannelOptions { HttpClient = httpClient });
21+
22+
// Now we can instantiate gRPC clients for this channel
23+
return new WeatherForecasts.WeatherForecastsClient(channel);
24+
});
1025
}
1126

1227
public void Configure(IComponentsApplicationBuilder app)

Diff for: Hosted/Client/_Imports.razor

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
@using Microsoft.JSInterop
66
@using BlazorGrpcHosted.Client
77
@using BlazorGrpcHosted.Client.Shared
8+
@using Google.Protobuf.WellKnownTypes

Diff for: Hosted/Server/BlazorGrpcHosted.Server.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Grpc.AspNetCore" Version="2.27.0-dev202001100801" />
10+
<PackageReference Include="Grpc.AspNetCore.Web" Version="2.27.0-dev202001100801" />
911
<PackageReference Include="Microsoft.AspNetCore.Blazor.Server" Version="3.1.0-preview4.19579.2" />
1012
</ItemGroup>
1113

Diff for: Hosted/Server/Controllers/WeatherForecastController.cs

-40
This file was deleted.

Diff for: Hosted/Server/Services/WeatherForecastService.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using BlazorGrpcHosted.Shared;
2+
using System;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Google.Protobuf.WellKnownTypes;
6+
using Grpc.Core;
7+
8+
namespace BlazorGrpcHosted.Server.Services
9+
{
10+
public class WeatherForecastsService : WeatherForecasts.WeatherForecastsBase
11+
{
12+
private static readonly string[] Summaries = new[]
13+
{
14+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
15+
};
16+
17+
public override Task<WeatherReply> GetWeather(Empty request, ServerCallContext context)
18+
{
19+
var reply = new WeatherReply();
20+
21+
var rng = new Random();
22+
reply.Forecasts.Add(Enumerable.Range(1, 5).Select(index => new WeatherForecast
23+
{
24+
Date = DateTime.Now.AddDays(index),
25+
TemperatureC = rng.Next(-20, 55),
26+
Summary = Summaries[rng.Next(Summaries.Length)]
27+
}));
28+
29+
return Task.FromResult(reply);
30+
}
31+
}
32+
}

Diff for: Hosted/Server/Startup.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BlazorGrpcHosted.Server.Services;
12
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.ResponseCompression;
@@ -13,7 +14,7 @@ public class Startup
1314
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
1415
public void ConfigureServices(IServiceCollection services)
1516
{
16-
services.AddMvc();
17+
services.AddGrpc();
1718
services.AddResponseCompression(opts =>
1819
{
1920
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
@@ -36,10 +37,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3637
app.UseClientSideBlazorFiles<Client.Startup>();
3738

3839
app.UseRouting();
40+
app.UseGrpcWeb();
3941

4042
app.UseEndpoints(endpoints =>
4143
{
42-
endpoints.MapDefaultControllerRoute();
44+
endpoints.MapGrpcService<WeatherForecastsService>().EnableGrpcWeb();
4345
endpoints.MapFallbackToClientSideBlazor<Client.Startup>("index.html");
4446
});
4547
}

Diff for: Hosted/Shared/BlazorGrpcHosted.Shared.csproj

+13
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,17 @@
55
<LangVersion>7.3</LangVersion>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<PackageReference Include="Google.Protobuf" Version="3.11.2" />
10+
<PackageReference Include="Grpc.Net.Client" Version="2.27.0-dev202001100801" />
11+
<PackageReference Include="Grpc.Tools" Version="2.27.0-dev202001081219">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Protobuf Include="weather.proto" />
19+
</ItemGroup>
20+
821
</Project>

Diff for: Hosted/Shared/WeatherForecast.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
2+
using Google.Protobuf.WellKnownTypes;
43

54
namespace BlazorGrpcHosted.Shared
65
{
7-
public class WeatherForecast
6+
public partial class WeatherForecast
87
{
9-
public DateTime Date { get; set; }
8+
// Properties for the underlying data are generated from the .proto file
9+
// This partial class just adds some extra convenience properties
1010

11-
public int TemperatureC { get; set; }
12-
13-
public string Summary { get; set; }
11+
public DateTime Date
12+
{
13+
get => DateTimeStamp.ToDateTime();
14+
set { DateTimeStamp = Timestamp.FromDateTime(value.ToUniversalTime()); }
15+
}
1416

1517
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
1618
}

Diff for: Hosted/Shared/weather.proto

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
syntax = "proto3";
2+
3+
import "google/protobuf/empty.proto";
4+
import "google/protobuf/timestamp.proto";
5+
6+
option csharp_namespace = "BlazorGrpcHosted.Shared";
7+
8+
package WeatherForecast;
9+
10+
service WeatherForecasts {
11+
rpc GetWeather (google.protobuf.Empty) returns (WeatherReply);
12+
}
13+
14+
message WeatherReply {
15+
repeated WeatherForecast forecasts = 1;
16+
}
17+
18+
message WeatherForecast {
19+
google.protobuf.Timestamp dateTimeStamp = 1;
20+
int32 temperatureC = 2;
21+
string summary = 3;
22+
}

0 commit comments

Comments
 (0)