Blazor 0.6.0 "wipes" Flurl-compatibility?
Just for some background, the Blazor team is in the process of significantly reducing the app's footprint, and resorting to some unusual measures to do so. In a nutshell, they've reduced it by about 20% by "wiping" HttpClientHandler
.
wipe means "replace specified method bodies with a single throw instruction". Doing this (instead of actually removing the method entirely) means that the assembly retains a completely standard API surface, and if you try to use one of the wiped methods, you get an easy-to-understand exception stack trace that tells you which wiped method you're trying to call.
This is what you've bumped up against: Blazor is still aware of HttpClientHandler
for compilation purposes, but will throw a runtime exception if you (or in this case a compatible library) try to use it.
But HttpClient
must wrap some implementation of HttpMessageHandler
Blazor has its own: BrowserHttpMessageHandler
. And Flurl provides an easy way to swap this in via its HttpClientFactory
. But you don't need to pass in an HttpClient
instance or implement CreateHttpClient
. Instead, inherit from DefaultHttpClientFactory
and just override CreateMessageHandler
:
private class HttpClientFactoryForBlazor : DefaultHttpClientFactory
{
public override HttpMessageHandler CreateMessageHandler()
{
return new BrowserHttpMessageHandler();
}
}
I'd also recommend registering this once globally on app startup, rather than every time you create a FlurlClient
:
FlurlHttp.Configure(settings =>
{
settings.HttpClientFactory = new HttpClientFactoryForBlazor();
});
It should also be noted that Blazor is still experimental and that BrowserHttpMessageHandler
may be deprecated in a future release, so expect that this could be just a temporary work-around.