Solved

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

Posted on 2008-06-25
13
316 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
  • 7
  • 6
13 Comments
 
LVL 18

Accepted Solution

by:
Richard Lee earned 125 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 28
How can I use this extension method? 8 36
Image(2) 3 26
Get list of word ducuments in a folder 10 17
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now