Monday, September 13, 2010

Clean paths with WCF Hosted Workflows

I am a big fan of the things added to WCF 4.0. One of those things is the deep integration with ASP.NET routes. Today, I was writing a service in WF and hosting the workflow with WCF. I really didn’t like the service URL—yeah, I’m a picky developer who doesn’t like exposing implementation details in the URL.
I knew a few things:
1. WF/WCF integration provides a ServiceHostFactory named WorkflowServiceHostFactory for hosting XAMLX files in WCF.
2. WorfklowServiceHostFactory will see CreateServiceHost called with some constructorString plus a bunch of baseAddresses.
3. I wanted the host to work on HTTP only—I don’t care about goofy URLs for net.tcp.
4. XAMLX services do not have a runtime defined type- they exist only in XAML.
My goal was to create a new route type, like ServiceRoute, that allowed me to pass in the desired path and the path to the XAMLX to instantiate. After a few minutes of thinking and hacking, I had the following:
public class WorkflowServiceRoute : ServiceRoute
{
 public class HostedWorkflowServiceHostFactory :
   WorkflowServiceHostFactory
 {
   public HostedWorkflowServiceHostFactory(string xamlxPath)
   {
     XamlxPath = xamlxPath;
   }
   string XamlxPath { get; set; }
   public override System.ServiceModel.ServiceHostBase CreateServiceHost(
     string constructorString, Uri[] baseAddresses)
   {
     return base.CreateServiceHost(XamlxPath, baseAddresses);
   }
 }
Read more: devlicio.us