swagger无法与gRPC .net核心7一起使用

gijlo24d  于 2022-11-23  发布在  .NET
关注(0)|答案(1)|浏览(196)

我按照这个文档的步骤https://learn.microsoft.com/en-us/aspnet/core/grpc/json-transcoding-openapi?view=aspnetcore-7.0和集成的谷歌协议。我试图在本地打开它,并添加/swagger/index. html,我得到
HTTP/1.x请求已发送到仅限HTTP/2的终结点。
Program.cs:

using Extensions;
using Google.Api;
using gRPCserver.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.OpenApi.Models;
using Models.Models;
using Serilog;
using Serilog.Sinks.Kafka;
using sKashCallCenterAPI.Interface;
using sKashCallCenterAPI.Service;

var builder = WebApplication.CreateBuilder(args);

var ConnectionString = builder.Configuration["sqlconnection:ConnectionString"];
builder.Services.AddDbContext<sKashDbContext>(options =>
{
    options.UseSqlServer(ConnectionString);
});

//builder.Services.AddGrpc();
builder.Services.AddGrpcSwagger();

builder.Services.AddGrpcHttpApi();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1",
        new OpenApiInfo { Title = "gRPC Server", Version = "v1" });

    var filePath = Path.Combine(System.AppContext.BaseDirectory, "gRPCserver.xml");
    c.IncludeXmlComments(filePath);
    c.IncludeGrpcXmlComments(filePath, includeControllerXmlComments: true);
});


ConfigurationManager configuration = builder.Configuration;
var kafkaServer = configuration["KafkaConfig:ServerIP"] + ":" + configuration["KafkaConfig:Port"];
builder.Host.UseSerilog((ctx, lc) => lc
    .WriteTo.Console()
    .WriteTo.Kafka(topic: configuration["KafkaConfig:Topic"], bootstrapServers: kafkaServer)
    .Enrich.WithProperty("Source", configuration["KafkaConfig:Source"])
    .ReadFrom.Configuration(ctx.Configuration));

builder.Services.ConfigurJWTAuthentication();
builder.Services.ConfigureRepositoryWrapper();
builder.Services.ConfigureContractsServices();

builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<IMemoryCache, MemoryCache>();

var app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});

app.UseRouting();

// Configure the HTTP request pipeline.
//app.MapGrpcService<GreeterService>();
app.UseEndpoints(endpoints =>
{
    endpoints.MapGrpcService<GreeterService>();
});
app.MapGet("/", () => "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();

项目正在运行,但未显示任何Swagger文档。

xxhby3vn

xxhby3vn1#

是的,你是对的。swagger文档不会默认显示。你必须使用grpc-gateway插件。这是一个反向代理服务器,它通过阅读protobuf服务定义将RESTful HTTP API转换为gRPC。

相关问题