0

I have the following simple WCF library which was developed on Visual Studio 2008.
When running WCFTestClinet/javascript(with SOAP) that calls this wcf service I get false value for the following scenario:
1. GetNumber --> output: "Your number is 0"
2. SetNumber --> No output
3. GetNumber --> output: "Your number is 0" instead of output: "Your number is 8" !!!
Can anyone explain why is this happening and how can I solve it?
Thanks

public class Service1 : IService1
    {
        private int Number;

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }

        public string GetNumber()
        {
            return string.Format("Your number is : {0}", Number);
        }

        public void SetNumber()
        {
            Number = 8;
        }
    }
liorafar
  • 2,264
  • 4
  • 19
  • 39
  • Each call to the service creates a new instance of Service1 therefore Number is always reinitialized. One 'quick' solution would be to mark Number as static... – Eric Dec 04 '12 at 13:58

5 Answers5

5

It's all about instances. One instance of your service will by default be instantiated per session; but depending on the configuration (for example using the BasicHttpBinding) the service may be instantiated per call (and/or not even support sessions at all).

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thanks this article was very helpfull ! – liorafar Dec 04 '12 at 15:27
  • How can I share memory between sessions without using static members? and if I have static member is it shared between different WCF clients or is it shared only between the client sessions? – liorafar Dec 05 '12 at 08:08
  • @liorafar you usually don't, but it totally depends on the situation. Please keep in mind WCF services usually are stateless and don't know each other's existence. You could try looking into a RDBMS like SQL Server to store data you want to share. If you can explain exactly what you're trying to do, you could open a new question, but not without showing some research effort. – CodeCaster Dec 05 '12 at 08:11
  • Hi and thanks again for your quick response ! if it is nesseccary I will open new question. but the point is that my WCF service suppose to be statefall and not stateless. and this I'm not able to achieve, and that is why I asked it. – liorafar Dec 05 '12 at 08:17
1

Probably because you have configured your WCF service to be per call instead of per session.

Great answer telling the differences: https://stackoverflow.com/a/2542991/70386

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • After i tested it I saw that it works only for InstanceContextMode.Single and not InstanceContextMode.PerSession , however i need to use perSession. why does not it share memory for the same wcf service? How can I share memory between sessions without using static members? and if I have static member is it shared between different WCF clients or is it shared only between the client sessions? Thanks. – liorafar Dec 05 '12 at 15:00
0

Is this in the same instance? As Number will always be set to 0 otherwise

CR41G14
  • 5,464
  • 5
  • 43
  • 64
0

I haven't used the SOAP WCF, but in my work with other web services my understanding is that each time an invocation is made to the service a new instance is created. This means that whatever you did on SetNumber is not there for the following GetNumber as it is a new instance.

If you wanted, you could make that value static to preserve the changes between calls.

Jay S
  • 7,904
  • 2
  • 39
  • 52
0

This is because by default, WCF 3.5's instance mode is PerCall. This means that for every call WCF receives, it creates a new instance of the service class, performs the call, and then destroys that instance.

If you want to have shared values you can configure your service to be a singleton, like so:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class  Service1 : IService1
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76