|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn an ASP.NET application, it is very easy to pass session data from one web page to another. For example, the following code stores three pieces of data of an internet session using the .NET web session provided by the .NET framework: ...
Session("UserName") = sUserName
Session("PageName") = sPageName
Session("StatusCode") = nStatusCode
If later the user navigated to a different page, there is no need to pass the session data via the query string because they are already available on the server. The following code will retrieve the session data stored previously: ...
sUserName = CType(Session("UserName"), String)
sPreviousPageName = CType(Session("PageName"), String)
nPreviousStatusCode = CType(Session("StatusCode"), Integer)
By default, the session data is stored in memory of the server process, which makes it very efficient. If you want load balancing your applications using several servers or you want to be able to reboot the server machine quickly without big impact on current users, you can modify the web.config file of your application so that the session data will be stored in a SQL server database or in the memory of a Microsoft session state server instance. It seems that we always want to do more than the current tool allows. For example, I would like to share session data among different applications. Some of my applications are running on machines without the .NET framework and some of them are not even web applications (windows services, console apps, and desktop GUI apps, etc.). Note that we are only dealing with applications we trust so there is no security issue here. The .NET framework built-in session is not suitable for me because
In this article, I will introduce a simple, reliable, and efficient session tool that does not have these shortcomings. I could have any number of applications running on one or more servers all sharing the same session data. I hope this tool will be useful to you, too. Overview of my session toolThe session data is managed by a .NET library Session.dll which contains the All session data is stored in the memory of the Different applications can store and retrieve data from this I have included with this article some sample code to access the The SessionManager classThis is a class implemented in Session.dll written in VB.NET (actually it can be converted easily to any other .NET language in case VB is not your favorite choice). It implements theSetSessionData and GetSessionData methods
Public Function SetSessionData( _
ByVal sID As String, ByVal sKey As String, _
ByVal oValue As Object) As Boolean
Public Function GetSessionData( ByVal sID As String, _
ByVal sKey As String) As Object
The C# signatures are public bool SetSessionData(String sID, _
String sKey, Object oValue);
Public Object GetSessionData(String sID, String sKey);
The ...
SetSessionData(sMySessionID, "UserName", sUserName)
SetSessionData(sMySessionID, "PageName", sPageName)
SetSessionData(sMySessionID, "StatusCode", nStatusCode)
...
sUserName = CType(GetSessionData(sMySessionID, _
"UserName"), String)
sPreviousPageName = CType(GetSessionData(sMySessionID, _
"PageName"), String)
nPreviousStatusCode = CType(GetSessionData(sMySessionID, _
"StatusCode"), Integer)
Here is the same code in C# ...
SetSessionData(sMySessionID, "UserName", sUserName);
SetSessionData(sMySessionID, "PageName", sPageName);
SetSessionData(sMySessionID, "StatusCode", nStatusCode);
...
sUserName = (String)GetSessionData(sMySessionID, "UserName");
sPreviousPageName = (String)GetSessionData(sMySessionID, "PageName");
nPreviousStatusCode = (int)GetSessionData(sMySessionID, "StatusCode");
Internally, the Each session has a time stamp associated with it. When the The
All .NET applications, which do not have to be internet related, can use To set up a remoting client, you need to create a configuration file containing the following text: <configuration>
<system.runtime.remoting>
<application>
<client>
<!--SessionManager-->
<wellknown type="SessionLib.SessionManager,Session"
url=" http://localhost:80/SessionService/SessionManager.soap" />
</client>
<channels>
<channel ref="http" />
</channels>
</application>
</configuration>
The RemotingConfiguration.Configure(sConfigurationFilePath)
where ...
Dim mgr As SessionManager = New SessionManager( )
mgr.SetSessionData(sSessionID, "TestData", "This is a test")
sData = CType(mgr.GetSessionData(sSessionID, "TestData"), String)
Here is the C# version ... SessionManager mgr = new
SessionManager(); mgr.SetSessionData(sSessionID, "TestData", "This is a
test"); sData = (String)mgr.GetSessionData(sSessionID, "TestData");
Note that once you configured the remoting client, session data will be saved to and loaded from the remoting server. Using the remoting server is not as efficient as keeping session data in memory of the same process, but it allows multiple .NET applications to share the same data. Also, using a .NET remoting server limits your session objects to serializable objects. We will talk about configuring the remoting server later. The disadvantage of using a .NET remoting server (comparing to using the web service) is that the clients can only be .NET applications. Since multiple applications can access the same data via ...
Dim mgr As
SessionManager = New SessionManager( )
Here is the C# version ...SessionManager mgr ;= new
SessionManager();
The web service and the remoting serverTheSessionService component provides the main features of the SessionManager class through a .NET web service. With this web service, you can have applications running on non .NET machines all sharing the same set of session data. You can even have applications accessing session data across firewalls.
To install the web service, you need to do the following
That's it. Now you can call the web service from client programs. Another way to install SessionService is run the included Install.vbs script. The SessionService will call the For non .NET client programs, calling the SessionService is made easier by using XYSoapClient.dll. This is a regular com dll that can be used in C++, VB, and VB script to access almost any web service. For example, here is a script that set and get session data: Dim obj
Set obj = CreateObject("XYSoapClient.1")
obj.InitService "http://localhost:80/SessionService/SessionService.asmx?wsdl"
If obj.InvokeMethod("SetSessionString", "MyID", "MyKey", "Test") Then
wscript.echo "Set session data ... Click OK to verify the data"
wscript.echo obj.InvokeMethod("GetSessionString", "MyID", "MyKey")
End If
Set obj = Nothing
The XYSoapClient.dll is described in detail in another article. You can have multiple instances of the SessionService running on different machines or on the same machine. A limitation of the SessionService is that you can only use string data with non .NET clients. This is not a big deal because you can always represent other data types using xml, it is not possible to use .NET objects in a non .NET program anyway. For .NET client programs. You can add web reference to access the SessionService. Alternatively, you can use the Dim proxy As SessionProxy
= New SessionProxy("http://localhost/SessionService/SessionService.asmx")
proxy.SetSessionData("MyID", "MyKey", "Test")
Dim sTestData As String = CStr(proxy.GetSessionData("MyID", "MyKey"))
And here is the C# version SessionProxy proxy = new SessionProxy(
"http://localhost/SessionService/SessionService.asmx");
proxy.SetSessionData("MyID", "MyKey", "Test");
String sTestData = (String)proxy.GetSessionData("MyID", "MyKey");
If you look carefully, you will find the following text in the web.config file of <configuration>
<system.runtime.remoting>
<application>
<service>
<!--SessionManager-->
<wellknown mode="SingleCall"
type="SessionLib.SessionManager, Session"
objectUri="SessionManager.soap" />
</service>
</application>
</system.runtime.remoting>
</configuration>
The above text exposes the If you unzip the downloaded file to a folder and run the Install.vbs file, it will copy all the components to a folder of your choice and set up the web service and sample applications for you (creates virtual directories, etc.). Error logging in the session serviceThe previous version of the Here is the section in the web.config file that determines how tracing is done. <appSettings>
<add key="TraceFilePrefix" value="Log\SessionService" />
<add key="TraceLevel" value="40">
<add key="TraceCleanup" value="7">
</appSettings>
The server will generate a new trace file each day. The Test programs and the SessionBrowserThe program SessionRemotingTest.exe and SessionConsoleTest.exe demonstrate how to access the The SessionBrowser program can be used to view data stored in the
The Load Sessions button causes all session data from the You can selete the session you want to view by using the Session ID List drop down. Then you can use the Item Key List drop down to select a session object belonging to the selected session. It is also possible to change session data from the To create a new session object in the The Search Text button is used to find session data that contains certain strings. If you enter a string into the edit box to the left of the button and click the button, the session object that contains that string will be displayed in the Session Data edit box. If you click this button again, it will display the next session object that satisfies the search condition, etc. There are other things you can do with the Typically, the Recent Updates
| ||||||||||||||||||||||