基本上我从.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?)
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
,在这里描述。由于您已经引用的帖子中提到的一些原因,HttpClientHandler
公开的选项比WebRequestHandler
少。
下面是使用HttpClientHandler
以类似于您的示例的方式配置HttpClient
的代码:
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);
不幸的是,还有 2 个未解决的方面,这两个方面都需要在 HttpClientHandler 之上执行自己的自定义实现:
- 除了 BypassCache 等效项之外,不支持
CachePolicy
,此处已讨论。 - 不支持
AuthenticationLevel
,在这里讨论。