Skip to content

Instantly share code, notes, and snippets.

@madhub
Last active March 21, 2024 13:11
Show Gist options
  • Save madhub/91b53ab2dfb082b29f3d6fdb7bb5212a to your computer and use it in GitHub Desktop.
Save madhub/91b53ab2dfb082b29f3d6fdb7bb5212a to your computer and use it in GitHub Desktop.
Setting up asp.net core server HTTP2 without TLS

Socker connect with timeout

using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;

CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

Socket sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

var timer = Stopwatch.StartNew();
try
{
    //sc.Connect(new IPEndPoint(IPAddress.Parse("222.56.1.45"), 11112));
    sc.ConnectAsync(new IPEndPoint(IPAddress.Parse("222.56.1.45"), 11112),cts.Token).GetAwaiter().GetResult();
}
catch (Exception exp)
{

    Console.WriteLine(exp);
    
    Console.WriteLine($"Socket Exception in {timer.ElapsedMilliseconds}  milliseconds");
}finally
{
    timer.Stop();
}

How to setup non secure http2 for gRPC grpc/grpc-dotnet#505

https://learn.microsoft.com/en-us/aspnet/core/grpc/troubleshoot?view=aspnetcore-3.0#call-insecure-grpc-services-with-net-core-client-2

Using configuration file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Kestrel": {
    "EndPoints": {
      "EndpointDefaults": {
        "Url": "http://0.0.0.0:5220",
        "Protocols": "Http2"
      }
    },
    "AllowedHosts": "*"
  }
}

OR Using code

builder.WebHost.ConfigureKestrel(kServerOptions =>
{
    kServerOptions.Listen(IPAddress.Any, 8081, listenOptions =>
    {
        listenOptions.Protocols = HttpProtocols.Http2;
    });
});

From Linux , Windows Curl does not support http2 negotiating Use gitbash with curl to test

curl -v --http2-prior-knowledge http://192.168.1.3:8081/weatherforecast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment