如何在?ASP.NET?Core?中創(chuàng)建?gRPC?客戶端和服務(wù)器
前言
gRPC 是一種高性能、開(kāi)源的遠(yuǎn)程過(guò)程調(diào)用(RPC)框架,它基于 Protocol Buffers(protobuf)定義服務(wù),并使用 HTTP/2 協(xié)議進(jìn)行通信。
新建項(xiàng)目
新建解決方案GrpcDemo
新建webapi項(xiàng)目GrpcServer作為grpc服務(wù)端項(xiàng)目
添加包
<PackageReference Include="Grpc.AspNetCore" Version="2.67.0" /> <PackageReference Include="Grpc.Tools" Version="2.67.0">
新建文本文件greeter.proto
syntax = "proto3"; option csharp_namespace = "GrpcServer"; 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; }
編輯GrpcServer項(xiàng)目文件,添加
新建類GreeterService.cs
using Grpc.Core; namespace GrpcServer { public class GreeterService : Greeter.GreeterBase { private readonly ILogger<GreeterService> _logger; public GreeterService(ILogger<GreeterService> logger) { _logger = logger; } public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); } } }
修改Program.cs
using GrpcServer; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddGrpc(); var app = builder.Build(); app.MapGrpcService<GreeterService>(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
就是添加下面兩行代碼
builder.Services.AddGrpc(); app.MapGrpcService<GreeterService>();
新建grpc客戶端項(xiàng)目GrpcClient
添加包
<PackageReference Include="Google.Protobuf" Version="3.28.3" /> <PackageReference Include="Grpc.Net.Client" Version="2.67.0" /> <PackageReference Include="Grpc.Tools" Version="2.67.0">
復(fù)制服務(wù)器端端的greeter.proto到客戶端項(xiàng)目
編輯GrpcClient項(xiàng)目文件,加
編輯Program.cs文件
using Grpc.Net.Client; using GrpcClient; using var channel = GrpcChannel.ForAddress("https://localhost:7052"); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync( new HelloRequest { Name = "wxy" }); Console.WriteLine("Greeting: " + reply.Message); Console.WriteLine("Press any key to exit..."); Console.ReadKey();
7052改成你的服務(wù)器端運(yùn)行端口
結(jié)果展示
運(yùn)行服務(wù)器端
運(yùn)行客戶端
到此這篇關(guān)于在 ASP.NET Core 中創(chuàng)建 gRPC 客戶端和服務(wù)器的文章就介紹到這了,更多相關(guān)ASP.NET Core gRPC 客戶端和服務(wù)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)多進(jìn)程代碼示例
Python中大部分情況下都需要使用多進(jìn)程,Python中提供了multiprocessing這個(gè)包實(shí)現(xiàn)多進(jìn)程。multiprocessing支持子進(jìn)程、進(jìn)程間的同步與通信,本文就詳細(xì)的介紹一下2018-10-10python判斷變量是否是None的三種寫(xiě)法總結(jié)
代碼中經(jīng)常會(huì)有變量是否為None的判斷,這篇文章給大家總結(jié)了三種判斷變量是否是none的寫(xiě)法,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12Python實(shí)現(xiàn)快速排序算法及去重的快速排序的簡(jiǎn)單示例
quick sort快速排序是一種再基礎(chǔ)不過(guò)的排序算法,使用Python代碼寫(xiě)起來(lái)相當(dāng)簡(jiǎn)潔,這里我們就來(lái)看一下Python實(shí)現(xiàn)快速排序算法及去重的快速排序的簡(jiǎn)單示例:2016-06-06Python中使用select模塊實(shí)現(xiàn)非阻塞的IO
這篇文章主要介紹了Python中使用select模塊實(shí)現(xiàn)非阻塞的IO,本文使用一個(gè)簡(jiǎn)單聊天室程序講解Python中的select模塊使用,需要的朋友可以參考下2015-02-02關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解
今天小編就為大家分享一篇關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python實(shí)戰(zhàn)之屏幕錄制功能的實(shí)現(xiàn)
屏幕錄制,即屏幕捕獲,是指將計(jì)算機(jī)屏幕上的活動(dòng)記錄下來(lái),生成視頻文件,本文 主要為大家介紹了如何使用Python實(shí)現(xiàn)這一功能,希望對(duì)大家有所幫助2025-03-03