Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2ConnectionErrorException

Aleksej_Shherbak

I have the following exception

Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2ConnectionErrorException: HTTP/2 connection error (PROTOCOL_ERROR): Invalid HTTP/2 connection preface.

I will describe my conditions. I have very simple grpc .Net Core project and I want make HTTP endpoint.

Here is Startup.cs

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options => options.EnableEndpointRouting = false);
        services.AddGrpc();

        services.AddHttpClient<IAnalyticApiAsker, AnalyticApiAsker>();

        // db context
        services.AddSingleton<IApplicationMongoContext, ApplicationMongoContext>();

        // repos
        services.AddTransient<IWorkspacesRepo, WorkspaceRepo>();
        services.AddTransient<IApplicationRepo, ApplicationRepo>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            // Communication with gRPC endpoints must be made through a gRPC client.
            // To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909
            endpoints.MapGrpcService<ApplicationsService>();
            endpoints.MapGrpcService<WorkspacesService>();
        });

        // I think I have to use this middleware for get http endpoints ability ?? Right? 
        app.UseMvc();
    }
}

And Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

launchSettings.json

{
  "profiles": {
    "Dashboard": {
      "commandName": "Project",
      "launchBrowser": false,
      "applicationUrl": "http://*:50053",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Local"
      }
    }
  }
}

For the endpoint I have the following controller:

public class HealthController : Controller
{ 
    [Route("/healthcheck")]
    [HttpGet]
    public IActionResult Index()
    {
        return Ok(new
        {
            status = "ok",
            description = "works"
        });
    }
}

But when I request from a browser my application's URL http://localhost:50053/healthcheck I have this exception. it looks like Kestrel configured only for http2. If it's true, how to switch on HTTP handling?

Update

Ok, after some researching I have understood the following. It's impossible to switch on both protocol types handling: grpc (that works with HTTP 2) and ordinary http1 at the same time. Am I right?

I have modified my Startapp.cs to:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureKestrel(options =>
            {
                options.ListenLocalhost(50053, listenOptions =>
                {
                    listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
                });
            })
            .UseStartup<Startup>();

HttpProtocols.Http1AndHttp2 this property has variant for each HTTP type (Http1, Http, Http1AndHttp2). But it does not work for GRPC. I think it's happening because of whether to use http1 to GRPC or http2. And I don't know how to set up it.

So, listenOptions.Protocols = HttpProtocols.Http1 - works for web, listenOptions.Protocols = HttpProtocols.Http2 - works for GRPC and listenOptions.Protocols = HttpProtocols.Http1AndHttp2 - works for web.

Update 2

I'm not sure if it answers or not, but I have solved the task with the help of the following Program.cs modification:

    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureKestrel(options =>
            {
                options.ListenLocalhost(80, listenOptions =>
                {
                    listenOptions.Protocols = HttpProtocols.Http1;
                });

                options.ListenLocalhost(50053, listenOptions =>
                {
                    listenOptions.Protocols = HttpProtocols.Http2;
                });
            })
            .UseStartup<Startup>();
}

Now I can work with http1 and http2. But now I have a new problem, it works only on localhost.

Aleksej_Shherbak

Ok, I have solved it with the help of the following Program.cs modification:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureKestrel(options =>
            {
                options.ListenAnyIP(80, listenOptions =>
                {
                    listenOptions.Protocols = HttpProtocols.Http1;
                });

                options.ListenAnyIP(50053, listenOptions =>
                {
                    listenOptions.Protocols = HttpProtocols.Http2;
                });
            })
            .UseStartup<Startup>();
}

Now I can work with GRPC and http1 in the same time, and connection allowed for any IP.

I hope it will useful for somebody who will add http1 to GRPC configured project.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to enable http/2 in Asp.net core 2.2 kestrel (self hosted) on server 2016

分類Dev

.Net Core2.xでのMicrosoft.AspNetCore.Hosting.WindowsServicesの使用

分類Dev

IFormFileは、アセンブリ 'Microsoft.AspNetCore.Http3.0'からタイプMicrosoft.AspNetCore.Http.Internal.FormFile 'をロードしません

分類Dev

AspNet Core2タイプ「Microsoft.AspNetCore.Http.HttpContext」セッションのサービスがありません

分類Dev

.Net Core2.2でMicrosoft.AspNetCore.Mvc.Internal.ActionContext.GetNormalizedRouteValueを解決する方法

分類Dev

System.TypeLoadException: 'メソッド' GetItem'in type 'Microsoft.AspNetCore.Mvc.Razor.Internal.FileProviderRazorProjectFileSystem'in asp.net core

分類Dev

Golang TCP server: how to write HTTP2 data

分類Dev

Where can I find the ASP.NET Core 2 source code? Specifically for Microsoft.AspNetCore.Authentication.OpenIdConnect

分類Dev

AspNetCore (Kestrel) request timed out

分類Dev

Http2 not working with express

分類Dev

Microsoft.AspNetCore.Http.HttpRequestをHttpRequestMessageに変換します

分類Dev

Why do I get this InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.RequestDelegate'?

分類Dev

microsoft.aspnetcore.signalr.clientとmicrosoft.aspnetcore.signalr.client.coreの違いは何ですか?

分類Dev

Microsoft.AspNetCore.Hosting.Internal.WebHostおよびlog4net.LogicalThreadContext.Properties ["requestid"]

分類Dev

Microsoft.AspNetCore.Hosting.Internal.WebHostおよびlog4net.LogicalThreadContext.Properties ["requestid"]

分類Dev

プレーンhttpを介してC#Kestrel Webサーバーでhttp2を有効にするにはどうすればよいですか?

分類Dev

.NET Core 2でMicrosoft.AspNetCore.Allを使用すると、どのような問題が発生する可能性がありますか?

分類Dev

.NET Core 2で定義されているMicrosoft.AspNetCore.Antiforgeryライブラリはどこにありますか?

分類Dev

Optimizing File Cacheing and HTTP2

分類Dev

'Microsoft.AspNetCore.Http.IFormFile.Headers'のコレクションタイプ 'Microsoft.AspNetCore.Http.IHeaderDictionary'はサポートされていません

分類Dev

Microsoft.AspNetCore.TestHostに含まれているTestServerを使用せずにテスト用にKestrelを実行する

分類Dev

.NET Microsoft.AspNetCore.Http.ExtensionsUriHelper.GetDisplayUrlが無効なURIを返します

分類Dev

ASP.NET Core 2.xのKestrelでHTTPS / SSLを使用する方法

分類Dev

メソッドが見つかりません: 'Microsoft.Extensions.Primitives.StringValues Microsoft.AspNetCore.Http.IQueryCollection.get_Item(System.String)

分類Dev

.NET Core adding client certificate to POST to Kestrel REST API on Linux fails server cert validation

分類Dev

Net Core 3でServer:Kestrelヘッダーを削除します

分類Dev

Getting Microsoft.AspNetCore.Hosting.Diagnostics[6] Application startup exception in .NET Core 3

分類Dev

How to intercept 500 internal server error in WSO2 ESB

分類Dev

Microsoft.AspNet.Identity.CoreとMicrosoft.AspNetCore.Identityの違いは何ですか?

Related 関連記事

  1. 1

    How to enable http/2 in Asp.net core 2.2 kestrel (self hosted) on server 2016

  2. 2

    .Net Core2.xでのMicrosoft.AspNetCore.Hosting.WindowsServicesの使用

  3. 3

    IFormFileは、アセンブリ 'Microsoft.AspNetCore.Http3.0'からタイプMicrosoft.AspNetCore.Http.Internal.FormFile 'をロードしません

  4. 4

    AspNet Core2タイプ「Microsoft.AspNetCore.Http.HttpContext」セッションのサービスがありません

  5. 5

    .Net Core2.2でMicrosoft.AspNetCore.Mvc.Internal.ActionContext.GetNormalizedRouteValueを解決する方法

  6. 6

    System.TypeLoadException: 'メソッド' GetItem'in type 'Microsoft.AspNetCore.Mvc.Razor.Internal.FileProviderRazorProjectFileSystem'in asp.net core

  7. 7

    Golang TCP server: how to write HTTP2 data

  8. 8

    Where can I find the ASP.NET Core 2 source code? Specifically for Microsoft.AspNetCore.Authentication.OpenIdConnect

  9. 9

    AspNetCore (Kestrel) request timed out

  10. 10

    Http2 not working with express

  11. 11

    Microsoft.AspNetCore.Http.HttpRequestをHttpRequestMessageに変換します

  12. 12

    Why do I get this InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.RequestDelegate'?

  13. 13

    microsoft.aspnetcore.signalr.clientとmicrosoft.aspnetcore.signalr.client.coreの違いは何ですか?

  14. 14

    Microsoft.AspNetCore.Hosting.Internal.WebHostおよびlog4net.LogicalThreadContext.Properties ["requestid"]

  15. 15

    Microsoft.AspNetCore.Hosting.Internal.WebHostおよびlog4net.LogicalThreadContext.Properties ["requestid"]

  16. 16

    プレーンhttpを介してC#Kestrel Webサーバーでhttp2を有効にするにはどうすればよいですか?

  17. 17

    .NET Core 2でMicrosoft.AspNetCore.Allを使用すると、どのような問題が発生する可能性がありますか?

  18. 18

    .NET Core 2で定義されているMicrosoft.AspNetCore.Antiforgeryライブラリはどこにありますか?

  19. 19

    Optimizing File Cacheing and HTTP2

  20. 20

    'Microsoft.AspNetCore.Http.IFormFile.Headers'のコレクションタイプ 'Microsoft.AspNetCore.Http.IHeaderDictionary'はサポートされていません

  21. 21

    Microsoft.AspNetCore.TestHostに含まれているTestServerを使用せずにテスト用にKestrelを実行する

  22. 22

    .NET Microsoft.AspNetCore.Http.ExtensionsUriHelper.GetDisplayUrlが無効なURIを返します

  23. 23

    ASP.NET Core 2.xのKestrelでHTTPS / SSLを使用する方法

  24. 24

    メソッドが見つかりません: 'Microsoft.Extensions.Primitives.StringValues Microsoft.AspNetCore.Http.IQueryCollection.get_Item(System.String)

  25. 25

    .NET Core adding client certificate to POST to Kestrel REST API on Linux fails server cert validation

  26. 26

    Net Core 3でServer:Kestrelヘッダーを削除します

  27. 27

    Getting Microsoft.AspNetCore.Hosting.Diagnostics[6] Application startup exception in .NET Core 3

  28. 28

    How to intercept 500 internal server error in WSO2 ESB

  29. 29

    Microsoft.AspNet.Identity.CoreとMicrosoft.AspNetCore.Identityの違いは何ですか?

ホットタグ

アーカイブ