asp.net-web-api – webapi批处理和委托处理程序

前端之家收集整理的这篇文章主要介绍了asp.net-web-api – webapi批处理和委托处理程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
基于我的 last post,我能够得到批量工作,直到一定程度.除了注册路由特定的处理程序,我还有2个委托处理程序

>验证用户
>记录

批处理程序通过委托处理程序来验证用户并记录请求.当messagehandlerinvoker开始发送子/嵌套请求时,抛出以下异常.

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=The 'DelegatingHandler' list is invalid because the property 'InnerHandler' of 'AuthenticationMessageHandler' is not null.
Parameter name: handlers
  Source=System.Net.Http.Formatting
  ParamName=handlers
  StackTrace:
       at System.Net.Http.HttpClientFactory.CreatePipeline(HttpMessageHandler innerHandler,IEnumerable`1 handlers)
       at System.Web.Http.HttpServer.Initialize()
       at System.Web.Http.HttpServer.<EnsureInitialized>b__3()
       at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target,Boolean& initialized,Object& syncLock,Func`1 valueFactory)
       at System.Threading.LazyInitializer.EnsureInitialized[T](T& target,Func`1 valueFactory)
       at System.Web.Http.HttpServer.EnsureInitialized()
       at System.Web.Http.HttpServer.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
       at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
       at RoutingRequest.Service.Startup.BatchMessageHandler.<>c__DisplayClassd.<PrcoessRequest>b__b(Task`1 m) in C:\CEI\Clients\Footlocker.com\FL - Vendor Routing Portal\source\RoutingRequest.Service\Startup\BatchMessageHandler.cs:line 45
       at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
       at System.Threading.Tasks.Task.Execute()
  InnerException:

有没有配置选项我缺少,或者我需要绕过委托处理程序?

编辑
这是我的验证处理程序.

public class AuthenticationMessageHandler
    : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
    {
        SetCurrentUser(request);
        return base.SendAsync(request,cancellationToken);
    }

    private void SetCurrentUser(HttpRequestMessage request)
    {
        var values = new List<string>().AsEnumerable();
        if (request.Headers.TryGetValues("routingrequest-username",out values) == false) return;

        var username = values.First();

        var user = Membership.GetUser(username,true);
        if (user == null)
        {
            var message = string.Format("membership information for '{0}' could not be found.",username);
            throw new HttpRequestException(message);
        }

        var roles = Roles.GetRolesForUser(username);

        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(user.UserName),roles);
    }
}

基于Kiran的回答,一个子类的httpserver修复了一个问题并引入了另一个问题.我的角色提供者得到一个空引用异常.现在看看.

解决方法

原文链接:https://www.f2er.com/aspnet/246362.html

猜你在找的asp.Net相关文章