[英]Can I combine a gRPC and webapi app into a .NET Core 3.0 in C#?
I am using dot.net core 3.0.我正在使用 dot.net 核心 3.0。
I have gRPC app.我有 gRPC 应用程序。 I am able to communicate to it through gRPC protocol.
我可以通过 gRPC 协议与它通信。
I thought my next step would be add some restful API support.我想我的下一步是添加一些 restful API 支持。 I modified my startup class to add controllers, routing etc..... When I try navigating to the API using a browser, I get an error "ERR_INVALID_HTTP_RESPONSE" no matter which protocol (http/https) and port I use.
我修改了我的启动 class 以添加控制器、路由等......当我尝试使用浏览器导航到 API 时,无论我使用哪种协议 (http/https) 和端口,我都会收到错误“ERR_INVALID_HTTP_RESPONSE”。 gRPC should be using 5001 and webapi using 8001.
gRPC 应该使用 5001 和 webapi 使用 8001。
heres my startup class:这是我的启动 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();
});
}
}
And my controller:还有我的 controller:
[ApiController]
[Route("[controller]")]
public class AdminController : ControllerBase
{
[HttpGet] public string Get()
{ return "hello"; }
}
Any thoughts?有什么想法吗?
Thnx谢谢
EDIT : the entire project can be found at this repo .编辑:整个项目可以在这个 repo中找到。
I found the solution.我找到了解决方案。 I didn't mention I was running on MacOS and using Kestrel (and it appears the combination of MacOS AND Kestrel is the problem).
我没有提到我在 MacOS 上运行并使用 Kestrel(看起来 MacOS 和 Kestrel 的组合是问题所在)。 I apologize for that missing information.
我为丢失的信息道歉。
The solution is similar to what is here .解决方案类似于这里的解决方案。 I had to add a call to
options.ListenLocalhost
for the webapi port.我必须为 webapi 端口添加对
options.ListenLocalhost
的调用。
here's the code:这是代码:
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>();
});
}
}
Thnx谢谢
Another solution is configure Kestrel parameter on appsettings.json
.另一种解决方案是在
appsettings.json
上配置 Kestrel 参数。 It work with .Net Core 3.1 too.它也适用于.Net Core 3.1 。
Edit appsettings.json
and set Endpoints with WebApi and gRPC with your custom name.编辑
appsettings.json
并使用您的自定义名称设置 Endpoints with WebApi 和 gRPC。 You don't need change the Program.cs
.您不需要更改
Program.cs
。
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"WebApi": {
"Url": "http://localhost:5001",
"Protocols": "Http1"
},
"gRPC": {
"Url": "http://localhost:11837",
"Protocols": "Http2"
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.