如何在?ASP.NET?Core?中創(chuàng)建?gRPC?客戶端和服務(wù)器
前言
gRPC 是一種高性能、開源的遠(yuǎn)程過程調(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)多進(jìn)程代碼示例
Python中大部分情況下都需要使用多進(jìn)程,Python中提供了multiprocessing這個包實(shí)現(xiàn)多進(jìn)程。multiprocessing支持子進(jìn)程、進(jìn)程間的同步與通信,本文就詳細(xì)的介紹一下2018-10-10
Python實(shí)現(xiàn)快速排序算法及去重的快速排序的簡單示例
quick sort快速排序是一種再基礎(chǔ)不過的排序算法,使用Python代碼寫起來相當(dāng)簡潔,這里我們就來看一下Python實(shí)現(xiàn)快速排序算法及去重的快速排序的簡單示例:2016-06-06
Python中使用select模塊實(shí)現(xiàn)非阻塞的IO
這篇文章主要介紹了Python中使用select模塊實(shí)現(xiàn)非阻塞的IO,本文使用一個簡單聊天室程序講解Python中的select模塊使用,需要的朋友可以參考下2015-02-02
關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解
今天小編就為大家分享一篇關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python實(shí)戰(zhàn)之屏幕錄制功能的實(shí)現(xiàn)
屏幕錄制,即屏幕捕獲,是指將計(jì)算機(jī)屏幕上的活動記錄下來,生成視頻文件,本文 主要為大家介紹了如何使用Python實(shí)現(xiàn)這一功能,希望對大家有所幫助2025-03-03

