Home > Ask the SOA Experts > Questions & Answers > Stateful Web services in .NET
Ask The SOA Expert: Questions & Answers
EMAIL THIS

Stateful Web services in .NET

Michèle Leroux Bustamante EXPERT RESPONSE FROM: Michèle Leroux Bustamante

Pose a Question
Other SOA Categories
Meet all SOA Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 10 February 2004
I have the following:

// ss is the object of the Web service class to which this application refers to.

1) ss.getcatalog(); // returns a value there in that Web service class
2) ss.change(132); // sets the same value
3) ss.getcatalog();

My question is that I cannot update the value in the above way at the Web service class. Does that mean the Web service is stateless? If so, how can I write stateful Web services in .NET?


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Microsoft .NET Web services
Microsoft preps .NET 4.0 - framework improves on REST, MVC, JQuery support
How do I balance throughput requirements and interoperability?
APM software traces transactions across tiers, technologies
How you can learn M Grammar for Oslo modeling
Legacy modernization opens Windows for publisher
Former .NET Web developers ride Ruby and Rails application framework
Microsoft Oslo at PDC: Dial 'M' for modeling language
Yahoo proxy fight looms
New Microsoft site for architects
LAMP coders go hybrid route
Microsoft .NET Web services Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Common Language Infrastructure  (SearchSOA.com)
Visual J#  (SearchSOA.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


Web services invoked over HTTP protocol are stateless by design. When the request reaches the Web server, the ASP.NET runtime manages the request by instantiating an instance of your Web service type, deserializing the SOAP envelope XML stream, and invoking the requested method passing parameters if applicable. A new instance of the Web service type is created for each HTTP request targeting the service, so any information stored in data fields contained within the type are recreated for each round trip. If you want to persist state you must specifically enable state for the method as shown here:

[ WebMethod(EnableSession=true) ]
public object getcatalog()
{ 
return Session["catalog"];
}

[ WebMethod(EnableSession=true) ]
public void change(object val)
{ 
Session["catalog"] = val;
}
This will cause the ASP.NET runtime to create an HttpSessionState object the first time a method that has session state enabled is called. Each subsequent call to a method that has session enabled will share the same session state object, so long as the client passes a session cookie with each subsequent request. If the client does not pass a session cookie ASP.NET will create a new session for each call.

The client invoking the service must be session aware, something we take for granted when we navigate Web sites with a browser. The client proxy created for a .NET Web service includes a CookieContainer property, and .NET provides us with CookieContainer type that we can instantiate and assign the proxy as follows:

// create cookie container once for the duration of the session
System.Net.CookieContainer cookies = new System.Net.CookieContainer();

// each time invoking the service, assign the same cookie container before invoking methods
localhost.SessionService1 svc = new localhost.SessionService1();
svc.CookieContainer = cookies;
MessageBox.Show(svc.UpdateHitCounter());


When the proxy invokes the service, it serializes the method call into a SOAP envelope, and builds an HTTP request with appropriate headers, and posts to the endpoint URI of the Web service. The HTTP headers will include the session cookie if one is provided here, and that's how ASP.NET can find the right session state object to make it available to your Web method.

See the following code sample for a complete example:
http://www.dotnetdashboard.com/downloads/sessionservice.zip




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



SOA Governance White Papers - BPM, EDA, IT Governance
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts