2012-09-21 7 views
5

WCF 용 REST 서비스를 구현했습니다. 이 서비스는 많은 클라이언트가 호출 할 수있는 하나의 기능을 제공하며이 기능을 완료하는 데 1 분 이상 걸립니다. 그래서 제가 원했던 것은 각 클라이언트에 대해 새로운 객체가 사용 되었기 때문에 한 번에 많은 클라이언트를 처리 할 수 ​​있다는 것입니다. 그것의 구현WCF REST 서비스 : InstanceContextMode.PerCall이 작동하지 않습니다.

[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); 
     } 
    } 

감사합니다!

+0

어떻게 클라이언트 요청을 산란됩니다? 그것에 대한 코드를 보여줄 수 있습니까? 질문을 편집하여 세부 사항을 추가 할 수 있습니다. – Jeroen

+0

클라이언트 측 코드가 추가되었습니다. – Cleo

+0

서비스가 공유 리소스를 사용하지 않는 경우 ServiceBehavior를 Concurrency Multiple을 사용하여 Single로 변경할 수 있습니다. 이렇게하면 하나의 서비스 인스턴스가 제공되며, 이는 다중 스레드 (각 호출마다 하나의 스레드)입니다. '[ServiceBehavior (InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] ' – JanW

답변

1

서비스가 GUI에 의해 제어되기 때문에, "UseSynchronizationContext"속성이 문제를 해결하기 위해 필요했다 :

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode=ConcurrencyMode.Multiple, UseSynchronizationContext=false)] 
관련 문제