Skip to content

Commit e2bbbbb

Browse files
committed
gRPC-Web code-first sample
1 parent a0df85f commit e2bbbbb

File tree

9 files changed

+119
-4
lines changed

9 files changed

+119
-4
lines changed

BlazorGrpcWebCodeFirst/Client/BlazorGrpcWebCodeFirst.Client.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Grpc.Net.Client" Version="2.28.0" />
10+
<PackageReference Include="Grpc.Net.Client.Web" Version="2.28.0-pre2" />
911
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.0-preview3.20168.3" />
1012
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0-preview3.20168.3" PrivateAssets="all" />
1113
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0-preview3.20168.3" PrivateAssets="all" />
1214
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.2.0-preview3.20168.3" />
15+
<PackageReference Include="protobuf-net.Grpc" Version="1.0.37" />
1316
</ItemGroup>
1417
<ItemGroup>
1518
<ProjectReference Include="..\Shared\BlazorGrpcWebCodeFirst.Shared.csproj" />
Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
@page "/"
2+
@using BlazorGrpcWebCodeFirst.Shared;
3+
@using ProtoBuf.Grpc.Client;
24

3-
<h1>Hello, world!</h1>
5+
<EditForm Model="@request" OnValidSubmit="Submit">
6+
<InputText @bind-Value="request.Text" />
7+
<InputNumber @bind-Value="request.Value" />
8+
<input type="submit" value="Submit" />
9+
</EditForm>
410

5-
Welcome to your new app.
11+
@if (result != null)
12+
{
13+
@result.NewText
14+
<br />
15+
@result.NewValue
16+
<br />
17+
}
618

7-
<SurveyPrompt Title="How is Blazor working for you?" />
19+
@code
20+
{
21+
MyServiceRequest request = new MyServiceRequest() { Text = "Hello world", Value = 42 };
22+
MyServiceResult result;
23+
24+
async Task Submit()
25+
{
26+
var handler = new Grpc.Net.Client.Web.GrpcWebHandler(Grpc.Net.Client.Web.GrpcWebMode.GrpcWeb, new HttpClientHandler());
27+
using (var channel = Grpc.Net.Client.GrpcChannel.ForAddress("https://localhost:44383/", new Grpc.Net.Client.GrpcChannelOptions() { HttpClient = new HttpClient(handler) }))
28+
{
29+
var testFacade = channel.CreateGrpcService<IMyService>();
30+
this.result = await testFacade.DoSomething(request);
31+
}
32+
}
33+
}

BlazorGrpcWebCodeFirst/Server/BlazorGrpcWebCodeFirst.Server.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Grpc.AspNetCore.Web" Version="2.28.0-pre2" />
910
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="3.2.0-preview3.20168.3" />
11+
<PackageReference Include="protobuf-net.Grpc.AspNetCore" Version="1.0.37" />
1012
</ItemGroup>
1113

1214
<ItemGroup>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using BlazorGrpcWebCodeFirst.Shared;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace BlazorGrpcWebCodeFirst.Server.Services
9+
{
10+
public class MyService : IMyService
11+
{
12+
public Task<MyServiceResult> DoSomething(MyServiceRequest request)
13+
{
14+
return Task.FromResult(new MyServiceResult()
15+
{
16+
NewText = request.Text + " from server",
17+
NewValue = request.Value + 1
18+
});
19+
}
20+
}
21+
}

BlazorGrpcWebCodeFirst/Server/Startup.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Hosting;
88
using System.Linq;
9+
using ProtoBuf.Grpc.Server;
10+
using BlazorGrpcWebCodeFirst.Server.Services;
11+
using BlazorGrpcWebCodeFirst.Shared;
912

1013
namespace BlazorGrpcWebCodeFirst.Server
1114
{
@@ -22,6 +25,14 @@ public Startup(IConfiguration configuration)
2225
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
2326
public void ConfigureServices(IServiceCollection services)
2427
{
28+
services.AddCodeFirstGrpc(config =>
29+
{
30+
config.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
31+
});
32+
services.AddGrpcWeb(options =>
33+
{
34+
options.GrpcWebEnabled = true;
35+
});
2536

2637
services.AddControllersWithViews();
2738
}
@@ -47,8 +58,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4758

4859
app.UseRouting();
4960

61+
app.UseGrpcWeb();
62+
5063
app.UseEndpoints(endpoints =>
5164
{
65+
endpoints.MapGrpcService<MyService>();
66+
5267
endpoints.MapControllers();
5368
endpoints.MapFallbackToFile("index.html");
5469
});
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="System.ServiceModel.Primitives" Version="4.7.0" />
9+
</ItemGroup>
10+
711
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.ServiceModel;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace BlazorGrpcWebCodeFirst.Shared
9+
{
10+
[ServiceContract]
11+
public interface IMyService
12+
{
13+
Task<MyServiceResult> DoSomething(MyServiceRequest request);
14+
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace BlazorGrpcWebCodeFirst.Shared
4+
{
5+
[DataContract]
6+
public class MyServiceRequest
7+
{
8+
[DataMember(Order = 1)]
9+
public string Text { get; set; }
10+
11+
[DataMember(Order = 2)]
12+
public int Value { get; set; }
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace BlazorGrpcWebCodeFirst.Shared
4+
{
5+
[DataContract]
6+
public class MyServiceResult
7+
{
8+
[DataMember(Order = 1)]
9+
public string NewText { get; set; }
10+
11+
[DataMember(Order = 2)]
12+
public int NewValue { get; set; }
13+
}
14+
}

0 commit comments

Comments
 (0)