await context.Response.WriteAsync("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");
});
});
复制代码
完整的 Program.cs 文件如下:
using MyFirstMagicOnionServer.Services;
var builder = WebApplication.CreateBuilder(args);
// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682
// Add services to the container.
builder.Services.AddGrpc();
builder.Services.AddMagicOnion();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>();
app.UseRouting() // 添加 UseRouting
app.UseEndpoints(endpoints =>
{
endpoints.MapMagicOnionService();
app.MapGet("/", async context =>
{
await context.Response.WriteAsync("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");
});
});
app.Run();
复制代码
ps:这边有个坑,运行之后会报一个错误:EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware. Please add EndpointRoutingMiddleware by calling 'IApplicationBuilder.UseRouting' inside the call to 'Configure(...)' in the application startup code. 意思就是说要在app.UseEndpoints()方法前面加上app.UseRouting() 方法
在RPC服务端项目上使用MagicOnion
MagicOnion提供类似Web API的RPC服务和用于实时通信的StreamingHub。本节实现类似于 Web API 的 RPC 服务。添加要在服务器和客户端之间共享的接口(命名空间应与项目匹配)。
创建共享库(共享库,客户端服务端都要有)
创建一个 Shared 文件夹 里面再创建一个接口文件 IMyFirstService
using MagicOnion;
namespace MyFirstMagicOnionServer.Shared
{
// 将 .NET 接口定义为 Server/Client IDL.
// 这接口在服务器和客户端之间共享
public interface IMyFirstService : IService<IMyFirstService>
{
// 该返回的类型必须是 UnaryResult<T>
UnaryResult<int> SumAsync (int x,int y);
}
}
复制代码
添加实现接口的类(服务端实现)
创建一个 Service 文件夹里面再创建一个实现类 MyFirstMagicOnionServer
using MagicOnion;
using MagicOnion.Server;
using MyFirstMagicOnionServer.Shared;
namespace MyFirstMagicOnionServer.Services
{
public class MyFirstService : ServiceBase<IMyFirstService>, IMyFirstService
{
public async UnaryResult<int> SumAsync(int x, int y)
await context.Response.WriteAsync("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");
});
});
复制代码
附带完整的 Program.cs
using MagicOnion;using MagicOnion.Server;var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddControllers();// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbucklebuilder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();#region 添加的 MagicOnionbuilder.Services.AddGrpc();builder.Services.AddMagicOnion();#endregionvar app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){ app.UseSwagger(); app.UseSwaggerUI();}app.UseHttpsRedirection();app.UseRouting(); // 调用UseRouting()app.UseAuthorization();// 添加的app.UseEndpoints(endpoints =>
{
endpoints.MapMagicOnionService();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("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");