WCF Static Variable Get Reset on Each Call

I have a WCF service that I call from several clients. I need to store and manage value all over the world. On my service, I have the following attributes:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]

In my service, I have something similar to this:

private static int counter;

public void PrintCounter()
{
    counter++;
    StreamWriter sw = new StreamWriter(@"C:\outFile.txt", true);
    sw.WriteLine("Counter: " + counter);
    sw.Close();
}

Given my limited knowledge of WCF, I would suggest that I have a Singleton service and the fact that my private variable is static, that all service calls will use the same object.

However, when I look at the output of the log, I see the following:

Counter: 1
Counter: 1

What I expected to see would be as follows:

Counter: 1
Counter: 2

Did I miss something to make this work the way I need? Do I need to store objects in some kind of cache? Any help is appreciated.

I can post more code if necessary.

+3
2

, . - , - ?

ConcurrencyMode , .

- singleton ConcurrencyMode = Single - , , , ConcurrencyMode = Multiple -.

+2

, , - .

, IIS - ?

, Interlocked.Increment , , ( , ,

10003120
10003121
10003121
10003121
10003122
0

Source: https://habr.com/ru/post/1736519/


All Articles