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

▼魔方 西西 提交于 2020-01-03 08:58:48

问题


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
        };

回答1:


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.



来源:https://stackoverflow.com/questions/43272530/whats-the-alternative-to-webrequesthandler-in-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!