我在 WCF 中很新,并试图了解各种实例管理技术,我能够理解 Per-Call 和 Singleton 实例模式,但我对每个会话实例模式感到困惑,在这种情况下,为每个客户端创建一个单独的会话,对吗?但在我的情况下它没有发生:
我的 WCF 服务:-
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession )]
public class CounterService : ICounterService
{
int _counter = 0;
public int GetCount()
{
_counter++;
return _counter;
}
}
客户代码:-
static void Main(string[] args)
{
CounterServiceReference.CounterServiceClient proxy = new CounterServiceReference.CounterServiceClient();
CounterServiceReference.CounterServiceClient proxy1 = new CounterServiceReference.CounterServiceClient();
Console.WriteLine("WCF Instance mode: Per Session");
Console.WriteLine("Invoking WCF service...");
Console.WriteLine("Counter: {0}", proxy.GetCount());
Console.WriteLine("Counter: {0}", proxy.GetCount());
Console.WriteLine("Counter: {0}", proxy.GetCount());
Console.WriteLine("---------------------------------------");
Console.WriteLine("Counter: {0}", proxy1.GetCount());
Console.WriteLine("Counter: {0}", proxy1.GetCount());
Console.WriteLine("Counter: {0}", proxy1.GetCount());
Console.ReadKey();
}
但是,控制台显示结果为 1,1,1---1,1,1 但我认为它应该是 1,2,3---1,2,3 我在某处错了吗?请帮忙!TIA