在 Internet 信息服务 (IIS) 中承载 Windows Communication Foundation (WCF) 时,需要将一个 .svc 文件放在虚拟目录中。此 .svc 文件应指定所用的服务主机工厂以及实现该服务的类。对服务发出请求时,在 URI 中指定 .svc 文件,例如 https://contoso.com/EmployeeServce.svc。对于编写 REST 服务的程序员,此类型的 URI 并非最佳选择。REST 服务的 URI 指定的是特定资源,通常没有任何扩展。System.Web.Routing 集成功能可用于承载响应无扩展的 URI 的 WCF REST 服务。有关路由的更多信息,请参见路由。
使用 N:System.Web.Routing 集成
若要使用 System.Web.Routing 集成功能,请使用 ServiceRoute 类创建一个或多个路由,然后将这些路由添加到 Global.asax 文件中的 RouteTable。这些路由可指定服务所响应的相对 URI。下面的示例演示如何执行此操作。
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.ServiceModel.Activation" %>
<%@ Import Namespace="System.ServiceModel.Web " %>
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.Add(new ServiceRoute("Customers", new WebServiceHostFactory(), typeof(Service)));
}
</script>
这示例将具有以 Customers 开头的相对 URI 的所有请求路由到 Service
服务。
在 Web.config 文件中,必须添加 System.Web.Routing.UrlRoutingModule
模块,将 runAllManagedModulesForAllRequests
特性设置为 true
,以及将 UrlRoutingHandler
处理程序添加到 <system.webServer>
元素,如下面的示例所示。
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
<handlers>
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd"/>
</handlers>
</system.webServer>
此示例将加载路由所需的模块和处理程序。有关更多信息,请参见 路由.另外,还必须在 <serviceHostingEnvironment>
元素中将 aspNetCompatibilityEnabled
特性设置为 true
,如下面的示例所示。
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<!-- ... -->
</system.serviceModel>
实现该服务的类必须启用 ASP.NET 兼容性要求,如下面的示例所示。
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
// ...
}