Manage Session State In Web Service - Part Three

Before proceeding, you should follow my ASP.NET Web Service series from the following links,

In previous articles, we learned about how we can consume a Web Service where we use proxy class. In this part of series, we’ll learn session state in a Web Service.

To use ASP.NET Session object in a Web Service, there are two things that we need to do. The Web Service class should inherit from System.Web.Service.WebServices class. If we remove the parent class, we can’t use Session object.


Second thing is that we need to set EnableSession property equal to true of the WebMethod attribute.

Now, I’ll work on a previous project of the Web Service. Flip to Visual Studio. I want to show all the results a user has performed in a grid view, using session object, so let’s go for it. 

Here, in MyWebService.asmx file, I’ve two methods AddName() and Sub(). I’m going to perform all the operations in AddName method. In previous articles, I worked on Sub() method.

Hence, within AddName() method, first create a variable of type list of string and call it a result. I’m using Session object within this method. Let’s use a Session variable and call it RESULT. If there is Sessio the RESULT is null, in which I’m going to create a list of strings. I want to show -

First Name (Amit) + Second Name (Mishra) = Amit Mishra

Let’s understand the code that we are going to use.

In order to achieve that, use the code given below -
  1. string strResult=FirstName.ToString() + " + " +SecondName.ToString() +   
  2.                 " = " + (FirstName+SecondName).ToString();  
Now, we need to store result object within the Session variable.
  1. Session["RESULT"]=result;  
  1. public string AddName(string FirstName, string SecondName) {  
  2.     List < string > result;  
  4.     if (Session["RESULT"] == null) {  
  5.         result = new List < string > ();  
  6.     } else {  
  7.         result = (List < string > ) Session["RESULT"];  
  8.     }  
  10.     string strResult = FirstName.ToString() + " + " + SecondName.ToString() +  
  11.         " = " + (FirstName + SecondName).ToString();  
  13.     result.Add(strResult);  
  15.     Session["RESULT"] = result;  
  17.     return FirstName + SecondName;  
  18. }  
Now, I need another method which returns all the recent results that the user has performed. If Session of the result is null, then the user has not performed any action. On the other hand, if Session of result is not null, then the user can perform some actions and then we store it in the Session variable.
  1. [WebMethod(EnableSession = true)]  
  2. public List < string > GetResult() {  
  3.     if (Session["RESULT"] == null) {  
  4.         List < string > result = new List < string > ();  
  6.         result.Add("Hey!!!!! you are unable to perform any action");  
  8.         return result;  
  9.     } else {  
  10.         return (List < string > ) Session["RESULT"];  
  11.     }  
  12. }  

In previous articles, I performed operations with Sub() method, so you can make some changes over in your design page MyWebForm.aspx, as shown below:  

Hence, I think all is set. Let’s check our solution. Right click MyWebService.asmx and view it on the Browser.

Look at the following screenshot. There are now three methods. GetResults() method has benn added to invoke the results, which we’ll perform under AddName() method. Hence, first click AddName() method.

Test your parameter value and invoke it by clicking Invoke button.

We got our result in the string format in the Browser.

By going back to the AddName method, test one more parameter value and click to invoke.

You can see that we got our result in the string format.

Now to see result and test the GetResult() method, click it.

Hence, this method doesn’t accept any parameters, so just click to invoke button to see what’s going to happen.

See the following screenshot, the operations  that performed in the AddName method are displayed in this method.

Now let’s drag and drop a GridView control in MyWebForm.aspx page.

In the following screenshot, I need to invoke GetResult() method.

The actual problem is that with this we go ahead to Reference.cs file, which contains proxy class. If you notice, in this class there are only two methods AddName() method and Sub() method. It does’t have GetResult() method. That is because we developed this proxy class awhile back. Hence, there are some changes that occur in the Web Service, so we need to update this proxy class.

For this, you need to right click on the Service and select Update Service reference, as shown in following screenshot:

After references are updated, you can see GetResult() method, which is automatically generated.

Go back to the code behind the file. You’ll find GetResult() method.

cl.GetResult(); this method is going to return array of strings. Finally invoke DataBind().

Here, call AddName() method.

Now all is set, right click MyWebForm.aspx and select View in the Browser option.

Go ahead and enter two values, click the output button to see the actual output. We already performed task for one string. Now, it says you’re unable to perform any action.

Let’s try and do it a second time. After this, you’ll find it still says you’re unable to perform any action. We’ve tested the Web Service method and it is working properly. GetResult() method returns the string that we’ve performed, but when we called that Web Service from the client Web Application, it’s not working as expected.

This means that our Demo Web Service is not sending the same Session ID to the Web Service and this is the reason why it’s not working. To make it work, first of all, I need to make some changes in Web Config file, the section under Web Config file that will automatically generate after adding Service reference, where you find allowCookies attribute, that would be set to false by default. I need to set this allowCookies attribute to be true.

Now, run your Web Form once again. I hope we’ll find our result as expected. Look in the following screenshot, Web Service method is working properly and Web Service that I called from client Web Application is working properly as well.

You can check it by entering one or more strings that we got successfully.

Thanks for reading this article. Stay tuned with us for more on the Web Service.

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles