dynamically select StateServer based on machinename (for PROD,QA,DEV selection)

First go have a look at Maartens post.

My purpose – to use one line in web config, but use the correct stateserver ip address when deployed.
And I tested this to make sure it worked.

web config entry

VB version (I’m running)

Imports System

' purpose - dynamically return a state server ip address based on DEV/PROD/QA deployment
' ref http://blog.maartenballiauw.be/post/2008/01/23/ASPNET-Session-State-Partitioning.aspx
Public Class WebConfigSessionStateServer
Implements System.Web.IPartitionResolver

Private Shared m_DynamicStateServerAddress As String = ""
Private ReadOnly Property DynamicStateServerAddress As String
Get
If (String.IsNullOrEmpty(m_DynamicStateServerAddress)) Then Initialize()

Return m_DynamicStateServerAddress
End Get
End Property

Public Function ResolvePartition(key As Object) As String Implements IPartitionResolver.ResolvePartition

Return DynamicStateServerAddress

End Function

Private Sub Initialize() Implements IPartitionResolver.Initialize
Dim sDEV As String = "tcpip=666.666.238.252:42424"
Dim sPROD As String = "tcpip=666.666.79.176:42424"
Dim sQA As String = "tcpip=666.666.238.249:42424"

Select Case DeploymentType
Case "QA"
m_DynamicStateServerAddress = sQA
Case "DEV"
m_DynamicStateServerAddress = sDEV
Case "PROD"
m_DynamicStateServerAddress = sPROD
Case Else
m_DynamicStateServerAddress = sPROD
End Select

End Sub

Private Shared m_DeploymentType As String = ""
Public Shared ReadOnly Property DeploymentType As String
Get
If (m_DeploymentType = "") Then
Dim sname As String = Environment.MachineName 'utils_SysNetInfo.machinename
m_DeploymentType = "PROD"
If (sname.ToUpper().IndexOf("DEV") > -1) Then m_DeploymentType = "DEV"
If (sname.ToUpper().IndexOf("QA") > -1) Then m_DeploymentType = "QA"
End If

Return m_DeploymentType

End Get
End Property

End Class

C# version (translated by Telerik. Bless them. Buy things from them What a great service.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace utils_lib
{

// purpose - dynamically return a state server ip address based on DEV/PROD/QA deployment
// ref http://blog.maartenballiauw.be/post/2008/01/23/ASPNET-Session-State-Partitioning.aspx
public class utils_SessionStateServer_Dynamic : System.Web.IPartitionResolver
{

private static string m_DynamicStateServerAddress = "";
private string DynamicStateServerAddress
{
get
{
if ((string.IsNullOrEmpty(m_DynamicStateServerAddress)))
Initialize();

return m_DynamicStateServerAddress;
}
}

// ************************************************************
public string ResolvePartition(object key)
{

return DynamicStateServerAddress;

}

// ************************************************************
public void Initialize()
{
string sDEV = "tcpip=666.666.238.252:42424";
string sPROD = "tcpip=666.666.79.176:42424";
string sQA = "tcpip=666.666.238.249:42424";

switch (DeploymentType)
{
case "QA":
m_DynamicStateServerAddress = sQA;
break;
case "DEV":
m_DynamicStateServerAddress = sDEV;
break;
case "PROD":
m_DynamicStateServerAddress = sPROD;
break;
default:
m_DynamicStateServerAddress = sPROD;
break;
}

}

// ************************************************************
// use machine name to decide PROD DEV QA
private static string m_DeploymentType = "";
public static string DeploymentType
{
get
{
if ((string.IsNullOrEmpty(m_DeploymentType)))
{
string sname = Environment.MachineName;
//utils_SysNetInfo.machinename
m_DeploymentType = "PROD";
if ((sname.ToUpper().IndexOf("DEV") > -1))
m_DeploymentType = "DEV";
if ((sname.ToUpper().IndexOf("QA") > -1))
m_DeploymentType = "QA";
}

return m_DeploymentType;

}
}

} // class

}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: @telerik
//Facebook: facebook.com/telerik
//=======================================================

Advertisements

Tags:

One Response to “dynamically select StateServer based on machinename (for PROD,QA,DEV selection)”

  1. slightlybehindthecurve Says:

    turn on local state server on your box:

    1.Type Services.msc (for XP /Windows 2003 and above you need to run this as administrator).
    2.Start ASP.NET State Service.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: