Skip to content

Commit d6ec609

Browse files
Convert standalone app to use gRPC for weather forecast
1 parent bcfbd59 commit d6ec609

File tree

9 files changed

+65
-18
lines changed

9 files changed

+65
-18
lines changed

Standalone/BlazorGrpcStandalone/BlazorGrpcStandalone.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@
1010
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.1.0-preview4.19579.2" PrivateAssets="all" />
1111
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.1.0-preview4.19579.2" />
1212
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.1.0-preview4.19579.2" PrivateAssets="all" />
13+
14+
<!-- Needed temporarily until the next Blazor WebAssembly preview release -->
15+
<PackageReference Include="Microsoft.AspNetCore.Blazor.Mono" Version="3.2.0-preview1.20052.1" />
16+
17+
<!-- gRPC-Web packages -->
18+
<PackageReference Include="Google.Protobuf" Version="3.11.2" />
19+
<PackageReference Include="Grpc.Net.Client" Version="2.27.0-dev202001100801" />
20+
<PackageReference Include="Grpc.Net.Client.Web" Version="2.27.0-dev202001100801" />
21+
<PackageReference Include="Grpc.Tools" Version="2.27.0-dev202001081219">
22+
<PrivateAssets>all</PrivateAssets>
23+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
24+
</PackageReference>
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<!-- Share .proto file with backend server to ensure consistency. Alternatively you could copy it into this project. -->
29+
<Protobuf Include="..\SampleBackendGrpcServer\Protos\weather.proto" />
1330
</ItemGroup>
1431

1532
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using Google.Protobuf.WellKnownTypes;
3+
4+
namespace BlazorGrpc // Note that the namespace must match the generated type
5+
{
6+
public partial class WeatherForecast
7+
{
8+
// Properties for the underlying data are generated from the .proto file
9+
// This partial class just adds some extra convenience properties
10+
11+
public DateTime Date
12+
{
13+
get => DateTimeStamp.ToDateTime();
14+
set { DateTimeStamp = Timestamp.FromDateTime(value.ToUniversalTime()); }
15+
}
16+
17+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
18+
}
19+
}

Standalone/BlazorGrpcStandalone/Pages/FetchData.razor

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@page "/fetchdata"
2-
@inject HttpClient Http
2+
@using BlazorGrpc
3+
@inject WeatherForecasts.WeatherForecastsClient WeatherForecastsClient
34

45
<h1>Weather forecast</h1>
56

@@ -35,21 +36,10 @@ else
3536
}
3637

3738
@code {
38-
private WeatherForecast[] forecasts;
39+
private IList<WeatherForecast> forecasts;
3940

4041
protected override async Task OnInitializedAsync()
4142
{
42-
forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
43-
}
44-
45-
public class WeatherForecast
46-
{
47-
public DateTime Date { get; set; }
48-
49-
public int TemperatureC { get; set; }
50-
51-
public string Summary { get; set; }
52-
53-
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
43+
forecasts = (await WeatherForecastsClient.GetWeatherAsync(new Empty())).Forecasts;
5444
}
5545
}

Standalone/BlazorGrpcStandalone/Startup.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1+
using BlazorGrpc;
2+
using Grpc.Net.Client;
3+
using Grpc.Net.Client.Web;
14
using Microsoft.AspNetCore.Components.Builder;
25
using Microsoft.Extensions.DependencyInjection;
6+
using System.Net.Http;
37

48
namespace BlazorGrpcStandalone
59
{
610
public class Startup
711
{
812
public void ConfigureServices(IServiceCollection services)
913
{
14+
services.AddSingleton(services =>
15+
{
16+
// Create a gRPC-Web channel pointing to the backend server
17+
var httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler()));
18+
19+
var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions { HttpClient = httpClient });
20+
21+
// Now we can instantiate gRPC clients for this channel
22+
return new WeatherForecasts.WeatherForecastsClient(channel);
23+
});
1024
}
1125

1226
public void Configure(IComponentsApplicationBuilder app)

Standalone/BlazorGrpcStandalone/_Imports.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
@using Microsoft.JSInterop
66
@using BlazorGrpcStandalone
77
@using BlazorGrpcStandalone.Shared
8+
@using Google.Protobuf.WellKnownTypes

Standalone/SampleBackendGrpcServer/Protos/weather.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ syntax = "proto3";
33
import "google/protobuf/empty.proto";
44
import "google/protobuf/timestamp.proto";
55

6-
option csharp_namespace = "SampleBackendGrpc";
6+
option csharp_namespace = "BlazorGrpc";
77

88
package WeatherForecast;
99

Standalone/SampleBackendGrpcServer/SampleBackendGrpcServer.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
</ItemGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Grpc.AspNetCore" Version="2.24.0" />
12+
<PackageReference Include="Grpc.AspNetCore" Version="2.27.0-dev202001100801" />
13+
<PackageReference Include="Grpc.AspNetCore.Web" Version="2.27.0-dev202001100801" />
1314
</ItemGroup>
1415

1516
</Project>

Standalone/SampleBackendGrpcServer/Services/WeatherForecastService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Threading.Tasks;
44
using Google.Protobuf.WellKnownTypes;
55
using Grpc.Core;
6-
using SampleBackendGrpc;
6+
using BlazorGrpc;
77

88
namespace SampleBackendGrpcServer.Services
99
{

Standalone/SampleBackendGrpcServer/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Startup
1414
public void ConfigureServices(IServiceCollection services)
1515
{
1616
services.AddGrpc();
17+
services.AddCors();
1718
}
1819

1920
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -25,10 +26,14 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2526
}
2627

2728
app.UseRouting();
29+
app.UseCors();
30+
app.UseGrpcWeb();
2831

2932
app.UseEndpoints(endpoints =>
3033
{
31-
endpoints.MapGrpcService<WeatherForecastsService>();
34+
endpoints.MapGrpcService<WeatherForecastsService>().EnableGrpcWeb()
35+
.RequireCors(cors => cors.AllowAnyHeader().AllowAnyMethod()
36+
.WithOrigins("http://localhost:54070", "https://localhost:44316"));
3237

3338
endpoints.MapGet("/", async context =>
3439
{

0 commit comments

Comments
 (0)