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

冷暖自知 提交于 2019-12-06 07:35:06

问题


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:


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)));
}


来源:https://stackoverflow.com/questions/6463933/how-to-host-wcf-rest-service-and-wcf-data-service-in-the-same-global-asax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!