Amazon EC2 multiple servers share session state

Posted by Theofanis Pantelides on Stack Overflow See other posts from Stack Overflow or by Theofanis Pantelides
Published on 2010-03-12T08:20:45Z Indexed on 2010/03/12 8:27 UTC
Read the original article Hit count: 220

Filed under:
|
|

Hi everyone,

I have a bunch of EC2 servers that are load balanced. Some of the servers are not sharing session, and users keep getting logged in and out.

How can I make all the server share the one session, possibly even using a partitionresolver solution

    public class PartitionResolver : System.Web.IPartitionResolver
    {
        private String[] partitions;

        public void Initialize()
        {
            // create the partition connection string table
            //                           web1,            web2
            partitions = new String[] { "192.168.1.1" };
        }

        public String ResolvePartition(Object key)
        {
            String oHost = System.Web.HttpContext.Current.Request.Url.Host.ToLower().Trim();

            if (oHost.StartsWith("10.0.0") || oHost.Equals("localhost"))
                return "tcpip=127.0.0.1:42424";

            String sid = (String)key;

            // hash the incoming session ID into
            // one of the available partitions
            Int32 partitionID = Math.Abs(sid.GetHashCode()) % partitions.Length;

            return ("tcpip=" + partitions[partitionID] + ":42424");
        }
    }

-theo

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET