5,442,164 members and growing! (17,146 online)
Email Password   helpLost your password?
Web Development » Web Services » General     Intermediate

Stateful Web Services

By mastergaurav

Article describes, by means of a simple example, how to maintain states across multiple operations in a web service
C# 1.0, C# 2.0, C#, Windows, .NET, Visual Studio, Dev

Posted: 24 Sep 2006
Updated: 10 Oct 2006
Views: 19,538
Bookmarked: 12 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.55 Rating: 2.22 out of 5
2 votes, 40.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 20.0%
4
2 votes, 40.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

mastergaurav


Gaurav lives in Bangalore, the Silicon Valley of India.
He is the Chief Technology Officer at Edujini Labs Pvt Ltd - a company that pioneers in Technology Trainings, Education using Technology and Technical Consultancy.
www.edujini-labs.com

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

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

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.

His domain expertise include e-Learning, Anti-Piracy and Telecommunications.

Before co-founding Edujini™, he has worked in the capacity of Head, R&D at TeN, India and as Member, Technical Staff in R&D Team at Adobe Systems, India.

He holds a Bachelor's Degree (B. Tech.) from IIT Kanpur www.iitk.ac.in. 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:
http://www.mastergaurav.com

Smile
Occupation: Web Developer
Location: India India

Other popular Web Services articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralCache objectmemberEric Lacroix10:14 13 Oct '06  
GeneralRe: Cache objectmembermastergaurav9:43 14 Oct '06  
QuestionHttpSession vs. CachememberAshish Jindal3:17 24 Sep '06  
AnswerRe: HttpSession vs. Cachemembermastergaurav3:25 24 Sep '06  
GeneralRe: HttpSession vs. CachememberHrusikesh9:41 26 Sep '06  
GeneralRe: HttpSession vs. Cachemembermastergaurav16:21 26 Sep '06  
GeneralRe: HttpSession vs. CachememberHrusikesh7:14 27 Sep '06  
GeneralRe: HttpSession vs. Cachemembermastergaurav7:15 27 Sep '06  
GeneralRe: HttpSession vs. CachememberHrusikesh10:22 27 Sep '06  
GeneralRe: HttpSession vs. Cachemembermastergaurav3:24 28 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Oct 2006
Editor:
Copyright 2006 by mastergaurav
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project