Search code examples
c#asp.netweb-serviceswcfodata

Route to different services based on URL template


I'm working on Backward Compatible project. In which, a WCF service supports OData v1-v3. I'm building ASP .NET Web API which support OData v4.

I have to call them as shows here:
http://localhost:17505/api/v4/Products should call ASP .NET Web API service. http://localhost:17505/api/v3/Products should call WCF service

Where should the routing take place?


Solution

  • I found and implemented the answer finally. It seems http.sys handles everything we need. Only the routing template should differ.

    Had initiateApis method which calls Start methods of both WebAPI and WCF classes with parameter as the uri.

    In WebAPI class - WebApi.cs

    WebApp.Start<WebApi>(uri);
    

    In WCF class - WcfService.cs

    _serviceHost = new DataServiceHost(typeof(WcfService), new Uri[0]);
    _serviceHost.AddServiceEndpoint(typeof(IRequestHandler), httpBinding, uri);
    

    So whenever passing the url,

    WebApi.Start("http://localhost:17505/api/v4/");
    WcfService.Start("http://localhost:17505/api/v3/");
    

    Happy Coding :)