希望你能帮我解决这个问题很简单:)
我在云端的某个地方有一个WCF服务,工作正常。我正在使用PerCall和Single,没有什么特别与常规wcf不同。
我的问题如下:
当使用Web客户端(例如ajax)发送http GET请求时,请求的处理不会以我想要的方式作出反应。让我用一个简短的代码来解释我的观点:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class Api
{
[OperationContract]
[WebInvoke(UriTemplate = "api/Function1", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
public String Function1()
{
logMessage("Function1 -> Begin on thread " + Thread.CurrentThread.ManagedThreadId);
System.Threading.Thread.Sleep(3000);
logMessage("Function1 -> End on thread " + Thread.CurrentThread.ManagedThreadId);
return "Function1 done";
}
[OperationContract]
[WebInvoke(UriTemplate = "api/Function2", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
public String Function2()
{
logMessage("Function2 -> Begin on thread " + Thread.CurrentThread.ManagedThreadId);
System.Threading.Thread.Sleep(3000);
logMessage("Function2 -> End on thread " + Thread.CurrentThread.ManagedThreadId);
return "Function2 done";
}
[OperationContract]
[WebInvoke(UriTemplate = "api/Function3", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
public String Function3()
{
logMessage("Function3 -> Begin on thread " + Thread.CurrentThread.ManagedThreadId);
System.Threading.Thread.Sleep(3000);
logMessage("Function3 -> End on thread " + Thread.CurrentThread.ManagedThreadId);
return "Function3 done";
}
}
和客户端代码:
var json1 = new XMLHttpRequest();
json1.open("GET", "127.0.0.1:81/api/Function1", true);
json1.setRequestHeader("Accept", "*/*");
json1.onload = function (e) {
alert("function1 done");
}
var json2 = new XMLHttpRequest();
json2.open("GET", "127.0.0.1:81/api/Function2", true);
json2.setRequestHeader("Accept", "*/*");
json2.onload = function (e) {
alert("function2 done");
}
var json3 = new XMLHttpRequest();
json3.open("GET", "127.0.0.1:81/api/Function3", true);
json3.setRequestHeader("Accept", "*/*");
json3.onload = function (e) {
alert("function3 done");
}
json1.send(NULL);
json2.send(NULL);
json3.send(NULL);
我在这个WCF中有多个函数,我想在调用它时异步处理它们。每个测试都有3秒的线程休眠时间。
因为我是从我的网络客户端异步发送我的3个请求,所以我应该等待3秒钟并同时接收所有响应(如果它真的是异步的,因为它应该在不同的处理线程)。
根据我在网上已经阅读的内容,当向WCF服务发送请求时,它在会话中存储了一个ID,对于从同一会话发出的每个http请求都是相同的。如果会话相同,则处理将在同一个线程上完成,一个请求接一个,如FIFO。为了确保问题是由于会话,我试图同时从2个不同的浏览器(chrome和firefox)发送这些请求,它工作正常(很好,我的意思是请求是在2个不同的处理线程)。以下是一个会话的测试日志:
10:50:00 - Log service - Function1 on thread 9 -> Begin
10:50:03 - Log service - Function1 on thread 9 -> End
10:50:03 - Log service - Function2 on thread 9 -> Begin
10:50:06 - Log service - Function2 on thread 9 -> End
10:50:06 - Log service - Function3 on thread 9 -> Begin
10:50:09 - Log service - Function3 on thread 9 -> End
以下是同时进行2次会议的日志:
10:50:00 - Log service - Function 1 on thread 9 -> Begin
10:50:00 - Log service - Function 1 on thread 10 -> Begin
10:50:03 - Log service - Function 1 on thread 9 -> End
10:50:03 - Log service - Function 1 on thread 10 -> End
10:50:03 - Log service - Function 2 on thread 9 -> Begin
10:50:03 - Log service - Function 2 on thread 10 -> Begin
10:50:06 - Log service - Function 2 on thread 9 -> End
10:50:06 - Log service - Function 2 on thread 10 -> End
10:50:06 - Log service - Function 3 on thread 9 -> Begin
10:50:06 - Log service - Function 3 on thread 10 -> Begin
10:50:09 - Log service - Function 3 on thread 9 -> End
10:50:09 - Log service - Function 3 on thread 10 -> End
这就是我正在等待一个会话:
10:50:00 - Log service - Function 1 on thread 9 -> Begin
10:50:00 - Log service - Function 2 on thread 10 -> Begin
10:50:00 - Log service - Function 3 on thread 11 -> Begin
10:50:03 - Log service - Function 1 on thread 9 -> End
10:50:03 - Log service - Function 2 on thread 10 -> End
10:50:03 - Log service - Function 3 on thread 11 -> End
在没有禁用会话的情况下解决该问题的任何想法?我希望我能清楚地告诉大家,提前谢谢:)