Click here to Skip to main content
14,665,188 members
Articles » Web Development » Web Services » General
Posted 24 Sep 2006


19 bookmarked

Stateful Web Services

Rate this:
2.22 (5 votes)
Please Sign up or sign in to vote.
2.22 (5 votes)
10 Oct 2006CPOL
This article describes, by means of a simple example, how to maintain states across multiple operations in a web service.
Stateful Services


Web Services allow several operations to be published at a single end-point.

However, there may be a scenario where more than one operation may be required to be invoked, in a specific order, and an internal state needs to be maintained.


Let’s take a concrete but simple example for demonstration.

There is a service called MathService that publishes two operations Add and Subtract.

Add operation requires that two integers are parameters and returns an integer – result of addition of values to the two parameters.

Subtract requires just one parameter. It returns the difference between the result of last operation - addition or subtraction - (by the same client) and the value of parameter passed.

As an example, first call to Add with values 4 and 5 returns 9. Then, a call to Subtract with a value of 3 would return 6. Next, a call to Subtract with a value of 10 would return -4 and so on.

How can this web service be modified to maintain the state?


For extreme cases, one must look at the Web Service Transactions Specification.

I am providing a quick-heal solution. But the same idea can be used in any web services that require maintaining states – not only by one service but multiple services.

I would modify the Add operation to return two items – the result of addition and a unique token.

I also modify the Subtract operation to take an additional parameter – a token that would be used to internally track the data.

It is required since for every operation invocation, a new instance of the class representing the web service is created. Using the unique token, the data would be cached on the server side.

The implementation of the cache is what remains in question. I am using an inbuilt implementation for the cache for MathService – using the HttpRuntime Cache. Persistent caching may be provided using databases and other dependencies.

public class MathService : WebService
   [return: XmlElement("result", typeof(int)),
        XmlElement("token", typeof(string))]
   public object[] Add(int x, int y)
      int z = x + y;
      string token = Guid.NewGuid().ToString();
      Cache cache = HttpRuntime.Cache;
      cache.Insert(token, z, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30));
      return new object[] { z, token };

   [return: XmlElement("result", typeof(int))]
   public int Subtract(int y, string token)
      Cache cache = HttpRuntime.Cache;
      object obj = cache[token];
      int x = 0;
      if(obj != null)
         x = (int) obj;
      int z = x – y;
      cache[token] = z;
      return z;

The web service is ready to be deployed and tested.

You can download the source code and test client to see how all this works!

The output may appear as shown below:

Stateful Services

You can also visit web-service related blog at Eduzine© - electronic technology magazine of EduJini, the company that I work with.


One can have stateful services without going into the nitty-gritties of Web Service Transactions, or using WSE to modify the Head of the Envelope or rely on cookies that some enterprises disallow.


You may need to update the web reference for the web-service in the client. I used 'File System' for ASP.NET application and the Web-Server was on port 1073. Update the reference for your case.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States

Gaurav lives in Silicon Valley.

His technical strength lies in having deep understanding of not one or two but bunch of enterprise frameworks.

Lately, he is working on an MVC framework implementation for Android put in open domain at

Today, he is an independent guy:

  • He is a programming language independent guy, well almost. He is proficient in C, C++, Java, C#, VB.Net, C++.Net, JavaScript, PHP, Tcl, Python, Ruby
  • He is an OS independent guy, well almost. Has worked and developed at length on HP-UX, Linux (Redhat / Mandrake), Macintosh (9, 10.x), Windows (NT, 2000, 2003, XP, Vista), Solaris (8, 9, 10); and mobile platforms Android, iPhone, Blackberry
  • He is a target independent guy, well almost. Has worked on thick clients (mainly desktops) as well as thin clients (mainly alternative platforms - Symbian, PalmOS, WinCE, WinXP Embedded, Linux Embedded, Android, iPhone, Blackberry)

Today, his thrust areas are Service Oriented Architecture (implementation expert in Java, .Net and PHP; integration with Mainframe and other legacy environments), Mobile Technologies and RFID.

He holds a Bachelor's Degree (B. Tech.) from IIT Kanpur His major was in EE with specialization in DSP (SSP).

His hobby is listening music, reading books (no, he can't read novels), photography and travelling.

Should you wish to talk to him, you can drop him a mail at gaurav[dot]vaish[at]gmail[dot]com. He generally responds within 24-48 hrs unless there is a compelling reason.

And yeah... here's his personal website:

Smile | :)

Comments and Discussions

GeneralCache object Pin
Eric Lacroix13-Oct-06 9:14
MemberEric Lacroix13-Oct-06 9:14 
GeneralRe: Cache object Pin
mastergaurav14-Oct-06 8:43
Membermastergaurav14-Oct-06 8:43 
QuestionHttpSession vs. Cache Pin
Ashish Jindal24-Sep-06 2:17
MemberAshish Jindal24-Sep-06 2:17 
AnswerRe: HttpSession vs. Cache Pin
mastergaurav24-Sep-06 2:25
Membermastergaurav24-Sep-06 2:25 
GeneralRe: HttpSession vs. Cache Pin
mrchief_200026-Sep-06 8:41
Membermrchief_200026-Sep-06 8:41 
GeneralRe: HttpSession vs. Cache Pin
mastergaurav26-Sep-06 15:21
Membermastergaurav26-Sep-06 15:21 
GeneralRe: HttpSession vs. Cache Pin
mrchief_200027-Sep-06 6:14
Membermrchief_200027-Sep-06 6:14 
GeneralRe: HttpSession vs. Cache Pin
mastergaurav27-Sep-06 6:15
Membermastergaurav27-Sep-06 6:15 
GeneralRe: HttpSession vs. Cache Pin
mrchief_200027-Sep-06 9:22
Membermrchief_200027-Sep-06 9:22 
GeneralRe: HttpSession vs. Cache Pin
mastergaurav28-Sep-06 2:24
Membermastergaurav28-Sep-06 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.