Search code examples
asp.net-mvcwcfasp.net-web-apiroutesasp.net-web-api-routing

MVC5 and WebAPI custom Routing with WCF


I have an MVC5 application that serves as a framework and uses plugins to extend functionality. Each plugin is its own MVC5/WebAPI project, and I have my framework project configured to dynamically install plugins and retrieve embedded resources.

My problem is each plugin can have WCF web services, WebAPI endpoints, and standard MVC controllers. For example, my directory structure might be:

MyFramework
    Resources
    Controllers (my framework level controllers)
    Plugins
        CoolNewPlugin
             Services
                 CoolService.svc
             Controllers
                CoolAPIController
        OtherPlugin
             Controllers
                OtherAPIController
        CollectionOfPlugins
             AnotherPlugin
                Services
                    AnotherWCFService.svc
        etc....

My goal is to have the following routes available:

/api/REST/{plugin}/{controller}/{action}

/api/SOAP/{plugin}/Services/Servicename.svc

Essentially I want hide the complexity of the application's directory structure from the end users trying to consume the web services (either SOAP or REST).

The REST stuff I think I have handled using a custom IHttpControllerSelector.

The problem is the WCF service endpoints. They are defined inside of each plugin independently (each plugin has its own web.config file that its services are using), and therefore I can't just modify the element to support the other address.

In the above example, the SOAP Service endpoints would currently look like:

http://example.com/plugins/CoolNewPlugin/Services/CoolService.svc

http://example.com/plugins/CollectionOfPlugins/AnotherPlugin/Services/AnotherWCFService.svc

and what I want is:

http://example.com/api/SOAP/CoolNewPlugin/Services/CoolService.svc

http://example.com/api/SOAP/AnotherPlugin/Services/AnotherWCFService.svc

Thanks for the help.


Solution

  • What you need to do is to create the WCF endpoint dynamically, instead of using the configuration on the web.config. In tis way you can host the WCF address in the desired URL. You have the MSDN documentation to do this here: How to: Create a Service Endpoint in Code

    You'll probably also have to ignore the routes in your route configuration code for MVC / Web API.