【问题标题】:WCF REST Service: InstanceContextMode.PerCall not workingWCF REST 服务:InstanceContextMode.PerCall 不起作用
【发布时间】:2012-09-21 11:34:45
【问题描述】:

我已经为 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 更改为 Single with Concurrency Multiple。这将为您提供一个服务实例,它是多线程的(每个调用一个线程)。 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
  • 这让我很吃惊,本来以为您的代码会按预期工作。但也许this msdn thread 可以提供帮助?
  • 找到了解决方案,感谢@Jeroen! [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode=ConcurrencyMode.Multiple, UseSynchronizationContext=false)]
  • 太棒了! 24小时后不要忘记回答自己的问题并接受,这样其他登陆的人可以直接发现解决方案!

标签: wcf rest


【解决方案1】:

由于服务由 GUI 控制,因此需要“UseSynchronizationContext”属性来解决问题:

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode=ConcurrencyMode.Multiple, UseSynchronizationContext=false)] 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 2011-12-01
    相关资源
    最近更新 更多