6

Basically I migrate from .framework to .core then I faced one error, Web request handler is not found. I searched alternate methods for .netcore here.

Is there any other method of registering web request handler?

error

Severity    Code    Description Project File    Line    Suppression State
Error   CS0246  The type or namespace name 'WebRequestHandler' could not be found 
(are you missing a using directive or an assembly reference?)

public HttpClient ConfigureHttpClient(Configuration.Configuration config)
{
    WebRequestHandler mtlsHandler = new WebRequestHandler
    {
        UseProxy = true,
        UseCookies = false,
        CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore),
        AuthenticationLevel = AuthenticationLevel.MutualAuthRequired,
        AllowAutoRedirect = false
    };
}
kwoxer
  • 3,734
  • 4
  • 40
  • 70

1 Answers1

12

In .netcore an equivalent would be HttpClientHandler, described here. For some of the reasons mentioned in the post you have referenced already, HttpClientHandler exposes less options than WebRequestHandler.

Below is the code that configures HttpClient in a manner similar to your example, using HttpClientHandler:

var mtlsHandler = new HttpClientHandler {
   UseProxy = true,
   UseCookies = false,
   AllowAutoRedirect = false
   // CachePolicy = ... not supported and set to HttpRequestLevel.BypassCache equivalent, see https://github.com/dotnet/runtime/issues/21799
   // AuthenticationLevel = ... need to implement it yourself by deriving from HttpClientHandler, see https://stackoverflow.com/questions/43272530/whats-the-alternative-to-webrequesthandler-in-net-core
};

var httpClient = new HttpClient(mtlsHandler);

Unfortunately there are 2 unresolved aspects remaining, both which require to do your own custom implementation on top of HttpClientHandler:

  1. No support for CachePolicy other than BypassCache equivalent, discussed here.
  2. No support for AuthenticationLevel, discussed here.
M. F.
  • 1,654
  • 11
  • 16