.net 无法访问HttpClient的DelegatingHandler类中已释放的对象
我正在为HttpClient使用TokenHttpHandler。看起来像这样.net 无法访问HttpClient的DelegatingHandler类中已释放的对象,.net,asp.net-core,asp.net-core-2.1,.net,Asp.net Core,Asp.net Core 2.1,我正在为HttpClient使用TokenHttpHandler。看起来像这样 public class TokenHttpHandler : DelegatingHandler { private readonly ITokenCache tokenCache; private readonly IHttpProxyClientService proxyClientService; public TokenHttpHandler(IHttpProxyClientS
public class TokenHttpHandler : DelegatingHandler
{
private readonly ITokenCache tokenCache;
private readonly IHttpProxyClientService proxyClientService;
public TokenHttpHandler(IHttpProxyClientService proxyClientService, ITokenCache tokenCache)
{
this.proxyClientService = proxyClientService;
this.tokenCache = tokenCache;
var useProxy = AppSettingsProvider.AppSettings.Proxy.UseProxy;
InnerHandler = useProxy ? proxyClientService.GetProxyHandler() : new HttpClientHandler();
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await SetAuthHeaderAndSendAsync(request, cancellationToken, false).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
response = await SetAuthHeaderAndSendAsync(request, cancellationToken, true);
}
return response;
}
private async Task<HttpResponseMessage> SetAuthHeaderAndSendAsync(HttpRequestMessage request, CancellationToken cancellationToken, bool forceTokenRefresh)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await tokenCache.GetAccessToken(forceTokenRefresh).ConfigureAwait(false));
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}
公共类TokenHttpHandler:DelegatingHandler
{
专用只读iTokeCache令牌缓存;
私有只读IHttpProxyClientService proxyClientService;
公共令牌HttpHandler(IHttpProxyClientService proxyClientService、iTokeCache令牌缓存)
{
this.proxyClientService=proxyClientService;
this.tokenCache=tokenCache;
var useProxy=AppSettingsProvider.AppSettings.Proxy.useProxy;
InnerHandler=useProxy?proxyClientService.GetProxyHandler():新的HttpClientHandler();
}
受保护的覆盖异步任务SendAsync(HttpRequestMessage请求,CancellationToken CancellationToken)
{
var response=await SetAuthHeaderAndSendAsync(请求,取消令牌,false);
if(response.StatusCode==HttpStatusCode.Unauthorized)
{
响应=等待SetAuthHeaderAndSendAsync(请求,取消令牌,true);
}
返回响应;
}
专用异步任务SetAuthHeaderAndSendAsync(HttpRequestMessage请求、CancellationToken CancellationToken、bool forceTokenRefresh)
{
request.Headers.Authorization=newauthenticationHeaderValue(“承载者”,wait tokenCache.GetAccessToken(forceTokenRefresh.configureWait(false));
返回wait base.sendaync(请求,取消令牌).configurewait(false);
}
}
在使用autofac I的ASP.NET核心应用程序中,注册了如下类型:
// Token cache
builder.RegisterType<TokenCache>().As<ITokenCache>()
.WithParameter("scope", "Scope")
.SingleInstance();
builder.RegisterType<TokenHttpHandler>()
.InstancePerLifetimeScope();
builder.RegisterType<ProductionHttpClient>()
.As<HttpClient>()
.InstancePerLifetimeScope();
//令牌缓存
builder.RegisterType().As()
.WithParameter(“范围”、“范围”)
.SingleInstance();
builder.RegisterType()
.InstancePerLifetimeScope();
builder.RegisterType()
.As()
.InstancePerLifetimeScope();
问题在于,由于只有代码才行:
返回wait base.sendaync(请求,取消令牌).configurewait(false)
我得到一个错误:
“无法访问已释放的对象。\r\n对象名称:'TokenHttpHandler'。”
这是怎么回事
我在这里遗漏了什么?嗨,John,你在哪里能解决这个问题?据我记忆所及,我调用了async方法,忘记了等待它完成它的工作。