本文介绍了我可以在 C# 中将 gRPC 和 webapi 应用程序组合到 .NET Core 3.0 中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 dot net core 3.0.

我有 gRPC 应用程序.我可以通过 gRPC 协议与它通信.

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

这是我的创业班:

公共类启动{public void ConfigureServices(IServiceCollection 服务){服务.AddGrpc();services.AddControllers();}公共无效配置(IApplicationBuilder 应用程序,IWebHostEnvironment 环境){如果 (env.IsDevelopment())app.UseDeveloperExceptionPage();app.UseRouting();app.UseHttpsRedirection();app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapGrpcService();端点.MapControllers();});}}

还有我的控制器:

[ApiController][路线([控制器]")]公共类 AdminController : ControllerBase{[HttpGet] 公共字符串 Get(){返回你好";}}

有什么想法吗?

谢谢

编辑:整个项目可以在

解决方案

我找到了解决方案.我没有提到我在 MacOS 上运行并使用 Kestrel(似乎 MacOS 和 Kestrel 的组合是问题所在).对于缺少的信息,我深表歉意.

解决方案类似于此处.我不得不为 webapi 端口添加对 options.ListenLocalhost 的调用.

代码如下:

公共类程序{public static void Main(string[] args){IHostBuilder hostBuilder = CreateHostBuilder(args);IHost 主机 = hostBuilder.Build();主机.运行();}//需要额外的配置才能在 macOS 上成功运行 gRPC.//有关如何在 macOS 上配置 Kestrel 和 gRPC 客户端的说明,请访问 https://go.microsoft.com/fwlink/?linkid=2099682公共静态 IHostBuilder CreateHostBuilder(string[] args) =>Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{webBuilder.ConfigureKestrel(选项=>{options.ListenLocalhost(5001, o => o.Protocols =HttpProtocols.Http2);//添加此行以解决问题options.ListenLocalhost(11837, o => o.Protocols =HttpProtocols.Http1);});webBuilder.UseStartup();});}}

谢谢

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

这篇关于我可以在 C# 中将 gRPC 和 webapi 应用程序组合到 .NET Core 3.0 中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:16