2012-11-24 84 views
0

當我添加HttpHeader篩選器時,其他篩選器停止工作。我不知道爲什麼。Web API篩選器停止擊中其他篩選器

BaseApiController.cs

[ExceptionHandling, ApiValidation, HttpHeader("X-Robots-Tag", "noindex, nofollow")] 
    public abstract class BaseApiController : System.Web.Http.ApiController 
    { 

HttpHeaderFilter.cs

namespace Tournaments.Models.Mvc.Filters 
{ 
    public class HttpHeaderAttribute : System.Web.Http.Filters.FilterAttribute 
    { 
     public string Name { get; set; } 
     public string Value { get; set; } 

     public HttpHeaderAttribute(string name, string value) 
     { 
      Name = name; 
      Value = value; 
     } 

     public override bool AllowMultiple 
     { 
      get { return true; } 
     } 
    } 

    public class HttpHeaderFilter : System.Web.Http.Filters.IActionFilter 
    { 
     private readonly string _name; 
     private readonly string _value; 

     public HttpHeaderFilter(string name, string value) 
     { 
      _name = name; 
      _value = value; 
     } 

     public bool AllowMultiple 
     { 
      get { return true; } 
     } 

     public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation) 
     { 
      return continuation().ContinueWith<HttpResponseMessage>((tsk) => 
       { 
        HttpResponseMessage response = tsk.Result; 

        response.Headers.Add(_name, _value); 

        return response; 

       }, TaskContinuationOptions.OnlyOnRanToCompletion);; 
     } 
    } 
} 

的Global.asax

protected override IKernel CreateKernel() 
     { 


      return Bootstrap.Configure((kernel) => 
       { 
        kernel.Bind<IUnitOfWork>().To<UnitOfWork>(); 
        kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InRequestScope(); 
        //kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InScope(q => HttpContext.Current ?? StandardScopeCallbacks.Thread(q)); 
        kernel.Bind<IEmailService>().To<EmailService>().WithConstructorArgument("templates", emailTemplates); 
        kernel.Bind<IPaymentMethod>().To<AuthorizeNetProvider>().Named("AuthorizeNet"); 
        kernel.Bind<IPaymentMethod>().To<StripeProvider>().Named("Stripe"); 
        kernel.Bind<List<IPaymentMethod>>().ToConstant(kernel.GetAll<IPaymentMethod>().ToList()); 

        ServiceBindings.Register(kernel); 
        RepositoryBindings.Register(kernel); 
        ValidationBindings.Register(kernel); 

        kernel.BindHttpFilter<ApiValidationFilter>(System.Web.Http.Filters.FilterScope.Action) 
         .WhenControllerHas<ApiValidationAttribute>(); 

        kernel.BindHttpFilter<HttpHeaderFilter>(System.Web.Http.Filters.FilterScope.Controller) 
         .WhenControllerHas<HttpHeaderAttribute>() 
         .WithConstructorArgumentFromControllerAttribute<HttpHeaderAttribute>("name", q => q.Name) 
         .WithConstructorArgumentFromControllerAttribute<HttpHeaderAttribute>("value", q => q.Value); 

        GlobalConfiguration.Configuration.Filters.Add(new ExceptionHandlingAttribute()); 

        GlobalConfiguration.Configuration.DependencyResolver = new Common.Ioc.NinjectDependencyResolver(kernel); 
        GlobalConfiguration.Configuration.Formatters.Add(new CustomXmlFormatter()); 
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); 
       }); 
     } 

回答

0

我添加編輯本

TaskContinuationOptions.ExecuteSynchronously 

而不是

TaskContinuationOptions.OnlyOnRanToCompletion 

,現在工作得很好。