How to Host WCF REST Service and WCF Data Service

2019-04-11 22:07发布

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.

1条回答
劫难
2楼-- · 2019-04-11 22:50

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)));
}
查看更多
登录 后发表回答