Practical Approach To ASP.NET Web Services - Part Three - Session State In A Web Service

This article is the third part of the "Practical Approach To ASP.NET Web Services" series. To read the first two parts, you can go through the following links.

To use ASP.NET Session object in a Web Service, the WebService class must inherit from System.Web.Services. WebService class and the Enable Session property of Web Method attribute must be set to true. Let’s see how can we do that . So, let’s flip to VS.

Now, in our Web Service (Calculator Web Service)

web service

We already have a class that is inheriting from System.Web.Services.WebService. Now, we will set EnableSession property to True.

web service

We want to display the recent calculations which the user has performed. So, we will be using Session object for that.

web service

So, this is a straight forward code. We are using a list to display list of recent calculations. Now, we are using a Session object and key. Then, we are looping those. If it is null, the user doesn’t perform any calculations and it will display the list. But, if the user has performed some sort of calculations, we will be storing that in a session and displaying those. Then, we are concatenating that calculation with the first number and the second number respectively.

Now, let’s test our Web Service.

web service

As you can see from above screenshot, we have got two methods - Add and getCalcualtions. We will click on Add and perform some calculations.

web service

Perform 3-4 operations. Then, get back and test Getcalculations method and invoke.

web service

As you can see from the above output, I have performed two operations and they are showing me the numbers and output. This means, we have successfully saved our session in our object.

Now, we will show this in our webform in a Grid.

Add a Gridview Control in our aspx page and click on Service and update the proxy class.

web service

We have added getCalculations method and this will update our proxy class. Now, go back to the code behind and let's bind our GridView.

web service

Now, let’s test our application and perform some operations.

web service

When you reload the page and re run your application in GridView,  it says that you haven’t performed any calculations. The client application is not storing the session.

The possible reason for this is that the client application is sending the same Session ID to the Web Service. This is why it not storing any session. 

Now, in web.config file, locate binding tag. In that, set "allowCookies= true".

web service

Now, just run the application and perform some calculations.

web service

You can see that the Grid is showing the recent calculations which are performed, that means our client application is now sending the same Session ID to our Web Service .

If you want to change the header of the GridView, use the following code.

web service

Now, let's check the output.

web service

Up Next
    Ebook Download
    View all
    Learn
    View all