简体   繁体   中英

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

I implements a service, built by using "WCF Service Application". The application has also two clients, and ment to work as a duplex. (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. I couldn't find the reason for that. I also tried to set the InstanceContextMode to other then single, but that didn't help. Thanks, Liron.

That's because you're using static field, which is shared between all calls.

And because of 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.

Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created.

from InstanceContextMode Enumeration

You should probably go with InstanceContextMode.PerSession instead, and change your field to instance:

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

Lets take WCF out of the picture. 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 .

WCF does not modify the behavior of how static works. 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.

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 . 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 .

Now apply this to your 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. 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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