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

问题描述 投票:0回答:1

基本上,我从.net框架迁移到.netcore,然后遇到一个错误,未找到Web请求处理程序。我搜索了.netcore here的替代方法。

他们还有其他注册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?)

public HttpClient ConfigureHttpClient(Configuration.Configuration config)
{
    WebRequestHandler mtlsHandler = new WebRequestHandler
    {
        UseProxy = true,
        UseCookies = false,
        CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore),
        AuthenticationLevel = AuthenticationLevel.MutualAuthRequired,
        AllowAutoRedirect = false
    };
}
c# .net-core .net-framework-version
1个回答
0
投票

在.netcore中,等效项为HttpClientHandler,描述为here。由于您已经引用的post中提到的某些原因,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);

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

  1. [BypassCache等效项之外,不支持CachePolicy,已在here中讨论。
  2. 不支持AuthenticationLevel,已讨论here
© www.soinside.com 2019 - 2024. All rights reserved.