|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
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
IntroductionWeb 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. ScenarioLet’s take a concrete but simple example for demonstration.
There is a service called
As an example, first call to How can this web service be modified to maintain the state? SolutionFor 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 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
[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: 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 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||