balanv
balanv

Reputation: 10898

Global Variable between two WCF Methods

I have two Methods in a WCF Service say

Method1()
{
 _currentValue = 10;
}

Method2()
{
return _currentValue;
}

I have a situation in which, i need to set a value in Method1() and read it in Method2().

I tried using static variable like public static int _currentValue, i could able to read the value set in Method1() in Method2().

But the issue is, i want this variable to react like separate instance variable for each request made. i.e., right now below is the problem

Browser 1 :

 - Method1() is called
    => sets _currentValue = 10;
 - Method2() is called
    => returns _currentValue = 10;

Browser 2:

 - Method2() is called
    => returns _currentValue = 10;

Actually the value set is Browser 1 is static, so in Browser 2 the same value is retrieved.

What i am trying to implement is the variable should act like a new instance for each request made (when calling from each browser). What should i use in this case? a session?

Upvotes: 6

Views: 9136

Answers (5)

ManojPatra
ManojPatra

Reputation: 114

Seems like an old thread but in case somebody is still interested, this can be achieved by just asking WCF to run a single instance of your service. Add the following line (decorator) to the definition of your class [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

If you want the behavior only for the same session but not across clients then you can mark it as per session by the following service behavior [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

The other option is per call which is the default option. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

Upvotes: 0

Chitta
Chitta

Reputation: 206

WCF has provided three ways by which you can control WCF service instances:

  • Per call
  • Persession
  • Single instance

You will find the best solution by reading this

Three ways to do WCF instance management

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

You're going to need some mechanism for correlation because you have two completely different sessions calling into different methods. So I would recommend using a private key that both callers know.

It is a bit impossible for me to know what that key can be because I can't really gather anything from your question, so only you know that, but the simple fact is you're going to need correlation. Now, once you determine what they can use you can do something like this.

public class SessionState
{
    private Dictionary<string, int> Cache { get; set; }

    public SessionState()
    {
        this.Cache = new Dictionary<string, int>();
    }

    public void SetCachedValue(string key, int val)
    {
        if (!this.Cache.ContainsKey(key))
        {
            this.Cache.Add(key, val);
        }
        else
        {
            this.Cache[key] = val;
        }
    }

    public int GetCachedValue(string key)
    {
        if (!this.Cache.ContainsKey(key))
        {
            return -1;
        }

        return this.Cache[key];
    }
}

public class Service1
{
    private static sessionState = new SessionState();

    public void Method1(string privateKey)
    {
        sessionState.SetCachedValue(privateKey, {some integer value});
    }

    public int Method2(string privateKey)
    {
        return sessionState.GetCachedValue(privateKey);
    }
}

Upvotes: 3

Mike Payne
Mike Payne

Reputation: 624

It sounds like you may need to use the per session instance context mode for the WCF service. This will allow you to maintain state on a per session basis, so member variables in the service instance will persist between method calls from the same proxy instance. Because each user has their own session, the state of the service instance will vary by user.

Check out this article for more information: http://msdn.microsoft.com/en-us/magazine/cc163590.aspx#S2

Upvotes: 3

Dan Puzey
Dan Puzey

Reputation: 34198

You have made your variable static, and this is what's causing the problem. static means that every instance of your class shares the variable, but all you really need is a variable declared outside of your methods, like this:

private int _currentValue;

Method1() 
{ 
    _currentValue = 10; 
} 

Method2() 
{ 
    return _currentValue; 
} 

This variable will be reated separately for each instance of your class - preserving this value between requests for a given user is a separate problem. (A session is one possible solution.)

Upvotes: 2

Related Questions