how did a singleton object achieve application scope in asp.net when it was declared a page variable

R

Rich

The following code produced a singleton object with application scope
when it should have had page scope:

public class Singleton
{
private static Singleton uniqueInstance = null;
private Singleton()
{
}
public static Singleton getInstance()
{
if (uniqueInstance == null)
uniqueInstance = new Singleton();
return uniqueInstance;
}
}
public partial class Default1:page
{
Singleton s = Singleton.getInstance();
void Page_Load(...)
{...}
void Button1Clicked(...)
{...}
}
public partial class Default2:page
{
Singleton s = Singleton.getInstance(); // this retrieved the
instance from Default1
void Page_Load(...)
{...}
void Button1Clicked(...)
{...}
}



Can anyone please explain how a Singleton object created within a Page
class is able to get application scope?


Rich
 
R

Rune B

sure, it's stored for all users in the static field " private static
Singleton uniqueInstance; "
-- Instead you can save it in the HttpContext.Items collection, so it is a
"singleton per HttpRequest" instead:

public class Singleton
{
private Singleton()
{
}

static Singleton(){
_ticket = new object();
}

private static object _ticket;

public static Singleton getInstance(HttpContext context)
{
Singleton uniqueInstance = context.Items[_ticket] as Singleton;
if(uniqueInstance == null)
{
uniqueInstance = new Singleton();
context.Items.Add(_ticket, uniqueInstance);
}
return uniqueInstance;
}

}

R-)
 
K

Karl Seguin [MVP]

There's a great write-up in singleton pattern in C# and making it thread
safe - your implementation isn't - at:
http://www.yoda.arachsys.com/csharp/singleton.html

But whatever mechanics you use to implement a singleton, static fields are
scoped to the entire appDomain/application. I'm not sure why you think it
should be page scoped, when you declare a static field there's only one
instance for the entire appdomain..that's just how it works :)

What exact scope are you looking for? For a given page and all users (create
a static field in the page)? For a given user (store in the session)? For a
given request(store in the HttpContext)...

Karl
 
R

Rich

Thanks all for the response. In regard to Karl's, wouldn't a given
request automatically have a session associated with that request (ie 1
user = 1 request)?

Rich
 
K

Karl Seguin [MVP]

A request is shorter than lived than a session. Yes a request is associated
with a session,but you can scope a singleton to live only for the individual
request - subsequent request (made by the same user in the same session)
would have a new instance.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top