Click here to Skip to main content
Email Password   helpLost your password?

Stateful Services

Introduction

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

However, it may be a scenario that more than one operation may be required to be invoked, in a specific order, and an internal state need to be maintained.

Scenario

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 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 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?

Solution

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.

[WebService(Namespace="http://www.edujinionline.com/ns/samples/webservices/")]
public class MathService : WebService
{
   [WebMethod]
   [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 };
   }

   [WebMethod]
   [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!

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.

Summary

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

Note:

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.

 

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralCache object
Eric Lacroix
10:14 13 Oct '06  
Some notes:

On windows 2003 server: Cache object are not working as you may expect with several worker process running, cache object are related to only one worker process and are not shared with all other worker proces running.

So a client may use the a webfunction on one worker process and try to acess the other web function and get redirected from the server to another workerprocess and bam you will not be able to get the cached object on the server.

Ther is no elegant way to resolve this issue under a WebService architecture.


(define Email (lambda ()
elacroix@devmesh.com))
Im not a church numeral im a free variable

GeneralRe: Cache object
mastergaurav
9:43 14 Oct '06  
Hi Eric,

Thanks for your notes.

My other article on working using WS-Tx would be coming up soon (soon means less than a fortnight)




--

Cheers,
Gaurav Vaish
http://www.mastergaurav.com
http://articles.edujinionline.com
---------------------------

QuestionHttpSession vs. Cache
Ashish Jindal
3:17 24 Sep '06  
You suggest using Cache. ASP.Net also allows working with HttpSession by enabling Session for a Web Service.

What's the reason you did not use session?


-Ashish
AnswerRe: HttpSession vs. Cache
mastergaurav
3:25 24 Sep '06  
Dear Ashish,

One can use HttpSession as well. However, that would be implemented thru Cookies. And somehow, I try to minimize the use of Cookies.

Note that this design can, because of its nature, can be used by direct webservice-proxies as well that may not work well with cookies.

Strictly speaking, this is a simple tweak if you do not wish to work with WSE. Similarly, for Java, one may need to work with SAAJ (Axis2 et al) to work with the better WS-Tx (Transactions) framework.

--
Cheers,
Gaurav Vaish
http://www.mastergaurav.com
http://articles.edujinionline.com
---------------------------

GeneralRe: HttpSession vs. Cache
Hrusikesh
9:41 26 Sep '06  
mastergaurav wrote:
One can use HttpSession as well. However, that would be implemented thru Cookies

Can you point me an article that says so? Never heard of itConfused


GeneralRe: HttpSession vs. Cache
mastergaurav
16:21 26 Sep '06  
HttpSession would be implemented through cookies unless you choose to do it by means of URL-rewriting.

Ok yes, I agree that you can go cookieless mode and the URL will be updated accordingly.

btw, I would be surprised to learn a remark of 'Never heard of it' on 'implemented thru Cookies' because that's the default way it is done.

Session are, unless specifically mentioned, always maintained thru Cookies.

How else do you think it is done? Sniff


--

Cheers,
Gaurav Vaish
http://www.mastergaurav.com
http://articles.edujinionline.com
---------------------------

GeneralRe: HttpSession vs. Cache
Hrusikesh
7:14 27 Sep '06  
mastergaurav wrote:
Session are, unless specifically mentioned, always maintained thru Cookies.

How else do you think it is done?

Only the Session ID is stored in cookie (or URL in cookieless mode). Session State is stored, based on your settings

1. On the same server
2. On a central server (in case of server farm/garden)
or SQL Server.

For details, see http://support.microsoft.com/kb/307598/[^]

GeneralRe: HttpSession vs. Cache
mastergaurav
7:15 27 Sep '06  
Well... of course. I mean the same.
I said that Session is managed thru cookies not that session data is kept in cookies.

No client will allow heavy data in cookies. And it'd be utter nonsense to keep all the 'safe and secure' data on the client.

--

Cheers,
Gaurav Vaish
http://www.mastergaurav.com
http://articles.edujinionline.com
---------------------------

GeneralRe: HttpSession vs. Cache
Hrusikesh
10:22 27 Sep '06  
mastergaurav wrote:
Well... of course. I mean the same.

Better be careful about what you write and how you write.

I guess it'll take few more years for CP to hook up to your thought process and choose the words accordingly.Smile
GeneralRe: HttpSession vs. Cache
mastergaurav
3:24 28 Sep '06  
Smile
Thanks for your words... will take care in future. I think I got a li'll too excited on my first article... Poke tongue

--

Cheers,
Gaurav Vaish
http://www.mastergaurav.com
http://articles.edujinionline.com
---------------------------


Last Updated 10 Oct 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010