Using Combres in Sitecore 6.6

After lots of frustration getting Combres to work properly with Sitecore 6.6, I got very good help from the Sitecore support team and a solution worth sharing with the community.

The problem is that since the first release of Sitecore 6.6, you cannot register a route to a synchronous IHttpHandler. (In the developer preview of 6.6 and in previews versions you could.) Instead you get this quite confusing error:

combres-route-problem

It turned out that Sitecore now only accepts routes to IAsyncHttpHandler, and since Combres handler is not asynchronous and is also  a sealed class, it got a bit tricky.

The Sitecore support team came up with this solution:

The easiest way to make it work is to add the Combres route after the InitializeRoutes processor of the “initialize” pipeline. It is defined in App_Config/Include/Sitecore.Mvc.config, and if intialized after, it won’t be wrapped by the RouteHandlerWrapper and will work as expected. It’s also important that the route is inserted on the first position of the RouteTable, otherwise Combres requests could match other routes.

Add an initialization class to your project:

public class InitializeCombres
{
    public virtual void Process(PipelineArgs args)
    {
        RouteTable.Routes.AddCombresRoute("Combres");
        RouteBase combresRoute = RouteTable.Routes["Combres"];
        RouteTable.Routes.Remove(combresRoute);
        RouteTable.Routes.Insert(0, combresRoute);
    }
}

Then add a config file into your App_Config/Include directory.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <!-- This must be after Sitecore.Mvc.Pipelines.Loader.InitializeRoutes -->
        <processor type="MyWeb.InitializeCombres, MyWeb"/>
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

Make sure it runs after Sitecore MVC, so if you use Sitecore.MVC.config in your include-folder, name the combres initialize config file to something that’ll run after Sitecore.MVC.config.

If you have other synchronous http handlers, where you have easy access to the source code, you can always wrap the synchronous handler into an asynchronous handler like this:

public class MyHandler : IHttpAsyncHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // do your work here
    }

    public bool IsReusable { get { return false; } }

    protected delegate void AsyncProcessorDelegate(HttpContext context);

    private AsyncProcessorDelegate _processorDelegate;

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
    {
        _processorDelegate = new AsyncProcessorDelegate(ProcessRequest);
        return _processorDelegate.BeginInvoke(context, cb, extraData);
    }

    public void EndProcessRequest(IAsyncResult result)
    {
        _processorDelegate.EndInvoke(result);
    }
}

2 thoughts on “Using Combres in Sitecore 6.6

  1. Thanks for this post, I actually found it out myself after 2 hours investigating when doing upgrade to 7.2 update 3, and noticed that if you enable MVC config, and want to use MVC architecture approach Combres will fail, the most unpleasant thing is as usual – bloody Sitecore developers – do provide informative exception log, that didn’t ring a bell at all looking at this exception. I must say its enough to have standard RouteTable.Routes.AddCombresRoute(“Combres”); its just needs to be initialized after the mvc routes initialization, so that’s why the solution to have it initialized on PreStart (PreApplicationStartMethod) won’t work anymore as it will before mvc routes initialization, which will bring this error.

  2. Just remove Combres.cs from App_Start, which is added by default when you install via NuGetPackage, in your assembly add InitializeCombres class with Process method like this:

    public class InitializeCombres
    {
    public virtual void Process(PipelineArgs args)
    {
    RouteTable.Routes.AddCombresRoute(“Combres”);
    }

    and no need to create a separate config file, just modifying Sitecore.MVC.config where

    add new processor type you’ve just created:

    after

    that’s it.

Comments are closed.