Home > Uncategorized > Using HTTP/2 from #AWS Lambda in .NET Core

Using HTTP/2 from #AWS Lambda in .NET Core

If you need to make an outbound HTTP request using HTTP/2 instead of HTTP/1.1 then you will need to modify your HTTP request in C# to do so, since it is not the default. If you are working in a windows environment, then you may have used code such as the following to perform a HTTP/2 request;

public class Http2CustomHandler : WinHttpHandler
{ // PM> Install-Package System.Net.Http.WinHttpHandler - REQ .NET 4.6.2 +
	protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
	{
		request.Version = new Version("2.0");
		return base.SendAsync(request, cancellationToken);
	}
}

Which requires the System.Net.Http.WinHttpHandler NUGET package.

However, if you try to use this code in .NET core, and run it on a non-Windows platform, such as AWS Lambda, then you get an error such as;

System.PlatformNotSupportedException: WinHttpHandler is only supported on .NET Framework and .NET runtimes on Windows. It is not supported for Windows Store Applications (UWP) or Unix platforms.
at System.Net.Http.WinHttpHandler..ctor()

There is a workaround, in .NET Core 3 (Which is supported by AWS Lambda), where you can simply specify the HTTP Version in the HTTPRequest object

var initialRequest = new HttpRequestMessage(HttpMethod.Get, strUrl)
{
	Version = new Version(2, 0)
};

var homepage = httpClient.SendAsync(initialRequest ).Result.Content.ReadAsStringAsync().Result;

Much simpler!

Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: