基本上,我从.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
};
}
在.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之上执行您自己的自定义实现: