From e2bbbbb0aa8b9c9709145e271ee25cb41a4effae Mon Sep 17 00:00:00 2001 From: Robert Haken Date: Wed, 15 Apr 2020 01:09:22 +0200 Subject: [PATCH] gRPC-Web code-first sample --- .../BlazorGrpcWebCodeFirst.Client.csproj | 3 ++ .../Client/Pages/Index.razor | 32 +++++++++++++++++-- .../BlazorGrpcWebCodeFirst.Server.csproj | 2 ++ .../Server/Services/MyService.cs | 21 ++++++++++++ BlazorGrpcWebCodeFirst/Server/Startup.cs | 15 +++++++++ .../BlazorGrpcWebCodeFirst.Shared.csproj | 6 +++- BlazorGrpcWebCodeFirst/Shared/IMyService.cs | 16 ++++++++++ .../Shared/MyServiceRequest.cs | 14 ++++++++ .../Shared/MyServiceResult.cs | 14 ++++++++ 9 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 BlazorGrpcWebCodeFirst/Server/Services/MyService.cs create mode 100644 BlazorGrpcWebCodeFirst/Shared/IMyService.cs create mode 100644 BlazorGrpcWebCodeFirst/Shared/MyServiceRequest.cs create mode 100644 BlazorGrpcWebCodeFirst/Shared/MyServiceResult.cs diff --git a/BlazorGrpcWebCodeFirst/Client/BlazorGrpcWebCodeFirst.Client.csproj b/BlazorGrpcWebCodeFirst/Client/BlazorGrpcWebCodeFirst.Client.csproj index 86ab650..7da3da4 100644 --- a/BlazorGrpcWebCodeFirst/Client/BlazorGrpcWebCodeFirst.Client.csproj +++ b/BlazorGrpcWebCodeFirst/Client/BlazorGrpcWebCodeFirst.Client.csproj @@ -6,10 +6,13 @@ + + + diff --git a/BlazorGrpcWebCodeFirst/Client/Pages/Index.razor b/BlazorGrpcWebCodeFirst/Client/Pages/Index.razor index e54d914..0fd9dba 100644 --- a/BlazorGrpcWebCodeFirst/Client/Pages/Index.razor +++ b/BlazorGrpcWebCodeFirst/Client/Pages/Index.razor @@ -1,7 +1,33 @@ @page "/" +@using BlazorGrpcWebCodeFirst.Shared; +@using ProtoBuf.Grpc.Client; -

Hello, world!

+ + + + + -Welcome to your new app. +@if (result != null) +{ + @result.NewText +
+ @result.NewValue +
+} - +@code +{ + MyServiceRequest request = new MyServiceRequest() { Text = "Hello world", Value = 42 }; + MyServiceResult result; + + async Task Submit() + { + var handler = new Grpc.Net.Client.Web.GrpcWebHandler(Grpc.Net.Client.Web.GrpcWebMode.GrpcWeb, new HttpClientHandler()); + using (var channel = Grpc.Net.Client.GrpcChannel.ForAddress("https://localhost:44383/", new Grpc.Net.Client.GrpcChannelOptions() { HttpClient = new HttpClient(handler) })) + { + var testFacade = channel.CreateGrpcService(); + this.result = await testFacade.DoSomething(request); + } + } +} \ No newline at end of file diff --git a/BlazorGrpcWebCodeFirst/Server/BlazorGrpcWebCodeFirst.Server.csproj b/BlazorGrpcWebCodeFirst/Server/BlazorGrpcWebCodeFirst.Server.csproj index 9a5bb85..1663714 100644 --- a/BlazorGrpcWebCodeFirst/Server/BlazorGrpcWebCodeFirst.Server.csproj +++ b/BlazorGrpcWebCodeFirst/Server/BlazorGrpcWebCodeFirst.Server.csproj @@ -6,7 +6,9 @@ + + diff --git a/BlazorGrpcWebCodeFirst/Server/Services/MyService.cs b/BlazorGrpcWebCodeFirst/Server/Services/MyService.cs new file mode 100644 index 0000000..0da95cc --- /dev/null +++ b/BlazorGrpcWebCodeFirst/Server/Services/MyService.cs @@ -0,0 +1,21 @@ +using BlazorGrpcWebCodeFirst.Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlazorGrpcWebCodeFirst.Server.Services +{ + public class MyService : IMyService + { + public Task DoSomething(MyServiceRequest request) + { + return Task.FromResult(new MyServiceResult() + { + NewText = request.Text + " from server", + NewValue = request.Value + 1 + }); + } + } +} diff --git a/BlazorGrpcWebCodeFirst/Server/Startup.cs b/BlazorGrpcWebCodeFirst/Server/Startup.cs index e6e111d..86fc122 100644 --- a/BlazorGrpcWebCodeFirst/Server/Startup.cs +++ b/BlazorGrpcWebCodeFirst/Server/Startup.cs @@ -6,6 +6,9 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System.Linq; +using ProtoBuf.Grpc.Server; +using BlazorGrpcWebCodeFirst.Server.Services; +using BlazorGrpcWebCodeFirst.Shared; namespace BlazorGrpcWebCodeFirst.Server { @@ -22,6 +25,14 @@ public Startup(IConfiguration configuration) // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { + services.AddCodeFirstGrpc(config => + { + config.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal; + }); + services.AddGrpcWeb(options => + { + options.GrpcWebEnabled = true; + }); services.AddControllersWithViews(); } @@ -47,8 +58,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseRouting(); + app.UseGrpcWeb(); + app.UseEndpoints(endpoints => { + endpoints.MapGrpcService(); + endpoints.MapControllers(); endpoints.MapFallbackToFile("index.html"); }); diff --git a/BlazorGrpcWebCodeFirst/Shared/BlazorGrpcWebCodeFirst.Shared.csproj b/BlazorGrpcWebCodeFirst/Shared/BlazorGrpcWebCodeFirst.Shared.csproj index d4c395e..7e24c3a 100644 --- a/BlazorGrpcWebCodeFirst/Shared/BlazorGrpcWebCodeFirst.Shared.csproj +++ b/BlazorGrpcWebCodeFirst/Shared/BlazorGrpcWebCodeFirst.Shared.csproj @@ -1,7 +1,11 @@ - + netstandard2.1 + + + + diff --git a/BlazorGrpcWebCodeFirst/Shared/IMyService.cs b/BlazorGrpcWebCodeFirst/Shared/IMyService.cs new file mode 100644 index 0000000..bacf50f --- /dev/null +++ b/BlazorGrpcWebCodeFirst/Shared/IMyService.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.ServiceModel; +using System.Text; +using System.Threading.Tasks; + +namespace BlazorGrpcWebCodeFirst.Shared +{ + [ServiceContract] + public interface IMyService + { + Task DoSomething(MyServiceRequest request); + + } +} diff --git a/BlazorGrpcWebCodeFirst/Shared/MyServiceRequest.cs b/BlazorGrpcWebCodeFirst/Shared/MyServiceRequest.cs new file mode 100644 index 0000000..9429d08 --- /dev/null +++ b/BlazorGrpcWebCodeFirst/Shared/MyServiceRequest.cs @@ -0,0 +1,14 @@ +using System.Runtime.Serialization; + +namespace BlazorGrpcWebCodeFirst.Shared +{ + [DataContract] + public class MyServiceRequest + { + [DataMember(Order = 1)] + public string Text { get; set; } + + [DataMember(Order = 2)] + public int Value { get; set; } + } +} \ No newline at end of file diff --git a/BlazorGrpcWebCodeFirst/Shared/MyServiceResult.cs b/BlazorGrpcWebCodeFirst/Shared/MyServiceResult.cs new file mode 100644 index 0000000..1d27e0a --- /dev/null +++ b/BlazorGrpcWebCodeFirst/Shared/MyServiceResult.cs @@ -0,0 +1,14 @@ +using System.Runtime.Serialization; + +namespace BlazorGrpcWebCodeFirst.Shared +{ + [DataContract] + public class MyServiceResult + { + [DataMember(Order = 1)] + public string NewText { get; set; } + + [DataMember(Order = 2)] + public int NewValue { get; set; } + } +} \ No newline at end of file