5

WCF用のRESTサービスを実装しました。このサービスは、多くのクライアントが呼び出すことができる1つの関数を提供し、この関数が完了するまでに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);
        }
    }

前もって感謝します!

4

1 に答える 1

2

サービスはGUIによって制御されるため、問題を解決するには「UseSynchronizationContext」属性が必要でした。

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode=ConcurrencyMode.Multiple, UseSynchronizationContext=false)] 
于 2012-09-21T20:28:12.810 に答える