.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
讀取配置文件
appsettings.json中定義json結構模塊來映射到Model,免去了本身讀取出來轉實體的麻煩。可是呢,兩邊的名字必須是要同樣的。
具體參考:這裏git
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