gpt4 book ai didi

c# - 我可以在 C# 中将 gRPC 和 webapi 应用程序组合到 .NET Core 3.0 中吗?

转载 作者:行者123 更新时间:2023-12-03 18:34:19 29 4
gpt4 key购买 nike

我正在使用点网核心 3.0。

我有 gRPC 应用程序。我能够通过 gRPC 协议(protocol)与之通信。

我认为我的下一步将是添加一些 Restful API 支持。我修改了我的启动类以添加 Controller 、路由等......当我尝试使用浏览器导航到 API 时,无论我使用哪种协议(protocol) (http/https) 和端口,我都会收到错误“ERR_INVALID_HTTP_RESPONSE”。 gRPC 应该使用 5001,webapi 应该使用 8001。

这是我的启动类(class):

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
services.AddControllers();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();

app.UseRouting();
app.UseHttpsRedirection();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<BootNodeService>();
endpoints.MapControllers();

});
}
}

我的 Controller :
[ApiController]
[Route("[controller]")]
public class AdminController : ControllerBase
{
[HttpGet] public string Get()
{ return "hello"; }
}

有什么想法吗?

谢谢

编辑 : 整个项目可以在 this repo 找到.

编辑 : 屏幕 View
enter image description here

最佳答案

我找到了解决方案。我没有提到我在 MacOS 上运行并使用 Kestrel(看起来 MacOS 和 Kestrel 的组合是问题所在)。我为丢失的信息道歉。

解决方案类似于 here .我必须给 options.ListenLocalhost 添加一个电话对于 webapi 端口。

这是代码:

public class Program
{
public static void Main(string[] args)
{
IHostBuilder hostBuilder = CreateHostBuilder(args);
IHost host = hostBuilder.Build();
host.Run();
}

// 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
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(options =>
{
options.ListenLocalhost(5001, o => o.Protocols =
HttpProtocols.Http2);

// ADDED THIS LINE to fix the problem
options.ListenLocalhost(11837, o => o.Protocols =
HttpProtocols.Http1);
});
webBuilder.UseStartup<Startup>();
});
}
}

谢谢

关于c# - 我可以在 C# 中将 gRPC 和 webapi 应用程序组合到 .NET Core 3.0 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58649775/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com