Monday, April 11, 2011

Routing WCF services by code

When you want to route the requests to specific WCF service deppending on the url, you can add routers on the Application_Start method of the Global.asax.cs. In the following example I want my ProductionService handle the service calls on the path starting with /Production/ and my PublicService handle the requests with a path starting with /public/demo/

using System;
using System.Web.Routing;
using System.ServiceModel.Activation;

public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
#region Register Routes
var factory =
new WebServiceHostFactory();
RouteTable.Routes.Add(
new ServiceRoute("Production", factory, typeof(ProductionService)));
RouteTable.Routes.Add(
new ServiceRoute("Public/demo/", factory, typeof(PublicService)));
#endregion
}