我想了解ServiceBehavior.ConcurrencyMode
屬性。瞭解WCF ServiceBehaviorProperty併發模式
考慮在服務端下面的代碼:
[ServiceContract]
public interface IMySercice {
[OperationContract]
int mycall(int a);
}
/*** HERE I WILL GENERATE Two variants of this code ***/
[ServiceBehaviour(InstanceContext = InstanceContextMode.Single)]
public class MyService : IMyService {
// Constructors...
// implementations of interface
public int mycall(int a) { ... }
}
class Program {
static void Main(string[] args) {
MyServiceMyS = new MyService(...); /* Creating service instance */
Uri MyUri = new Uri("http://services.mycompany.com/services/"); /* Base address for my hosted service */
using (ServiceHost host = new ServiceHost(MyS)) { /* Defining service host, configuration file contains info regarding endpoints, not shown here for clarity */
host.Start();
host.Stop();
}
}
}
現在考慮,我想調用此服務,請考慮可能調用我的服務網絡10臺機器。 在某一點上,它發生這10臺機器全部同時請求int mycall(int a)
10個請求。 我想,以檢查這些場景:
方案1
...
/*** VARIANT 1 ***/
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContext = InstanceContextMode.Single)]
public class MyService : IMyService {
...
}
...
方案2
...
/*** VARIANT 2 ***/
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContext = InstanceContextMode.Single)]
public class MyService : IMyService {
...
}
...
所以,10個併發呼叫到達...是什麼在這兩種情況下發生?請告訴我,我是對還是錯:
在場景1中,單線程,這10個調用一次排隊並執行一個。我的服務(我的服務的相同實例)的方法將依次被調用十次。在場景2中,多線程的WCF將導致10個線程同時調用我的服務方法。
這是我說的嗎? 謝謝
在這兩種情況下,您的代碼都不會編譯,因爲'ServiceHost'既沒有'Start()'也沒有'Stop()'方法。 – 2011-05-20 21:01:00