A
Anurag K
i have created a singleton class in a class file in my asp.net web
application.
this class is a singleton class.
the code is:
public class singletonclass
{
private static singletonclass sc = null;
private singletonclass(string path)
{
using (FileStream fs = File.OpenWrite(path))
{
Byte[] info =
new UTF8Encoding(true).GetBytes("new instance created");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
public static singletonclass getsingletonclassinstance(string path)
{
if (sc == null)
{
sc = new singletonclass(path);
}
return sc;
}
}
there is a single web form in my application. the code in the page
load of my webform is:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
singletonclass.getsingletonclassinstance(Server.MapPath(".")+@"\write.txt");
}
the issue here is even if i run different browser instances i am
getting only a single entry in my text file. my understanding is
different web requests do not share memory and so there should have
been a single object for each request. if within a web request there
are multiple calls to the singletonclass.getsingletonclassinstance()
method, reference to the same object should be returned.
if anybody got what i am trying to convey then please reply.
application.
this class is a singleton class.
the code is:
public class singletonclass
{
private static singletonclass sc = null;
private singletonclass(string path)
{
using (FileStream fs = File.OpenWrite(path))
{
Byte[] info =
new UTF8Encoding(true).GetBytes("new instance created");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
public static singletonclass getsingletonclassinstance(string path)
{
if (sc == null)
{
sc = new singletonclass(path);
}
return sc;
}
}
there is a single web form in my application. the code in the page
load of my webform is:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
singletonclass.getsingletonclassinstance(Server.MapPath(".")+@"\write.txt");
}
the issue here is even if i run different browser instances i am
getting only a single entry in my text file. my understanding is
different web requests do not share memory and so there should have
been a single object for each request. if within a web request there
are multiple calls to the singletonclass.getsingletonclassinstance()
method, reference to the same object should be returned.
if anybody got what i am trying to convey then please reply.