views:

109

answers:

3

In my code I'm using static field for storing a particular value.

public static int webServiceId;

I have remove that and use any other solution. But the value should retain after postback. We can't use Session or ViewState here. Coz I'm working with services (in Service layer).

Example:

I get webservice id in the below method of xyz.cs file:

public int SetWebServiceInformation(int? webServiceID, string webServiceName) 
{ 
    context.WebService_InsertUpdate(ref webServiceID, webServiceName);
         webServiceId = webServiceID.Value;
         return webServiceID.Value;

}

and then controls goes to another method of different class file (say abd.cs file).And suppose in calling method exception comes it will call method LogError(Exception error) of my first class file (xyz.cs). And when the control comes back in our class file (xyz.cs) we need the webservice Id back. Coz we are using that to store exception info into the database according to the webservice Id.

protected void LogError(Exception error)
{
    ----//some logic to get errorLogID//---

    if (errorLogID > 0)
          WebServiceErrorLogging(errorLogID, webServiceId);
    //here we have webServiceId is a static variable
}
A: 

You could wrap the webServiceId in a Singleton class.

public sealed class WebServiceSettings
{
    private static readonly WebServiceSettings instance=new WebServiceSettings();
    private static int webServiceId;

    static WebServiceSettings()
    {
         webServiceId = //set webServiceId
    }

    private WebServiceSettings(){}

    public static WebServiceSettings Current
    {
        get
        {
            return instance;
        }
    }

    public int WebServiceId {get{return webServiceId;}}
}

You can then call the Id:

WebServiceSettings.Current.WebServiceId;

This class essentially ensures that it is only constructed once (in C#, static constructors are guarenteed to be called once). Therefore, the code in the constructor to populate the WebServiceId will only run once.

David Neale
David, Can you little more explain this to me. Actually I didn't get what you are trying to explain me here. It really helps me if you can write some piece of code here.
John Smith
David, this only helps if you can show a Singleton w/o a static var underneath.
Henk Holterman
What's the problem with using a Singleton in this context? Surely the ID needs to be created only once.
David Neale
David, see http://stackoverflow.com/questions/2426820/wcf-static-variable-getting-reset-with-each-call
Henk Holterman
@Henk, what you suggest me to do for the static variable problem? coz I didn't get any solution yet for that.
John Smith
@John: see @Paw Baltzersen's answer, follow it to the blog.
Henk Holterman
A: 

Have wou considered implementing a Singleton?

That way you can have a "global" class for storing the parameters.

using System.Runtime.CompilerServices;

public class Singleton
{
    private static Singleton Instance = null;
    static readonly object padlock = new object();


    // The private constructor doesnt allos a default public constructor
    private Singleton() {}

    // Synchronized "constructor" to make it thread-safe
    [MethodImpl(MethodImplOptions.Synchronized)]
    private static void CreateInstance()
    {
        lock(padlock)
        {
             if (Instance == null)
             { 
                  Instance = new Singleton();
             }
        }
    }

    public static Singleton GetInstance()
    {
        if (Instance == null) CreateInstance();
        return Instance;
    }

    public int webServiceId {get; set;}


}

// Test class
public class Prueba
{
   private static void Main(string[] args)
   {
     //Singleton s0 = new Singleton();  //Error
     Singleton s1 = Singleton.GetInstance();
     Singleton s2 = Singleton.GetInstance();
     if(s1==s2)
     {
       // Misma instancia
     }
   }
}

The code above implements a class that when instanced (via the GetInstance() static method) returns an unique instance of the class.

Juan Nunez
Already suggested.
David Neale
@David Neale - Others have the freedom to suggest the same answer as you. There is no convention on SO that disallows it.
Oded
I was writing it while he posted it :)Should I delete it?
Juan Nunez
Juan, no need to delete because of David's post. But this is just a wrapper around a static var, problem not solved.
Henk Holterman
In the singleton you could make the var as public or use an autoimplemented property
Juan Nunez
how about having that way the service id?You could access it like (Singleton.GetInstance()).webServiceId
Juan Nunez
+1  A: 

http://stackoverflow.com/questions/2449158/session-variable-in-wcf-application

Maybe this will help, look at RandomNoob's answer.

Paw Baltzersen
It works.. thanks
John Smith