未找到 .net 核心中的 Web 请求处理程序?

4 c# .net-framework-version .net-core

基本上我从 迁移.framework.core然后我遇到了一个错误,找不到 Web 请求处理程序。我.netcore 在这里搜索了替代方法。

是否有其他注册 Web 请求处理程序的方法?

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?)
Run Code Online (Sandbox Code Playgroud)
public HttpClient ConfigureHttpClient(Configuration.Configuration config)
{
    WebRequestHandler mtlsHandler = new WebRequestHandler
    {
        UseProxy = true,
        UseCookies = false,
        CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore),
        AuthenticationLevel = AuthenticationLevel.MutualAuthRequired,
        AllowAutoRedirect = false
    };
}
Run Code Online (Sandbox Code Playgroud)

M. *_* F. 7

在 .netcore 中,等效的将是HttpClientHandler,描述here。由于您已经引用的帖子中提到的某些原因,HttpClientHandler公开的选项少于WebRequestHandler.

下面是以HttpClient类似于您的示例的方式配置的代码,使用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);
Run Code Online (Sandbox Code Playgroud)

不幸的是,还有 2 个未解决的方面,都需要在 HttpClientHandler 之上进行您自己的自定义实现:

  1. 不支持CachePolicyBypassCache 等价物,此处讨论。
  2. 不支持AuthenticationLevel这里讨论。