Net Core 3.0 尝鲜指南

  1. swagger

.Net Core 3.0中的swagger,必须引用5.0.0 及以上版本。能够在Nuget官网查看版本。目前最新版本(2019-9-25) 5.0.0rc3html

Install-Package Swashbuckle.AspNetCore.Swagger -Version 5.0.0-rc3
Install-Package Swashbuckle.AspNetCore.SwaggerUi -Version 5.0.0-rc3
  1. 读取配置文件
    appsettings.json中定义json结构模块来映射到Model,免去了本身读取出来转实体的麻烦。可是呢,两边的名字必须是要同样的。
    具体参考:这里git

  2. gRPC
    具体参考:这里github

也可看个人demo:这里json

public void ConfigureServices(IServiceCollection services)
{
    //添加grpc
    services.AddGrpc();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        //绑定mapping
        endpoints.MapGrpcService<GreeterService>();
        endpoints.MapGrpcService<FirstTestService>();
        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");
        });
    });
}

first.proto:app

//指定使用pb3
syntax = "proto3";

option csharp_namespace = "Core3Study.GRpc";

package First;

// 定义服务
service FirstTest {
// Sends a greeting
rpc GetCacheValue (Request) returns (Reply);
}


message Request {
string key = 1;
}


message Reply {
string value = 1;
}

service的命名:.proto中service的名字,好比:firstTest,则service名为 FirstTestService,继承FirstTest.FirstTestBase.
都是自动生成的,前提是,须要在项目文件中添加async

<ItemGroup>
    <Protobuf Include="Protos\first.proto" GrpcServices="Server" />
</ItemGroup>

的引用。ide

public override Task<Reply> GetCacheValue(Request request, ServerCallContext context)
{
    return Task.FromResult(new Reply()
    {
        Value = $"response {Cats[Rand.Next(0, Cats.Count)]}"
    });
}

以上代码是根据.proto中定义的建立。visual-studio

ServerCallContext相似于HttpContext,提供上下文的一些信息。ui

相关文章
相关标签/搜索