简体   繁体   English

双工Wcf服务应用程序,列表保留上次运行的应用程序的值

[英]Duplex Wcf Service Application, List keep values from last application running

I implements a service, built by using "WCF Service Application". 我实现了通过使用“ WCF服务应用程序”构建的服务。 The application has also two clients, and ment to work as a duplex. 该应用程序还具有两个客户端,并且可以作为双工使用。 (wsDualHttpBinding) (wsDualHttpBinding)

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
    private static List<int> lst = new List<int>();
    public void Subscribe()
    {
          lst.Add(1);
          //....

The strange thing is, When i run the application, the List 'remember' its values from last running.. and the lst.Count gets bigger and bigger from complete different runnings of the whole application. 奇怪的是,当我运行该应用程序时,列表从上次运行时“记住”其值..而lst.Count从整个应用程序的完全不同运行中变得越来越大。 I couldn't find the reason for that. 我找不到原因。 I also tried to set the InstanceContextMode to other then single, but that didn't help. 我还尝试将InstanceContextMode设置为其他然后是single,但这没有帮助。 Thanks, Liron. 谢谢,利隆。

That's because you're using static field, which is shared between all calls. 这是因为您使用的是静态字段,该字段在所有调用之间共享。

And because of InstanceContextMode = InstanceContextMode.Single . 并且由于InstanceContextMode = InstanceContextMode.Single It makes your service work in singletone mode, which mean there is only one instance of Service1 class created for all your clients and connections. 它使您的服务以单调模式工作,这意味着仅为您的所有客户端和连接创建了Service1类的一个实例。

Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. 所有传入呼叫仅使用一个InstanceContext对象,并且不会在呼叫之后回收。 If a service object does not exist, one is created. 如果服务对象不存在,则会创建一个。

from InstanceContextMode Enumeration InstanceContextMode枚举

You should probably go with InstanceContextMode.PerSession instead, and change your field to instance: 您可能应该改为使用InstanceContextMode.PerSession ,并将字段更改为instance:

public class Service1 : IService1
{
    private List<int> lst = new List<int>();

Lets take WCF out of the picture. 让我们将WCF带出图片。 If I had the following code what would you expect the output to be. 如果我有以下代码,您期望输出是什么。

public class Foo
{
    private static int number = 0;

    public int GetNumber()
    {
        number = number + 1;
        return number;
    }
}

public static Main()
{
    var foo1 = new Foo();
    Console.WriteLine(foo1.GetNumber());
    Console.WriteLine(foo1.GetNumber());

    var foo2 = new Foo();
    Console.WriteLine(foo1.GetNumber());
}

You should expect to see 1, 2, 3 . 您应该看到1, 2, 3

WCF does not modify the behavior of how static works. WCF不会修改static的行为。 If you have two instances of your class it still shares the static variable. 如果您有两个类的实例,它仍然共享静态变量。 What InstanceContextMode does is control how often new Foo() is done. InstanceContextMode作用是控制执行new Foo()频率。

Here is some more Example code showing the differences. 这是更多显示差异的示例代码。

public static Main()
{
    Console.WriteLine("1- PerCall");
    Console.WriteLine("2- Session");
    Console.WriteLine("3- Single");
    Console.Write("Choose: ");

    var choice = Console.ReadLine();

    switch(choice)
    {
        case "1":
            PerCallExample();
            PerCallExample();
            break;
        case "2":
            PerSessionExample();
            PerSessionExample();
            break;
        case "3":
            var foo = Foo();
            SingleExample(foo);
            SingleExample(foo);
            break;

    }
}

void Call(Foo foo)
{
    Console.WriteLine(foo.GetNumber());
}

void PerCallExample()
{
     Foo foo;

     foo = new Foo();
     Call(Foo foo);

     foo = new Foo();
     Call(Foo foo);
}

void PerSessionExample()
{
     Foo foo = new Foo();

     Call(Foo foo);
     Call(Foo foo);
}

void SingleExample(foo)
{
     Call(Foo foo);
     Call(Foo foo);
}

No-matter what you choose all 3 modes will output 1, 2, 3, 4 . 没关系,您选择的全部3种模式都会输出1, 2, 3, 4 However if you remove the static from number you should get 1, 1, 1, 1 from PerCall , 1, 2, 1, 2 for Session , and 1, 2, 3, 4 for Single . 但是,如果您删除staticnumber ,你应该得到1, 1, 1, 1 ,从PerCall1, 2, 1, 2用于Session ,和1, 2, 3, 4Single

Now apply this to your WCF. 现在将此应用到您的WCF。 Because your List is static it will be shared among all calls to your service until the service is next restarted, that is why your data is being kept over. 由于List是静态的,因此它将在所有服务调用之间共享,直到下次重新启动该服务为止,这就是为什么您的数据将保持不变的原因。 What you need to change it to instead of being a static list I can't say without knowing more of what you are wanting to do (However, changing it off of static and making it the instance context Single will have the same effect as being static, as you saw in the above example. So it is likely you don't want to use Single either.) 您需要将其更改为静态列表而不是静态列表的原因我不能不知道您想做的事情的更多信息(但是,将其更改为静态并使其成为实例上下文Single将具有与如上例所示,它是静态的。因此,您也可能不想使用Single 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM