What is the best way to replace the default HttpControllerDispatcher globally

Topics: ASP.NET Web API
Oct 22, 2012 at 8:09 PM

Assume that I have the following custom HttpControllerDispatcher implementation:


    public class MyCustomDispatcher : HttpControllerDispatcher {

        protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, 
            CancellationToken cancellationToken) {
            // Do you stuff here

            // According to your requirements, either run its default funtionality 
            // or return your own stuff
            return base.SendAsync(request, cancellationToken);


And I want to register this globally. I thought that the best way would be to replace the GlobalConfiguration.DefaultHandler but it turns out that that property is read-only. so, the below solution is not applicable.

protected void Application_Start(object sender, EventArgs e) {

    GlobalConfiguration.DefaultHandler = new HttpRoutingDispatcher(
        GlobalConfiguration.Configuration, new MyCustomDispatcher());


What is the best way to replace the default HttpControllerDispatcher globally? It's easy to do it per-route by attaching the handler but I wasn't able to find a way to do it globally.

Oct 22, 2012 at 8:24 PM

I don’t think we provide a way to set the global default message handler when web hosted. Do you want to open an issue in our Issue Tracker to track making it settable?

Thanks for bringing this to our attention.

Daniel Roth

Oct 22, 2012 at 8:29 PM

Hi Daniel,

yep, right. Here is how HttpControllerHandler constructs a new HttpServer instance:

        private static readonly Lazy<HttpMessageInvoker> _server =
            new Lazy<HttpMessageInvoker>(
                () =>
                    HttpServer server = new HttpServer(GlobalConfiguration.Configuration, GlobalConfiguration.DefaultHandler);
                    return new HttpMessageInvoker(server);

I am not sure why GlobalConfiguration.DefaultHandler was made read-only but I am assuming there is a reason. I will open up an issue anyway.


Oct 22, 2012 at 8:32 PM

here it is: http://aspnetwebstack.codeplex.com/workitem/574

Oct 23, 2012 at 2:52 PM

The following articles helped me to solve my problem:

  1. http://blog.ploeh.dk/2012/09/28/DependencyInjectionAndLifetimeManagementWithASPNETWebAPI.aspx
  2. http://blog.ploeh.dk/2012/10/03/DependencyInjectionInASPNETWebAPIWithCastleWindsor.aspx
Oct 23, 2012 at 2:54 PM


Thanks but those are not related to this topic.