Skip to content

Commit 0da0662

Browse files
authored
Add gRPC-Web Blazor example (#718)
1 parent 0d35b5a commit 0da0662

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2814
-1
lines changed

build/dependencies.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<GrpcDotNetPackageVersion>2.25.0</GrpcDotNetPackageVersion> <!-- Only use for example projects -->
77
<GrpcPackageVersion>2.27.0-dev202001131222</GrpcPackageVersion>
88
<MicrosoftAspNetCorePackageVersion>3.0.0</MicrosoftAspNetCorePackageVersion>
9+
<MicrosoftAspNetCoreBlazorPackageVersion>3.1.0-preview4.19579.2</MicrosoftAspNetCoreBlazorPackageVersion>
910
<MicrosoftBuildLocatorPackageVersion>1.2.2</MicrosoftBuildLocatorPackageVersion>
1011
<MicrosoftBuildPackageVersion>16.0.461</MicrosoftBuildPackageVersion>
1112
<MicrosoftCodeAnalysisFxCopAnalyzersPackageVersion>2.9.4</MicrosoftCodeAnalysisFxCopAnalyzersPackageVersion>

build_and_test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ example_solutions=( $( ls examples/**/*.sln ) )
2727

2828
for example_solution in "${example_solutions[@]}"
2929
do
30+
# dotnet build uses msbuild, and attempts to speed consecutive builds by reusing processes.
31+
# This can become a problem when multiple versions of Grpc.Tools are used between builds.
32+
# The different versions will conflict. Shutdown build processes between builds to avoid conflicts.
33+
# Will be fixed in msbuild 16.5 - https://github.com/microsoft/msbuild/issues/1754
34+
dotnet build-server shutdown
35+
3036
dotnet build $example_solution -c Release
3137
done
3238

examples/Blazor/Blazor.sln

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29521.150
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{B22D0F2E-D67A-413E-9450-4B64A60B56EF}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{09CAF21C-D7D8-478C-9E74-3678DF615B8F}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.Net.Client.Web", "..\..\src\Grpc.Net.Client.Web\Grpc.Net.Client.Web.csproj", "{FDE80D13-B93B-421E-A67C-23C28EE19F52}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.AspNetCore.Web", "..\..\src\Grpc.AspNetCore.Web\Grpc.AspNetCore.Web.csproj", "{72824FA1-8041-4252-9230-92208FA7C749}"
13+
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.Net.Client", "..\..\src\Grpc.Net.Client\Grpc.Net.Client.csproj", "{2EAB3743-10DD-44F5-9D25-04BDEB0C3BEC}"
15+
EndProject
16+
Global
17+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18+
Debug|Any CPU = Debug|Any CPU
19+
Release|Any CPU = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
22+
{B22D0F2E-D67A-413E-9450-4B64A60B56EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{B22D0F2E-D67A-413E-9450-4B64A60B56EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{B22D0F2E-D67A-413E-9450-4B64A60B56EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{B22D0F2E-D67A-413E-9450-4B64A60B56EF}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{09CAF21C-D7D8-478C-9E74-3678DF615B8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{09CAF21C-D7D8-478C-9E74-3678DF615B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{09CAF21C-D7D8-478C-9E74-3678DF615B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{09CAF21C-D7D8-478C-9E74-3678DF615B8F}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{FDE80D13-B93B-421E-A67C-23C28EE19F52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{FDE80D13-B93B-421E-A67C-23C28EE19F52}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{FDE80D13-B93B-421E-A67C-23C28EE19F52}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{FDE80D13-B93B-421E-A67C-23C28EE19F52}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{72824FA1-8041-4252-9230-92208FA7C749}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{72824FA1-8041-4252-9230-92208FA7C749}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{72824FA1-8041-4252-9230-92208FA7C749}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{72824FA1-8041-4252-9230-92208FA7C749}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{2EAB3743-10DD-44F5-9D25-04BDEB0C3BEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{2EAB3743-10DD-44F5-9D25-04BDEB0C3BEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{2EAB3743-10DD-44F5-9D25-04BDEB0C3BEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{2EAB3743-10DD-44F5-9D25-04BDEB0C3BEC}.Release|Any CPU.Build.0 = Release|Any CPU
42+
EndGlobalSection
43+
GlobalSection(SolutionProperties) = preSolution
44+
HideSolutionNode = FALSE
45+
EndGlobalSection
46+
GlobalSection(ExtensibilityGlobals) = postSolution
47+
SolutionGuid = {95736335-0C8B-4FDA-8722-E08046339B83}
48+
EndGlobalSection
49+
EndGlobal

examples/Blazor/Client/App.razor

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Router AppAssembly="@typeof(Program).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
</Found>
5+
<NotFound>
6+
<LayoutView Layout="@typeof(MainLayout)">
7+
<p>Sorry, there's nothing at this address.</p>
8+
</LayoutView>
9+
</NotFound>
10+
</Router>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
<RazorLangVersion>3.0</RazorLangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="$(MicrosoftAspNetCoreBlazorPackageVersion)" />
10+
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="$(MicrosoftAspNetCoreBlazorPackageVersion)" PrivateAssets="all" />
11+
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="$(MicrosoftAspNetCoreBlazorPackageVersion)" />
12+
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="$(MicrosoftAspNetCoreBlazorPackageVersion)" PrivateAssets="all" />
13+
14+
<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
15+
<PackageReference Include="Grpc.Tools" Version="$(GrpcPackageVersion)" PrivateAssets="All" />
16+
17+
<Protobuf Include="..\Proto\weather.proto" GrpcServices="Client" Link="Protos\weather.proto" Access="Internal" />
18+
19+
<ProjectReference Include="..\..\..\src\Grpc.Net.Client.Web\Grpc.Net.Client.Web.csproj" />
20+
<!-- Changes in Grpc.Net.Client are not on NuGet yet. Reference latest source from project -->
21+
<ProjectReference Include="..\..\..\src\Grpc.Net.Client\Grpc.Net.Client.csproj" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System;
20+
using Google.Protobuf.WellKnownTypes;
21+
22+
namespace Weather
23+
{
24+
// Properties for the underlying data are generated from the .proto file
25+
// This partial class just adds some extra convenience properties
26+
internal partial class WeatherForecast
27+
{
28+
public DateTime DateTime
29+
{
30+
get => Date.ToDateTime();
31+
set { Date = Timestamp.FromDateTime(value.ToUniversalTime()); }
32+
}
33+
34+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
35+
}
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@page "/counter"
2+
3+
<h1>Counter</h1>
4+
5+
<p>Current count: @currentCount</p>
6+
7+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
8+
9+
@code {
10+
private int currentCount = 0;
11+
12+
private void IncrementCount()
13+
{
14+
currentCount++;
15+
}
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@page "/fetchdata"
2+
@inject WeatherForecasts.WeatherForecastsClient WeatherForecastsClient
3+
@using Google.Protobuf.WellKnownTypes
4+
5+
<h1>Weather forecast</h1>
6+
7+
<p>This component demonstrates fetching data from the server.</p>
8+
9+
@if (forecasts == null)
10+
{
11+
<p><em>Loading...</em></p>
12+
}
13+
else
14+
{
15+
<table class="table">
16+
<thead>
17+
<tr>
18+
<th>Date</th>
19+
<th>Temp. (C)</th>
20+
<th>Temp. (F)</th>
21+
<th>Summary</th>
22+
</tr>
23+
</thead>
24+
<tbody>
25+
@foreach (var forecast in forecasts)
26+
{
27+
<tr>
28+
<td>@forecast.DateTime.ToShortDateString()</td>
29+
<td>@forecast.TemperatureC</td>
30+
<td>@forecast.TemperatureF</td>
31+
<td>@forecast.Summary</td>
32+
</tr>
33+
}
34+
</tbody>
35+
</table>
36+
}
37+
38+
<img src="/grpc-logo.png" alt="gRPC logo" />
39+
40+
@code {
41+
private IList<WeatherForecast>? forecasts;
42+
43+
protected override async Task OnInitializedAsync()
44+
{
45+
forecasts = (await WeatherForecastsClient.GetWeatherForecastsAsync(new Empty())).Forecasts;
46+
}
47+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@page "/"
2+
3+
<h1>Hello, world!</h1>
4+
5+
Welcome to your new app.
6+
7+
<SurveyPrompt Title="How is Blazor working for you?" />

examples/Blazor/Client/Program.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using Microsoft.AspNetCore.Blazor.Hosting;
20+
21+
namespace Client
22+
{
23+
public class Program
24+
{
25+
public static void Main(string[] args)
26+
{
27+
CreateHostBuilder(args).Build().Run();
28+
}
29+
30+
public static IWebAssemblyHostBuilder CreateHostBuilder(string[] args) =>
31+
BlazorWebAssemblyHost.CreateDefaultBuilder()
32+
.UseBlazorStartup<Startup>();
33+
}
34+
}

0 commit comments

Comments
 (0)