ASP.NET Core gRPC
首先要下载 Visual Studio 预览版 和 .NET Core 3.0v SDK。
如果不想安装 VS 预览版,可以参考 .NET Core tooling update for Visual Studio 2017 version 15.9 启用预览版的 .NET Core SDK。(选项位置:Tools > Options > Projects and Solutions > .NET Core: > Use Previews of the .NET Core SDK)
具体的教程参考下面的官方文档:
教程:开始使用 ASP.NET Core 中的 gRPC 服务
简单来说就是创建一个 ASP.NET Core Web Application => gRPC 服务 模板项目。
直接使用该默认项目即可。其代码如下:依赖项:
- Google.Protobuf -Version 3.7.0
- Grpc.AspNetCore.Server -Version 0.1.20-pre1
- Grpc.Tools -Version 1.20.0-pre3
Program.cs
csharpusing System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; namespace FirstGrpc { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25Startup.cs
csharpusing System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace FirstGrpc { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddGrpc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { // Communication with gRPC endpoints must be made through a gRPC client. // To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909 endpoints.MapGrpcService<GreeterService>(); }); } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39Protos/greet.proto
protosyntax = "proto3"; package Greet; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings. message HelloReply { string message = 1; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19Services/GreeterService.cs
csharpusing System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Greet; using Grpc.Core; namespace FirstGrpc { public class GreeterService : Greeter.GreeterBase { public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20appsettings.json
json{ "Logging": { "LogLevel": { "Default": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "Kestrel": { "EndpointDefaults": { "Protocols": "Http2" } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14暂时还么有 gRPC 客户端的模板,需要创建 控制台项目,然后手动添加 NuGet 包。
powershellInstall-Package Grpc.Core Install-Package Google.Protobuf Install-Package Grpc.Tools
1
2
3复制 Protos\greet.proto 到客户端项目后,需手动修改工程文件。
右键点击工程,选择 编辑项目文件 选项。
在 Project 下新增一个 ItemGroup。
xml<ItemGroup> <Protobuf Include="Protos\greet.proto" GrpcServices="Client" /> </ItemGroup>
1
2
3保存后重新加载项目。
修改 Program.cs 中的
main
方法。csharpusing Greet; using Grpc.Core; using System; using System.Threading.Tasks; namespace FirstGrpcClient { class Program { static async Task Main(string[] args) { // The port number here must match the port of the gRPC server var channel = new Channel("localhost:50051", ChannelCredentials.Insecure); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" }); Console.WriteLine("Greeting: " + reply.Message); await channel.ShutdownAsync(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25分别启动两个项目后,客户端程序打印结果如下:
bashGreeting: Hello GreeterClient Press any key to exit...
1
2官方的 示例代码
项目结构