我为WCF实现了一个REST服务。该服务提供一个函数,可以被许多客户端调用,这个函数需要超过1分钟的时间才能完成。因此,我希望为每个客户端使用一个新的对象,以便可以同时处理多个客户端。
我的接口如下所示:
[ServiceContract]
public interface ISimulatorControlServices
{
[WebGet]
[OperationContract]
string DoSomething(string xml);
}
并且它的(测试)实现:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall]
public class SimulatorControlService : SimulatorServiceInterfaces.ISimulatorControlServices
{
public SimulatorControlService()
{
Console.WriteLine("SimulatorControlService started.");
}
public string DoSomething(string xml)
{
System.Threading.Thread.Sleep(2000);
return "blub";
}
}
现在的问题是:如果我使用一个创建10(或任意数量)线程的客户端,每个线程都调用服务,它们并不并发运行。这意味着,这些调用是一个接一个地处理的。有人知道为什么会出现这种情况吗?
补充说明:客户端代码如下:
线程的生成:
for (int i = 0; i < 5; i++)
{
Thread thread = new Thread(new ThreadStart(DoSomethingTest));
thread.Start();
}
方法:
private static void DoSomethingTest()
{
try
{
using (ChannelFactory<ISimulatorControlServices> cf = new ChannelFactory<ISimulatorControlServices>(new WebHttpBinding(), "http://localhost:9002/bla/SimulatorControlService"))
{
cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
ISimulatorControlServices channel = cf.CreateChannel();
string s;
int threadID = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Thread {0} calling DoSomething()...", threadID);
string testXml = "test";
s = channel.StartPressureMapping(testXml);
Console.WriteLine("Thread {0} finished with reponse: {1}", threadID, s);
}
}
catch (CommunicationException cex)
{
Console.WriteLine("A communication exception occurred: {0}", cex.Message);
}
}
提前感谢您!
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode = ConcurrencyMode.Multiple)]
- JanW