.Net6 WebAPI 手动开启gRPC服务

文章目录

  • .Net6 WebAPI 手动开启gRPC服务
    • 引用包
    • 开启服务
    • 配置gRPC通道
    • 完整代码

.Net6 WebAPI 手动开启gRPC服务

引用包

开启服务

  // Add services to the container.
            builder.Services.AddGrpc();

配置gRPC通道

 // Configure the HTTP request pipeline.
            app.MapGrpcService();

完整代码

 public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
        

            // Add services to the container. 
            builder.Services.AddControllers();

            // Add services to the container.
            builder.Services.AddGrpc();

    


            builder.Services.AddSignalR();

            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

 

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            //if (app.Environment.IsDevelopment())
            //{
            app.UseSwagger();
            app.UseSwaggerUI();
            //}

            app.UseStaticFiles();

            app.UseHttpsRedirection();

            app.UseAuthorization();


            app.MapControllers();

          

            // Configure the HTTP request pipeline.
            app.MapGrpcService();
           

            app.Run();
        }
    }

以上内容完成以后,按照上一篇文章http://t.csdn.cn/M6nZV进行配置协议和服务类就可以用了。

你可能感兴趣的:(分布式,.net,c#,rpc)