简体   繁体   English

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

[英]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中找到。

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).我没有提到我在 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.

相关问题 如何将 C# .net 框架项目更改为 .net 核心 3.0 项目? [等候接听] - How can I change a C# .net framework project to a .net core 3.0 project? [on hold] C#WebApi-ASP Net Core 2-如何以编程方式将csproj名称放入路由? - C# WebApi - Asp Net Core 2 - How can I put the csproj name into the route programmatically? 我如何为 c# Visual Studio 解决方案编译 Grpc.Core.dll 表格 https://github.com/grpc/grpc - How can I complile Grpc.Core.dll form https://github.com/grpc/grpc for c# Visual Studio solution 如何在.NET Core 3.0中启用gRPC的日志记录 - How to enable logging for gRPC in .NET Core 3.0 在 Net Core 3.0 中同时使用 GRPC 通道 - An using GRPC Channel concurrently in Net Core 3.0 Sharepoint 在线/365 集成(上传文件)在 Azure 和 WebApi C#.Net Core 2.2 上托管的 React 应用程序上 - Sharepoint online/365 integration (To upload files) on React app hosted on Azure and WebApi C# .Net Core 2.2 何时应该使用 Pipes 或 gRPC 进行进程间通信(在 C# .NET Core 中)? - When should I use Pipes or gRPC for interprocess communication (in C# .NET Core)? 无法将 c#.Net Core 3.0 与 directx 9.0 依赖项链接 - Can't link c# .Net Core 3.0 with directx 9.0 dependency 在 .net 控制台应用程序中调用 external.Net C# webapi,但每当我尝试连接时,连接都会出错 - Call an external .Net C# webapi in .net Console app, but the connection gives an error whenever I trty to Connect 如何使用C#WebAPI .NET Core实体框架修复Odata - How to fix Odata with C# WebAPI .NET Core Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM