在基于Rest的WCF开发中,我们有必要将原来的/xxx.svc/xxx的Url更改为/xxx/xxx,在WCF4中,使用Asp.Net模式可以通过System.Web.Routing组件来完成这一操作.
首先,在项目中引用Syste.Web.Routing和System.ServiceModel.Activation这两个程序集,然后修改web.config配置,
1.修改system.webserver节点,增加modules
代码
<system.webServer>
2 <modules runAllManagedModulesForAllRequests="true">
3 <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
4 System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
5 </modules>
6 </system.webServer>
2.修改serviceHostingEnvironment,启用asp.net模式
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
然后修改Global.ascx中的数据:
代码
protected void Application_Start(object sender, EventArgs e) {
RegisterRoutes();
}
private static void RegisterRoutes() {
RouteTable.Routes.Add(new ServiceRoute("TestService",
new WebServiceHostFactory(), typeof(TestService)));
}
最后修改Service文件,以支持该模式:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]