locked
Configuring Routing/Activation for RESTful WCF service with MVC app RRS feed

  • Question

  • User33487012 posted

    I'm sure this is a common scenario but I haven't really found much exactly outlining what the best way to do it is. 

    My scenario is this: I have a VS2013 solution with an MVC front end project, a data domain project, and several WCF web service projects.

    I am in the process of setting up the first WCF web service and I am a little confused about how to register it's routing information and how it is activated. I want the services to be hosted in IIS/WAS alongside the ASP.NET MVC site. I also want the web services to be RESTful.

    So, I made a WCF service project and deleted the default service and added my own RESTful service with a single method that returns a "hello world" string. I setup the config file to match the one in the WCF REST Service Template 40. It just has a single standard endpoint, asp net compatibility mode turned on, and the UrlRoutingModule defined.

    I had defined a Global.asax file for that project (my MVC project obviously already has one) and in the Application_Start() method I added a ServiceRoute to register my RESTful service. If I set the service as the startup project then I am able to use the service through the browser as excepted. However, this only starts up the service and the MVC project is not run.

    How do I keep my MVC project as the startup project while having my WCF services startup and be registered with the ASP.NET routing system?

    I guess I could add ServiceRoutes to the Global.asax file in the MVC app to register the services, but I feel like there might be a looser-coupled way to do this. If this makes a difference, I also plan on using the NinjectServiceHostFactory instead of the default ServiceHostFactory.

    Saturday, March 15, 2014 8:51 PM

Answers

  • User-417640953 posted

    Hi dummerbd,

    Thanks for your post and solution.

    For run both the mvc project and wcf restfull model service, I suggest you create the rest service inside the mvc project.

    How to create and host WCF services in a MVC environment, please check below article.

    http://www.cleancode.co.nz/blog/1188/wcf-mvc

    public class RouteConfig
     
    {
     
         public static void RegisterRoutes(RouteCollection routes)
        {
     
             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     
     
             routes.MapRoute(
     
                     name: "Default",
     
                      url: "{controller}/{action}/{id}",
     
                     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
     
                     ,constraints: new { controller = @"^(?!api)\w+$" }
     
               );
     
     
            // Add wcf service
     
            //WebServiceHostFactory, WebHttpBehavior,  WebHttpBinding
     
            RouteTable.Routes.Add(new ServiceRoute(
               @"api/RestService", new WebServiceHostFactory(), typeof(RestService)));
     
       }
     
    }
    

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 18, 2014 2:15 AM

All replies

  • User33487012 posted

    Well I've sort of figured out a solution. I made a RouteConfig class with a static method called RegisterRoutes(RouteCollection routes) in the WCF project that registers the ServiceRoute. This method is called in the Application_Start method of the MVC project before the MVC's RegisterRoute method.

    I had to add a few things to the MVC project's Web.config file to get asp net compatibility mode and it works for the most part. The MVC site and the WCF service both start/activate and I'am able to access both.

    Unfortunately, now I'm having routing problems... I'm sure I can figure it out eventually but this solution still doesn't seem ideal. Any suggestions are welcome.

    Saturday, March 15, 2014 9:42 PM
  • User-417640953 posted

    Hi dummerbd,

    Thanks for your post and solution.

    For run both the mvc project and wcf restfull model service, I suggest you create the rest service inside the mvc project.

    How to create and host WCF services in a MVC environment, please check below article.

    http://www.cleancode.co.nz/blog/1188/wcf-mvc

    public class RouteConfig
     
    {
     
         public static void RegisterRoutes(RouteCollection routes)
        {
     
             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     
     
             routes.MapRoute(
     
                     name: "Default",
     
                      url: "{controller}/{action}/{id}",
     
                     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
     
                     ,constraints: new { controller = @"^(?!api)\w+$" }
     
               );
     
     
            // Add wcf service
     
            //WebServiceHostFactory, WebHttpBehavior,  WebHttpBinding
     
            RouteTable.Routes.Add(new ServiceRoute(
               @"api/RestService", new WebServiceHostFactory(), typeof(RestService)));
     
       }
     
    }
    

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 18, 2014 2:15 AM
  • User33487012 posted

    Thank you for providing an answer and the link.

    I ended up doing pretty much what you did, I registered the service routes in my MVC project and defined the service endpoint there too. I just left all the actual service implementation in a separate assembly which was what I was going for.

    Tuesday, March 18, 2014 11:45 AM