Blogger Widgets
  • Sharing Photos using SignalR
  • TFS Extenstion - allows copy work items between projects
  • Displaying jquery progressbar with ajax call on a modal dialog
  • Managing windows services of a server via a website
  • Exploring technologies available to date. TechCipher is one place that any professional would like to visit, either to get an overview or to have better understanding.

Search This Blog

Tuesday 6 December 2011

Using session locking with EnableSession = true

Locking session object for each webservice is simple as just adding [WebMethod(EnableSession = true)] for each of the webmethods that require locking. At the same time if you also want to lock an object that is/will be part of the session object then Declare a static object
public static object lock_updatesharevalue = new object();
Now use this object in the webmethod to enable locking for an object in session
[WebMethod(EnableSession = true)]
public int UpdateShareValue(string Shareidentifier,float Sharevalue)
{
 lock(lock_updatesharevalue)
 {
    ///Update database with new share value
 }
 Session[Shareidentifier + "CurrentShareValue"] = Sharevalue;
}
So firstly session object is locked and then you are forcing a lock on a static object inside the webmethod that solves the issue of value failing to update when there are thousands of requests comming. Also point to note at this point is this method makes all the calls serialized and hence might reduce the performance and some of the calls might be waiting longer than expected so remember to increase the timeout in web.config
httpRuntime executionTimeout="10000" maxRequestLength="102400"
Refer to this article about Session State Overview

The real danger is not that computers will begin to think like men, but that men will begin to think like computers. ~Sydney J. Harris

0 comments:

Post a Comment

Copyright © 2013 Template Doctor . Designed by Malith Madushanka - Cool Blogger Tutorials | Code by CBT | Images by by HQ Wallpapers