|
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.
I am working on the C# compiler at Microsoft since 2007.
Microsoft Certified Professional since 2006
Interests: C#, ASP.NET, LINQ