我下载了您的源代码以自行进行一些测试。我设法通过将其添加到web.config中使其工作:
<sessionState mode="Off"></sessionState>
该问题似乎与Asp.Net会话处理有关(似乎与WCF会话不同)。
没有这个,Fiddler将显示WCF服务响应正在发送
ASP.NET_SessionIdcookie。您的
Page等级
EnableSessionState="False"不会影响服务。
编辑 :我做了一些更多的测试,看看是否可以使它工作而不必关闭整个应用程序的会话状态。我现在开始工作了。我所做的是:
- 在应用程序的根目录下创建一个新文件夹“ ServiceFolder”
- 将服务接口和实现移至该文件夹
然后在
Global.asax我更改了
ServiceRoute注册:
RouteTable.Routes.Add(new ServiceRoute("ServiceFolder/", new WebServiceHostFactory(), typeof(MyService)));在Default.aspx中,我更改了:
$(function () { $("#callMany").click(function () { $("#message").html(""); callMethodAsync('<%=this.ResolveUrl("~/ServiceFolder/Execute1") %>', "hello"); callMethodAsync('<%=this.ResolveUrl("~/ServiceFolder/Execute2") %>', "crazy"); callMethodAsync('<%=this.ResolveUrl("~/ServiceFolder/Execute3") %>', "world"); });});然后,为了控制cookie的处理,我创建了一个
HttpModule:
using System;using System.Web;namespace RestService{ public class TestPreventcookie : IHttpModule { public void Dispose() { } public void Init(HttpApplication application) { application.BeginRequest += (new EventHandler(this.Application_BeginRequest)); application.PostAcquireRequestState += (new EventHandler(this.Application_PostAcquireRequestState)); } private void Application_BeginRequest(Object source, EventArgs e) { //prevent session cookie from reaching the service HttpApplication application = (HttpApplication)source; HttpContext context = application.Context; if (context.Request.Path.StartsWith("/ServiceFolder")) { context.Request.cookies.Remove("ASP.NET_SessionId"); } } private void Application_PostAcquireRequestState(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; HttpContext context = application.Context; if (context.Request.Path.StartsWith("/ServiceFolder")) { var s = context.Session; if (s != null) s.Abandon(); } } }}然后我在web.config中注册了该模块:
<httpModules> <add name="TestPreventcookie" type="RestService.TestPreventcookie" /></httpModules>
最后,我将Default.aspx更改为允许会话:
EnableSessionState="True"
这些更改之后,并行执行服务调用。强制性免责声明:
它可以在我的机器上工作
这个想法是当对服务的调用进入时,HttpModule会检查URL,并在必要时删除
ASP.NET_SessionIdcookie,以使其无法到达服务。然后,
Application_PostAcquireRequestState我们立即放弃创建的会话,这样就不必在大量空会话中不必要地消耗服务器内存。
使用此解决方案,您将获得:
- 并行执行服务调用
- 您仍然可以在应用程序的其他部分使用Session
以:
- 但是您必须在不需要会话cookie的可识别URL上调用服务
- 服务器将创建并放弃很多会话



