C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Implementing Session in WebService


Posted Date: 28 May 2008    Resource Type: Articles    Category: WCF/Webservices

Posted By: shakti singh tanwar       Member Level: Diamond
Rating:     Points: 30



As we all know that all web applications are stateless and webServices are no exceptions.There are various techniques of state management in web applications viz
Hidden Fields
ViewState
Session
Application
Cache
HttpCookies
HttpContext
Since web services don’t have any user interface we can’t use Hidden fields and ViewState for maintaining state. Here in we will discuss how to maintain session in web services.
In my previous post regarding web services attributes http://www.dotnetspider.com/resources/16088-Web-Services-Attributes.aspx. I have specified what all attributes a web service can have and what is the use of each attribute. Let’s talk in depth about EnableSession attribute:-

The EnableSession property is a Boolean property and allows us to enable sessions (this requires the use of the proxy object, which will be discussed in another article). Without the proxy object, this web property cannot be implemented as the property is discarded as soon as the value is assigned to it. Without a proxy object, only one request is allowed per session, and assigning a value to the property itself is a request and therefore the session is ended just after.
Now just have a look at the below code
[WebMethod]
public void SetSessionVariable(string str)
{
Session["id"] = str;
}

[WebMethod]
public string GetSessionVariable()
{
if ( Session["id"] != null )
return Session["id"].ToString();
else
return "Nothing in Session";
}
When you run this code for the first time It will give Object reference error in SetSessionVariable function since Session is not enables and thus Session object will be null.
To get around this issue you just need to tweak the WebMethod attribute a bit.Append the WebMethod attributes to the functions to enable sessions.


[WebMethod(EnableSession=true)]
public void SetSessionVariable(string str)
{
Session["id"] = str;
}

[WebMethod(EnableSession=true)]
public string GetSessionVariable()
{
if ( Session["id"] != null )
return Session["id"].ToString();
else
return "Nothing in Session";
}
Now try to run this sample by writing the following code
localhost.Service obj = new localhost.Service();
obj.SetSessionVariable(“Some Value”);
MessageBox.Show(obj. GetSessionVariable().ToString());
What Happened this time?Did we get the value this time round from session?What have we done wrong……………
Well the web service code is ok but since web Services are stateless where will they store the session information. System.Net.CookieContainer class comes to the rescue.Since web services are a complete framework you will find a lot of things when you have just created them by writing a simple function one og the things is a CookieContainer property.Yeah you have to set this property to get the session thing working for you.
So here goes the complete code

localhost.Service obj = new localhost.Service();
System.Net.CookieContainer container= new System.Net.CookieContainer();
obj.CookieContainer=container;
obj.SetSessionVariable(“Some Value”);//Sets value in session
MessageBox.Show(obj. GetSessionVariable().ToString());//Retrieves value from Session




Responses

Author: Rahul Sharma    28 May 2008Member Level: Silver   Points : 2
Excellent article! Thanks a lot, I tried this and my application is able to mantain sessions on webservice. Please do let me know about the web service security with examples. It would be great help for me as this.



Author: vijayavasavi narapuram    29 May 2008Member Level: Silver   Points : 2
Hi,
Very nice article..Thanks for posting this article in dotnet spider...


Author: Kumar Velu    30 May 2008Member Level: Diamond   Points : 2
Excellent article!


Author: Sarfraz Ahmad    30 May 2008Member Level: Silver   Points : 2
Nice article !


Author: ramesh.v    30 May 2008Member Level: Bronze   Points : 2
hi this is very nice article


Author: suhati gupta    30 May 2008Member Level: Silver   Points : 2
very important and good article.Thanks


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: What is new in Microsoft Dynamics CRM 4.0?
Previous Resource: Introduction to SOAP protocol
Return to Discussion Resource Index
Post New Resource
Category: WCF/Webservices


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

internet fax

Contact Us    Privacy Policy    Terms Of Use