我有一个非常奇怪的错误,只有在我有某个DelegatingHandler拦截调用时才会弹出我的应用程序。它的要点是,当请求通过我的DelegatingHandler时,HttpContext.Current对于Web服务器仅接收的第一个请求是。因此,如果我是第一个在部署代码后点击我的API的人,我就会收到错误消息。否则,我很酷。
以下是错误发生的概述:
SendAsync
函数,并通过调用return await base.SendAsync(request, cancellationToken);
HttpContext.Current
为空(尝试HttpContext.Current.Items
为IUnitOfWork
所以看起来线程关联/上下文只在我的处理程序中被更改?我不明白。调用堆栈始终显示错误(HttpContext为null)在最终await base.SendAsync(request, cancellationToken);
内发生。奇怪。
这是处理程序代码:
public class RecipeIdValidator:DelegatingHandler
{
private static string[] _Ignore = new string[] {"api/webhook" };
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if(_Ignore.Any(i=>request.RequestUri.AbsoluteUri.IndexOf(i)!=-1))
return await base.SendAsync(request, cancellationToken);
var recipeId = request.GetQueryNameValuePairs().Where(i=>i.Key.Equals("recipeid",StringComparison.InvariantCultureIgnoreCase));
if (!recipeId.Any())
return request.CreateErrorResponse(System.Net.HttpStatusCode.Forbidden,"Missing recipeId/api key. Please add this to your request in the form of the querystring ?recipeId=value.");
CheckValidity(recipeId);
return await base.SendAsync(request, cancellationToken);
}
}