«^»
10.5. MTotal: a .NET Web Service to store the sum of a series of values

We now suppose that, instead, the .NET Framework is being used to provide the Web Service [12, 24]. Visual Studio.NET has a wizard that makes it easy to create a Web Service. It generates most of the code for you in a class that is derived from a class called WebService (which is declared in the System.Web.Services namespace). All you need to do is to include in this class the code of the methods that you wish to make available. You will have to declare each of these methods with a WebMethod attribute.

You will need to do two things to get a value retained from one call of a method to the next:

Here is the full code (in C#) for the web method inc:

0298: [WebMethod(EnableSession=true)]
0299: public double inc(double pIncrement)
0300: {
0301:    double tNewTotal;
0302:    object tObject = Session["MTotal"];
0303:    if ( tObject == null ) 
0304:       tNewTotal = pIncrement;
0305:    else
0306:       tNewTotal = (double)tObject + pIncrement;
0307:    Session["MTotal"] = tNewTotal;
0308:    return tNewTotal;
0309: }

The HTTP response from the first call of a method of this Web Service will include a header line like:

Set-Cookie: ASP.NET_SessionId=50jlsti3jqa5gg45321sbkia; path=/
A client needs to ensure that it captures this value and uses it in the header of the HTTP requests of subsequent calls to the Web Service, e.g.:
Cookie: ASP.NET_SessionId=50jlsti3jqa5gg45321sbkia