简体   繁体   中英

How to Host WCF REST Service and WCF Data Service in the same global.asax

I have a WCF REST web service that is hosted via a service route in global.asax which looks like this;

protected override void RegisterRoutes(System.Web.Routing.RouteCollection routeTable)
    {
        routeTable.Add(new ServiceRoute("", new WebServiceHostFactory(),
                       typeof(UserService)));
    }

I am wondering whether or not it is possible to also host another web service (which is a WCF Data Service) in the same application.

protected override void RegisterRoutes(System.Web.Routing.RouteCollection routeTable)
    {
        routeTable.Add(new ServiceRoute("", new WebServiceHostFactory(),
                       typeof(UserService)));
        routeTable.Add(new ServiceRoute("OData", new DataServiceHostFactory(),
                       typeof(UserDataService)));
    }

Attempting to navigate in my browser to http://localhost:port/ brings up the standard REST service fine whilst navigating to http://localhost:port/OData brings up the 'end point not found page'.

The reason for this is that I have legacy code in the REST service I need to keep around but also want to expose some pure data via the data service.

It turns out this was exceedingly simple and I completely overlooked the obvious.

It appears when you are hosting multiple service routes you cannot have a default/empty route prefix on any of the routes as you can with a single route. Note this was what I had in my question above for the UserService route.

Thus providing a route prefix for both service routes allows both services to be hosted within the same global.asax.

Providing code for completeness...

protected override void RegisterRoutes(System.Web.Routing.RouteCollection routeTable)
{
    routeTable.Add(new ServiceRoute("Rest", new WebServiceHostFactory(),
                   typeof(UserService)));
    routeTable.Add(new ServiceRoute("OData", new DataServiceHostFactory(),
                   typeof(UserDataService)));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM