我是 WCF 中状态/并发管理的初学者。假设我有一个如下服务类:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
private int _myCounter;
private MyType _myType;
private static MyType _myTypeStatic;
private static Dictionary<string,string> _d = new Dictionary<string,string>();
public void Method1()
{
// logic to set _myType
// logic to set _myCounter
// logic to set _myTypeStatic
// logic to set _d
}
public void Method2()
{
// logic to save _myType, set in Method1() to db
// logic to save _myCounter, set in Method1() to db
// logic to save _myTypeStatic, set in Method1() to db
// logic to save _d, set in Method1() to db
}
}
如果 2 个客户端连接同时使用此服务,它们将在 2 个不同的线程上运行。由于只有服务实例,在这种情况下 2 个全局类级别变量的状态将如何变化?它如何用于静态变量和类型?这 2 个线程是否有可能拥有 2 个不同的变量数据副本,但仍使用一个服务实例?