Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the alternative to WebRequestHandler in .NET Core?

I was using the WebRequestHandler for setting the CachePolicy and AuthenticationLevel in my full stack .NET application. Now I am migrating my application to .NET core and can't find an alternative to these properties or the WebRequestHandler. Any help? Following is my usage:

        var httpClientHandler = new WebRequestHandler
        {
            UseProxy = true,
            UseCookies = false,
            CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore),
            AuthenticationLevel = AuthenticationLevel.MutualAuthRequired
        };
like image 763
Amit Avatar asked Apr 07 '17 07:04

Amit


People also ask

Is HttpWebRequest deprecated?

NET 6, the WebRequest, WebClient, and ServicePoint classes are deprecated. The classes are still available, but they're not recommended for new development. To reduce the number of analyzer warnings, only construction methods are decorated with the ObsoleteAttribute attribute.

Is .NET Core in C#?

NET Core platform such as mobile, desktop, web, cloud, IoT, machine learning, microservices, game, etc. Supports Multiple Languages: You can use C#, F#, and Visual Basic programming languages to develop .

What is .NET Core used for?

NET Core is used to create server applications that run on Windows, Linux and Mac. It does not currently support creating desktop applications with a user interface. Developers can write applications and libraries in VB.NET, C# and F# in both runtimes.

Is AddHttpClient transient?

In the preceding code, AddHttpClient registers GitHubService as a transient service. This registration uses a factory method to: Create an instance of HttpClient .


1 Answers

CachePolicy:

There is no equivalent to CachePolicy in .NET Core. However, .NET Core is equivalent to RequestCacheLevel.BypassCache. I confirmed that in this GitHub issue.

So while there is no built-in CachePolicy, this design enables you to build your own cache on top of HttpClient using any policy you like.

AuthenticationLevel:

WebRequest in .NET Core offers an AuthenticationLevel property, but that won't help you much if you need to use HttpClient.

You could implement a custom HttpMessageHandler to pass into HttpClient that supports AuthenticationLevel. To make it easier to implement, you could base it off an existing HttpMessageHandler such as the Windows one.

like image 88
Daniel Crabtree Avatar answered Oct 11 '22 04:10

Daniel Crabtree