简体   繁体   中英

Handle Session in ClassLibrary C#

I am creating classlibrary project to track the session values.

I don't know how to achieve this, please guide me.

in global.asax file

session_Start() //event
{
  obj.Start(string val1,string val2);
 }

and

session_end()
{
   obj.End();
}

in classlibrary project

Class Class_Name
{
     public void Start(string val1,string val2)
      {
          guid=Guid.NewGuid(); //for unique id for each session,
          hastable and its values;
           session[guid]=hastable_values;
       }
   public void End()
      {
       hastable t=(hastable)  session[guid]; //here is the problem,its null.

       send the hastable data to database;

      }
  }

Thanks.

You seem to be confused by Session and ApplicationState .

At the moment the Session_Start runs you get a fresh container for that particular browser (due to the session cookie being generated). On every subsequent call the same session container is returned with the objects stored in it for this session. That container is removed when Session_End is called.

I'm not sure if obj is an instance variable or static variable in both cases your code will behave differently. My suggestion is to keep an instance of the type Class_Name in the session with a fixed key. That makes that you can always retrieve that instance and the asp.net framework handles loading the correct unique session container for each request.

Session_Start() //event
{
     var cn = new Class_Name();
     cn.Start(string val1,string val2);
     Session["class_name"] = cn;
}

Session_end()
{
    var cn = Session["class_name"] as Class_Name;
    if (cn !=null) 
    {
         cn.End();
    }
}

In your classlibrary project and types you can store what ever you want as long as you make sure the types are serializable (in case your instances need to be serialized in a State Server)

Class Class_Name
{
     Guid guid;
     HashTable hashtable_values;
     public void Start(string val1,string val2)
     {
          guid=Guid.NewGuid(); //for unique id for each session,
          //hastable and its values;
          // those are now already stored in the session 
     }

     public void End()
     {
        // send the hastable data to database;
        // hash_table has all the values still stored, process them at will
        // do realize that Session_End is not guaranteed to be called
        // so if you still want to store critical stuff in this phase
        // you might be missing data for some sessions    
     }
  }

You can bring this logic to your class if you add static factory like methods and an Instance property:

class Class_Name
{
    public static void StartSession(string val1, string val2)
    {
        var cn = new Class_Name();
        cn.Start(val1, val2);
        HttpContext.Current.Session["class_name"] = cn;
    }

    // all your classes that need access to theis session object will need 
    // to call this Instance property
    public static Class_Name Instance 
    {
       get 
       {
          return HttpContext.Current.Session["class_name"] as Class_Name;
       }
    }

    public static void EndSession()
    {
        var cn = Instance;
        if (cn != null) 
        {
             cn.End():
        }
    }      
}

and your global asax will become:

Session_Start() //event
{
     Class_Name.StartSession(string val1,string val2);
}

Session_end()
{
    Class_Name.EndSession();
}

Overview of Application and Page life cycle on msdn.

If you want to access session in class used HttpContext.Current.Session . check this link for more information

Class Class_Name
{
     public void Start(string val1,string val2)
      {
          guid; //for unique id for each session,
          hastable and its values;
           HttpContext.Current.Session[guid]=hastable_values;
       }
   public void End()
      {
       hastable t=(hastable)  HttpContext.Current.Session[guid]; //here is the problem,its null.

       send the hastable data to database;

      }
  }

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