WCF实例管理 - PerSession模式 [英] WCF Instance Management - PerSession Mode

查看:96
本文介绍了WCF实例管理 - PerSession模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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实例模式:每个会话);
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

解决方案

我认为最好的解释方法是通过这些图表(借鉴关于主题的CodePlex文章):





grock的主要概念是,当给定客户端在PerSession模式下创建一个服务的代理时,从该代理对该服务的重复调用将调用同一个服务实例(特定于该客户端)。



这与PerCall和Singleton形成对比,如下所示:

/ p>

在创建PerCall服务的代理时,每次调用服务操作时,都会获得一个新的服务对象实例。



当您创建SingleTon服务的代理时,只有该服务的一个实例处理所有客户端请求。


I am pretty new in WCF and trying to understand various instance management techniques, I am able to understand Per-Call & Singleton instance mode but i am confused in per session instance mode, In this case for every client a separate session is created right? But its not happening in my case:

My WCF Service:-

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession )]
public class CounterService : ICounterService
{
    int _counter = 0;
    public int GetCount()
    {
        _counter++;
        return _counter;
    }
}

Client Code:-

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();
    }

But, console is displaying result as 1,1,1---1,1,1 but i think it should be 1,2,3---1,2,3 Am i wrong somewhere? Please Help! TIA

解决方案

I think perhaps the best way to explain it is via these diagrams (borrowed from a CodePlex article on the topic):

The main concept to grock is that when a given client creates a proxy to a service in the PerSession mode, repeated calls to that service from that proxy will be calling the same service instance (specific to that client). This allows you to store some state in your service object for a client, since each client gets their own service object instance.

This contrasts with PerCall and Singleton as follows:

When you create a proxy to a PerCall service, you get a fresh, new instance of the service object every time you invoke a service operation.

When you create a proxy to a SingleTon service, there is only one instance of that service handling all client requests.

这篇关于WCF实例管理 - PerSession模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆