Migration path for WCF SOAP+REST => ASP.NET Web API ?

Topics: Interop Bindings, Web Api
Mar 8, 2012 at 9:48 AM

We have a single .NET 4.0 service that's exposed at two endpoints, one REST and the other SOAP (different end customers, different business preferences). Everything is done in WCF and the code skeleton snippets are below. As expected, my IService class is decorated with [ServiceContract], [OperationContract] and [WebInvoke(UriTemplate ...)] tags. And the actual data contract classes are also decorated by [DataContract(...)] and [DataMember(...)] tags. For brevity sake, I've skipped those.

For a situation like this one, what is the migration path towards the new "ASP.NET Web API" way of doing things? Can this also be documented somewhere as a tutorial or walkthrough? It seems documentation assumes either REST or SOAP.

Thanks
Sid 

(Low priority)PS: The message text editor for the discussion forum is quite tedious. Can you leverage stackoverflows message editor? It's outstanding! Ok, back to the main topic!

Inside Global.asax.cs

routes.Add(new ServiceRoute("api/rest", new RestServiceHostFactory(typeof(IService)), typeof(Service))); routes.Add(new ServiceRoute("api/soap", new SoapServiceHostFactory(typeof(IService)), typeof(Service)));

Inside my class files

public class RestServiceHostFactory : WebServiceHostFactory 
{
	private Type serviceInterfaceType;
	
	public RestServiceHostFactory(Type serviceInterfaceType)
	{
	    this.serviceInterfaceType = serviceInterfaceType;
	}
	
	protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
	{
	    ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);
	    ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
	
	    WebHttpBinding myBinding = new WebHttpBinding();
	    host.AddServiceEndpoint(serviceInterfaceType, myBinding, "");
	    
		WebHttpBehavior helpBehavior = new WebHttpBehavior();
	    helpBehavior.HelpEnabled = true;
	    host.Description.Endpoints[0].Behaviors.Add(helpBehavior);
	
	    return host;
	}
}

   and similarly (but differently the same :) ) ... 

public class RestServiceHostFactory : WebServiceHostFactory 
{
        ...
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
           ...
           WebHttpBinding myBinding = new WebHttpBinding();
           ...
        }
}