我可以将gRPC和webapi应用程序组合到C#中的.NET Core 3.0吗? [英] Can I combine a gRPC and webapi app into a .NET Core 3.0 in C#?
问题描述
我正在使用点网核心3.0。
我有gRPC应用程序。我可以通过gRPC协议与之通信。
我认为下一步是添加一些宁静的API支持。我修改了启动类以添加控制器,路由等。...当我尝试使用浏览器导航到API时,无论使用哪种协议(http / https)和端口,都会出现错误 ERR_INVALID_HTTP_RESPONSE。 gRPC应该使用5001,而webapi使用8001。
这是我的启动类:
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(端点=>
{
端点.MapGrpcService< BootNodeService>();
端点.MapControllers();
});
}
}
和我的控制器:
[ApiController]
[Route( [controller])]
公共类AdminController:ControllerBase
{
[HttpGet]公共字符串Get()
{return hello; }
}
有什么想法吗?
Thnx
编辑:可以在
我找到了解决方案。我没有提到我在MacOS上运行并使用Kestrel(这似乎是MacOS和Kestrel的组合)。对于这些丢失的信息,我深表歉意。
该解决方案类似于此处。我必须添加对 options.ListenLocalhost
的webapi端口的调用。
这是代码:
公共类程序
{
public static void Main(string [] args)
{
IHostBuilder hostBuilder = CreateHostBuilder(args);
IHost主机= hostBuilder.Build();
host.Run();
}
//要在macOS上成功运行gRPC,需要进行其他配置。
//有关如何在macOS上配置Kestrel和gRPC客户端的说明,请访问https://go.microsoft.com/fwlink/?linkid=2099682
公共静态IHostBuilder CreateHostBuilder(string [] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(options =>
{
options.ListenLocalhost (5001,o => o.Protocols =
HttpProtocols.Http2);
//添加此行以解决问题
options.ListenLocalhost(11837,o => ; o.Protocols =
HttpProtocols.Http1);
});
webBuilder.UseStartup< Startup>();
});
}
}
Thnx
I am using dot net core 3.0.
I have gRPC app. I am able to communicate to it through gRPC protocol.
I thought my next step would be add some restful API support. 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. gRPC should be using 5001 and webapi using 8001.
heres my startup 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:
[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.
EDIT: view of screen
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). 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.
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
这篇关于我可以将gRPC和webapi应用程序组合到C#中的.NET Core 3.0吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!