?
Solved

What are the minimal requirements for an object which can be placed into this type of HttpSessionState?

Posted on 2008-06-25
13
Medium Priority
?
337 Views
Last Modified: 2013-12-16
Instances of the class (written in C#)  must be placed into the session of an ASP.NET application deployed to a server cluster with distributed sessions.  What are the minimal requirements for an object which can be placed into this type of HttpSessionState?
0
Comment
Question by:niceguy971
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
ID: 21872245
Something that developers need to also pay close attention to is SerializableAttribute. When running InProc, any CLR type can be shoved into the session object without much consideration and be confident of retrieval in a subsequent page request. However, developers often find themselves with serialization problems when migrating a system to production where one of the two out of process modes is selected. This is because types that are to be stored in the HttpSessionState object must be marked with the SerializableAttribute when a setting other than InProc is used (since they will need to be marshalled between processes) e.g. : [Serializable] class Dog { } A good recommendation is for developers to assume that deployment will be an out of process mode of operation. Developers should use StateServer IMHO, that way, they will catch serialization problems at development time and not at deployment time.

REF: http://blogs.msdn.com/tims/archive/2003/11/21/57453.aspx
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx

As always the smaller the session the better the performace. 100KB or less.
0
 

Author Closing Comment

by:niceguy971
ID: 31470861
Thank you very much!!!
I have completed the following exercise & would like to know your opinion. Is there a better solution???

Exercise:
Instances of the following class must be placed into the session of an ASP.NET application deployed to a server cluster with distributed sessions and used as a key for a Hashtable of other
objects. Make the necessary modifications to the UserKey so that it meets the minimal requirements for an object which can be placed into this type of HttpSessionState and ensure the test code following the class definition below produces the indicated results. Modify the class to
use properties and provide full documentation.

namespace com.landsafe.eval
{
public class UserKey
{
private readonly String name;
private readonly String userid;
public UserKey(String name, String userid)
{
this.name = name;
this.userid = userid;
}
public String GetName()
{
return name;
}
public String GetUserID()
{
return userid;
}
}
}
Test Code (ensure the results are as indicated in the comments).
NOTE: Do NOT modify this test code. Ensure your code can return the indicated results without
any alteration to this test suite.
UserKey b1 = new UserKey(Bill Smith, BSMITH);
UserKey b2 = new UserKey(Bill Smith, BSMITH);
UserKey b3 = new UserKey(Susan Smith, SSMITH);

UserKey b4 = new UserKey(null,null);
System.Console.WriteLine( b1.Equals(b1) ); // prints true
System.Console.WriteLine( b1.Equals(b2) ); // prints true
System.Console.WriteLine( b1.Equals(b3) ); // prints false
System.Console.WriteLine( b1.Equals(null) ); // prints false
System.Console.WriteLine( b1.Equals(Some String) ); // prints false
System.Console.WriteLine( b4.Equals(b1) ); // prints false
Hashtable ht = new Hashtable();
ht.Add(b1,Some Data);
String s = (String) ht[b1];
System.Console.WriteLine( s.Equals(Some Data) ); // prints true
//My solution:
using System;
using System.Collections;
using System.Runtime.Serialization;


namespace com.landsafe.eval
{


[Serializable()]
public class UserKey
{
            public String name; // private readonly String name;
            public String userid; // private readonly String userid;

            public UserKey(String name, String userid)
            {
                  this.name = name;
                  this.userid = userid;
            }

            
                /*
                public String GetName()
            {
                  return name;
            }
            public String GetUserID()
            {
                  return userid;
            }  */


                  public bool Equals(UserKey p)  // I added
            {
                  // If parameter is null return false:
                  if ((object)p == null)
                  {
                        return false;
                  }

                  // if p is not a UserKey reference ...return false

                        if (! ReferenceEquals( this.GetType(),p.GetType() ))
                        {
                            return false;
                        }


                  // Return true if the fields match:
                  return (this.name == p.name) && (this.userid == p.userid);
            }


}

class Test3
{

      public static void Main()
        {

            UserKey b1 = new UserKey("Bill Smith", "BSMITH");
            UserKey b2 = new UserKey("Bill Smith", "BSMITH");
            UserKey b3 = new UserKey("Susan Smith", "SSMITH");

            UserKey b4 = new UserKey(null,null);
           
           System.Console.WriteLine("They want --true");                   // I added
                  System.Console.WriteLine( b1.Equals(b1) ); // prints true
           System.Console.WriteLine(""); // I added

           System.Console.WriteLine("They want --true");                   // I added
                  System.Console.WriteLine( b1.Equals(b2) ); // prints true
           System.Console.WriteLine(""); // I added
                  
               System.Console.WriteLine("They want --false"); // I added
                  System.Console.WriteLine( b1.Equals(b3) ); // prints false
           System.Console.WriteLine(""); // I added

           System.Console.WriteLine("They want --false"); // I added
            System.Console.WriteLine( b1.Equals(null) ); // prints false
           System.Console.WriteLine(""); // I added


               System.Console.WriteLine("They want --false"); // I added
                  System.Console.WriteLine( b1.Equals("Some String") ); // prints false
           System.Console.WriteLine(""); // I added

           System.Console.WriteLine("They want --false"); // I added
            System.Console.WriteLine( b4.Equals(b1) ); // prints false
           System.Console.WriteLine(""); // I added
           
                  Hashtable ht = new Hashtable();
            ht.Add(b1,"Some Data");
            String s = (String) ht[b1];
           
                  
               System.Console.WriteLine("They want --true"); // I added
                  System.Console.WriteLine( s.Equals("Some Data") ); // prints true
           System.Console.WriteLine(""); // I added


      }

}

} //end namespace


0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 21891276
Here you go mate. This code sample uses properties and produces the desired result. I have overidden the other necessaries from the object class and added a default constructor which is necessary for XML serialization (this may not be need however).
using System;
using System.Collections;
using System.Runtime.Serialization;
 
namespace com.landsafe.eval
{
	[Serializable()]
	public class UserKey
	{
		private String _name;	// private readonly String name; 
		private String _userId;	// private readonly String userid;
 
		public String Name
		{
			get { return _name; }
			set { _name = value; }
		}
 
		public String UserId
		{
			get { return _userId; }
			set { _userId = value; }
		}
 
		/// <summary>
		/// Default constructor necessary for 
		/// XML serialization
		/// </summary>
		public UserKey()
		{
			_name = String.Empty;
			_userId = String.Empty;
		}
 
		public UserKey(String name, String userId)
		{
			this._name = name;
			this._userId = userId;
		}
 
		public override bool Equals(object obj)
		{
			if ((object)obj == null)
				return false;
 
			if(!(obj is UserKey))
				return false;
 
			return (this._name == (obj as UserKey)._name) && (_userId == (obj as UserKey)._userId);
		}
 
		public override string ToString()
		{
			return String.Format("{0}: {1}", _userId, _name);
		}
 
		public override int GetHashCode()
		{
			return this.ToString().GetHashCode();
		}
	}
}

Open in new window

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:niceguy971
ID: 21893144
If I move from Net Framework v1.1 to v2.0 ...do I need to make changes in the code above?
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 21899203
Sorry for taking so long to respond. I didn't realise you posted a comment. Yes this code definitely works in 2.0, 3.0, 3.5 and I am reasonably certain in works in 1.1.
0
 

Author Comment

by:niceguy971
ID: 21904882
Thank You very much!!!  I would like to know your opinion--please see my question below:

Implement the Employee and Manager classes as showen on the diagram (the diagram.doc file). The classes should be in the com.landsafe.eval package, be public, and implement constructors that take the values to initialize all their attributes.  The classes should be serializable.
************
I wrote the code. Is there a better solution??

using System;
using System.Collections;
using System.Runtime.Serialization;


namespace com.landsafe.eval
{


[Serializable()]
public class Employee
{

    private String _name;
    private DateTime _dateHired;
    private int _id;

//===========+DateHired() : DateTime====
    public DateTime getDateHired()
    {
        return _dateHired;
    }

    public void SetDateHired(DateTime dateHired)
    {
      this._dateHired = dateHired;
    }

//========+ID() : int=======
      public int GetId()
      {
         return _id;
      }


      public void SetId(int id)
      {
         this._id=id;
      }

//========+Name() : String======
      public String GetName()
      {
         return _name;
      }

      public void SetName(String name)
      {
         this._name=name;
      }



    public Employee(String name, DateTime dateHired, int id) // should add default constructor?
    {
       this._name=name;
       this._dateHired=dateHired;
       this._id=id;      

    }


    public override bool Equals(object obj) // should I do it?-not shown on the diagram
    {
      if ((object)obj == null)
      return false;
 
      if(!(obj is Employee))
      return false;
 
      return (this._name == (obj as Employee)._name) && (this._id == (obj as Employee)._id) && (this._dateHired == (obj as Employee)._dateHired);
    }


    public override string ToString() // should I do it?-not shown on the diagram
    {
        return (this._name + " " +this._dateHired + " "+this._id );

    }


   public override int GetHashCode()
   {
      return this.ToString().GetHashCode();
   }
   

}  //End of class Employee



[Serializable()]
public class Manager : Employee
{
    private decimal _bonus;

      public decimal GetBonus()
      {
         return _bonus;
      }


      public void SetBonus(decimal bonus)
      {
         this._bonus=bonus;
      }



    public Manager(String name, DateTime dateHired, int id,decimal bonus):base(name, dateHired, id)
    {
       this._bonus=bonus;  

    }


} //End of class Manager


class Test5
{

           public static void Main()
        {


             Console.WriteLine("Inside of Main mathod");

             DateTime hiredDate,d2;
             hiredDate = new DateTime(2008, 1, 2, 3, 4, 5);    // Date = 2 Jan 2008 03:04:05
             d2= new DateTime(2008, 3, 2, 3, 4, 5);

             Employee bob= new Employee("Bob",hiredDate, 12345);
             Console.WriteLine("bob's _name: "+ bob.GetName());
             Console.WriteLine("bob's _id: "+ bob.GetId());

   
             decimal bon = 300.5m;

             Manager john=new Manager("John", d2, 98765, bon);
             Console.WriteLine("john's _name: "+ john.GetName());
            Console.WriteLine("john's _id: "+ john.GetId());

            Console.WriteLine("john's _bonus: "+ john.GetBonus());
             
           
        }



}




} // End Namespace
diagram.doc
Test5.txt
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 21905781
Here you go mate. To note that this is a lot of work for 125 points. Is this your assignment?
using System;
using System.Collections;
using System.Runtime.Serialization;
 
namespace com.landsafe.eval
{
	[Serializable]
	public class Employee
	{
		#region Fields & Properties
 
		private String _name;
		private DateTime _dateHired;
		private int _id;
 
		/// <summary>
		/// Gets or sets the date employee was hired
		/// </summary>
		public DateTime DateHired
		{
			get { return _dateHired; }
			set { _dateHired = value; }
		}
 
		/// <summary>
		/// Gets or sets the employee id
		/// </summary>
		public int Id
		{
			get { return _id; }
			set { _id = value; }
		}
 
		/// <summary>
		/// Gets or sets the name of the employee
		/// </summary>
		public String Name
		{
			get { return _name; }
			set { _name = value; }
		}
 
		#endregion
 
		#region Constructors
 
		/// <summary>
		/// Use a default constructor in this case
		/// only if XML Serialization is required - 
		/// remove otherwise asn question does not
		/// state that you need one.
		/// </summary>
		public Employee()
			: this("Unknown", DateTime.Now, -1)
		{
		}
 
		/// <summary>
		/// Construct employee object.
		/// </summary>
		public Employee(String name, DateTime dateHired, int id)
		{
			_name = name;
			_dateHired = dateHired;
			_id = id;
		}
 
		#endregion
 
		/// <summary>
		/// This is good practice. Its not shown on the diagram 
		/// however its a sensible addition to the class.
		/// </summary>
		public override bool Equals(object obj)
		{
			if ((object)obj == null)
				return false;
 
			if (!(obj is Employee))
				return false;
 
			return (this._name == (obj as Employee)._name) && 
				(this._id == (obj as Employee)._id) && 
				(this._dateHired == (obj as Employee)._dateHired);
		}
 
		/// <summary>
		/// This is good practice. Its not shown on the diagram 
		/// however its a sensible addition to the class.
		/// </summary>
		public override string ToString()
		{
			// Use String.Format / String.Concat to combat the
			// fact that .NET string are immutable.
			return String.Format("{0} {1} {2}", this._name, this._dateHired, this._id);
		}
 
		public override int GetHashCode()
		{
			return this.ToString().GetHashCode();
		}
	}
 
 
	[Serializable]
	public class Manager : Employee
	{
		#region Fields & Properties
 
		private decimal _bonus;
 
		public decimal Bonus
		{
			get { return _bonus; }
			set { _bonus = value; }
		}
 
		#endregion
 
		#region Constructors
 
		public Manager(String name, DateTime dateHired, int id, decimal bonus)
			: base(name, dateHired, id)
		{
			_bonus = bonus;
		}
 
		#endregion
	}
 
 
	class Test5
	{
		public static void Main()
		{
			Console.WriteLine("Inside of Main mathod");
 
			// Date = 2 Jan 2008 03:04:05
			Employee bob = new Employee("Bob", new DateTime(2008, 1, 2, 3, 4, 5), 12345);
			Console.WriteLine("bob's name: {0}", bob.Name);
			Console.WriteLine("bob's id: {0}\n",  bob.Id);
 
			Manager john = new Manager("John", new DateTime(2008, 3, 2, 3, 4, 5), 98765, 300.50M);
			Console.WriteLine("john's name: {0}", john.Name);
			Console.WriteLine("john's id: {0}", john.Id);
			Console.WriteLine("john's bonus: £{0}", john.Bonus);
 
			// Pause
			Console.ReadLine();
		}
	}
}

Open in new window

0
 

Author Comment

by:niceguy971
ID: 21913292
Thank You very much!!!  How can I add points???  It's not my assignment. I'm trying to learn C# deeper & was going thru some exersices.  I recently found this website.
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 21923764
I don't know how that is done, without opening a question.
0
 

Author Comment

by:niceguy971
ID: 21931165
OK...I'm ready to ask another question (and assign the max amount of points)...Could you please take a look? Thanks
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 21931703
Ask away
0
 

Author Comment

by:niceguy971
ID: 21935647
I submitted a new Question (Use try..catch...)- 500 points. Please take a look at open questions. Is it a good programming practice? What's your opinion? Thanks

Just in case- the same code is below:
using System;
using System.IO;

public class ExceptionTest
{

  public static void Main()  
  {
     int i=-5;

     try
     {
        if (i==-5)
          throw new Exception("My exception");

     }
     catch (Exception e)
     {
           Console.WriteLine("MESSAGE: " + e.Message);

     }
     finally
     {
       Console.WriteLine("Inside Finally.");
     }

     Console.WriteLine("End of application");
 
  }
}

0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 21936331
I have left an answer, however the code examples provided were sufficient. Please feel free to ask any question easy or complicated.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question