如何在同一个Global.asax的主机WCF REST服务和WCF数据服务 [英] How to Host WCF REST Service and WCF Data Service in the same global.asax

查看:772
本文介绍了如何在同一个Global.asax的主机WCF REST服务和WCF数据服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是通过服务路由在Global.asax中看起来像这样举办一个WCF REST Web服务;

 保护覆盖无效的RegisterRoutes(System.Web.Routing.RouteCollection routeTable)
    {
        routeTable.Add(新ServiceRoute(,新WebServiceHostFactory(),
                       的typeof(UserService的)));
    }
 

我想知道是否有可能还承载另一个Web服务(这是一个WCF数据服务)在同一应用程序。

 保护覆盖无效的RegisterRoutes(System.Web.Routing.RouteCollection routeTable)
    {
        routeTable.Add(新ServiceRoute(,新WebServiceHostFactory(),
                       的typeof(UserService的)));
        routeTable.Add(新ServiceRoute(OData的新DataServiceHostFactory()
                       的typeof(UserDataService)));
    }
 

试图在我的浏览器导航到的http://本地主机:端口/ 带来了标准的REST服务优良而导航到的http://本地主机:端口/ OData的会弹出找不到网页终点

这样做的原因是,我有传统的code在REST服务,我需要保持周围也希望通过数据服务来揭露一些纯粹的数据。

解决方案

事实证明,这是非常简单的,我完全忽略了明显的。

这时候您承载多个服务航线看来你不能在任何航线有一个默认/空航线preFIX,你可以用一个单一的路线。请注意,这是我曾在我的问题上面的UserService的路线。

因此​​,对于这两种服务航线提供航线preFIX允许这两种服务在同一范围内的Global.asax被托管。

提供了code的完整性......

 保护覆盖无效的RegisterRoutes(System.Web.Routing.RouteCollection routeTable)
{
    routeTable.Add(新ServiceRoute(休息,新WebServiceHostFactory()
                   的typeof(UserService的)));
    routeTable.Add(新ServiceRoute(OData的新DataServiceHostFactory()
                   的typeof(UserDataService)));
}
 

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

这篇关于如何在同一个Global.asax的主机WCF REST服务和WCF数据服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆