【发布时间】:2011-05-19 22:07:39
【问题描述】:
我想了解ServiceBehavior.ConcurrencyMode 属性。
在服务端考虑以下代码:
[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 台机器会同时发出 10 个对int mycall(int a) 的请求。
我想检查一下这些场景:
场景 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 个调用排队并一次执行一个。我的服务(THE SAME INSTANCE OF MY SERVICE)的方法会被依次调用十次。场景二,多线程,WCF会导致10个线程同时调用我的服务方法。
我说的是真的吗? 谢谢
【问题讨论】:
-
在这两种情况下,您的代码都不会编译,因为
ServiceHost既没有Start()也没有Stop()方法。