Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » XML » Web Services » Downloads
 
Add your own
alternative version

Building web services with persistent state

, 13 Mar 2006 CPOL
Describes a way of creating a web service that persists its state between sequent calls.
persistentwebservice_src.zip
WebServicePersistent
App_Code
App_Data
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string MethodCall_ver2(int id)
    {
        //We have a setting in web.config which tells us where should we store the files. 
        // The setting is called TemporaryDirectory

        //Check to see if the file if the specified directory exists. If it does, this is not the first request and we retrieve it
        if (File.Exists(string.Format("{0}{1}.dat", System.Configuration.ConfigurationManager.AppSettings["TemporaryDirectory"],   id )))
        {
            //Get a FileStream to point to that file
            FileStream sr = new FileStream(string.Format("{0}{1}.dat", System.Configuration.ConfigurationManager.AppSettings["TemporaryDirectory"], id), FileMode.Open);
            //Create an object used in serializing/deserializing objects
            BinaryFormatter bf = new BinaryFormatter();
            //deserialize the object
            d = (UserDefinedClass)bf.Deserialize(sr);
            sr.Close();
        }
        else
        {
            //if the file does not exist then we create a new object
            d = new UserDefinedClass();
        }

        //Call methods on the object

        //We serialize the object back to the file system
        FileStream sw = new FileStream(string.Format("{0}{1}.dat", System.Configuration.ConfigurationManager.AppSettings["TemporaryDirectory"], id), FileMode.OpenOrCreate);

        BinaryFormatter abf = new BinaryFormatter();
        abf.Serialize(sw, d);
        sw.Close();

        //The service returns
        return string.Format("{0}", d.something.ToString());
    }


    [WebMethod]
    public string MethodCall_ver1(int token)
    {
        UserDefinedClass d = null;

        //If the token not found in the Application object, we retrieve it
        if (Application[token.ToString()] != null)
        {
            d = (UserDefinedClass)Application[token.ToString()];
        }
        else
        {
            //if the token was not found then we create a new object
            d = new UserDefinedClass();
        }

        //Call methods on the object

        //We save the object into the Application object
        Application[token.ToString()] = d;

        //The service returns
        return string.Format("{0}", d.something.ToString());
    }

}
[Serializable]
public class UserDefinedClass
{
    public UserDefinedClass()
    {
        something = 0;
    }

    public int something;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Alexandru Ghiondea
Software Developer Microsoft
United States United States
I am working on the C# compiler at Microsoft since 2007.
 
Microsoft Certified Professional since 2006
 
Interests: C#, ASP.NET, LINQ

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150128.1 | Last Updated 13 Mar 2006
Article Copyright 2006 by Alexandru Ghiondea
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid