如果我使用这个:InstanceContextMode.PerSession,那么对于每个客户端,我都会初始化我的服务一次。我对吗?
如果我调用具有 [OperationBehavior(TransactionScopeRequired = true)] 的方法,则会调用 JobImplement 构造函数。为什么?
我的服务:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class JobImplement : IJob
{
public static int Sum = 0;
public JobImplement()
{
Sum++;
}
[OperationBehavior(TransactionScopeRequired = true)]
public void UselessMethod1()
{
}
[OperationBehavior(TransactionScopeRequired = true)]
public void AddEmployee(string Name, string Age)
{
}
public int GetSum()
{
return Sum;
}
}
编辑1:
我使用 WSHttpBinding 绑定,这是我唯一的客户端代码。
我的客户:
static void Main(string[] args)
{
ServiceReference1.IJob Service1 = new ServiceReference1.JobClient();
Service1.UselessMethod1();//sum become [1]
Service1.AddEmployee("","");//sum become [2]
Console.WriteLine(Service1.GetSum());//show [2].
Console.WriteLine(Service1.GetSum());//show [2].
Console.WriteLine(Service1.GetSum());//show [2].
}