473,387 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

singleton classes in web application

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.Ma pPath(".")+@"\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.
Nov 18 '05 #1
2 2141
There is only one instance of the static member sc in your class. The
problem you may run into is that it could be initialized by more than
one thread (it is not thread safe as written).

Good information in this article:
Implementing Singleton in C#
http://msdn.microsoft.com/library/de...onInCsharp.asp

Pay careful attention to the volatile keyword when looking at the
source of the article - it is needed in the member declaration.

What I am not understanding is your description of the behavior.
Multiple web requests will all see the same reference for sc once it
has been safely initialized. The private constructor will run exactly
once during the lifetime of the appdomain, and you should see only one
entry in the file.

Does that make sense or am I not understanding the question?

--
Scott
http://www.OdeToCode.com/blogs/scott/
On 20 Oct 2004 10:28:58 -0700, ka**********@gmail.com (Anurag K)
wrote:
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.Ma pPath(".")+@"\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.


Nov 18 '05 #2
Hello,

Looks like you want a singleton per web request. It is not possible in a way
you're trying to do it. I don't know is there a way to do it - other than
creating it e.g. in Page.OnInit or in similar place and disposing it in
Page.OnUnload.

Singleton is really a singleton. Only one instance is living at one time in
the application. And, all clients and sessions and web requests share the
same application.

JMu

"Anurag K" <ka**********@gmail.com> wrote in message
news:7e**************************@posting.google.c om...
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.Ma pPath(".")+@"\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.

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: weberjacob | last post by:
Sometimes I want to use certain data and functions anywhere in my code; for example, when writing logging routines. In PHP5 I could write a class with all static members and functions, a singleton...
16
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
7
by: Robert W. | last post by:
I've just been reading all about the Singleton class and understand how to implement and use it but I cannot understand why one NEEDS to use it instead of just declaring a class and implementing...
2
by: Hadidi | last post by:
I want to make a class that : - will have one and only one object - when you try to make another object of this class , an IDE error should be seen . like using undefined variable , or any thing...
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
4
by: ayan4u | last post by:
in a singleton class do we need to call a destructor explicitly....as everythng is made private there....
2
by: BLUE | last post by:
Since singleton classes conceptually are like static classes, the are supposed to last for the entire lifetime of the application. Starting from this point tell me if I'm wrong saying: - it make...
2
by: BLUE | last post by:
FirstClass members: - static int counter; - SingletonClass sc = SingletonClass.Instance; Moreovere FirstClass uses a static class named SecondClass with a static property...
2
by: Bob Johnson | last post by:
Just wondering the extent to which some of you are implementing classes as Singletons. I'm working on a brand new project and, early on, identified some obvious candidates. By "obvoius candidates"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.