473,382 Members | 1,512 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,382 software developers and data experts.

Static members in ASP .net

RSH
If I have a static method that references the HttpContext.Current object is
it safe to be in a static method within a class under ASP .Net?

I know that static classes are shared by all sessions at a given time, so Im
not sure if this can be a static class.

public static SessionManager Instance

{

get

{

HttpContext context = HttpContext.Current;

SessionManager manager = context.Session[SESSION_MANAGER] as SessionManager;

if (manager == null)

{

manager = new SessionManager();

context.Session[SESSION_MANAGER] = manager;

}

return manager;

}

}

Thanks!

Ron
Oct 9 '07 #1
3 3389
This will be valid as long as you call it from methods that are involved in
a postback - this is, areas that actually have a context. If you call this
method from areas that don't have a context - such as a timer callback, or a
thread function, or an async method completion (that doesn't go through the
Async Manager), then this won't work.

Now, with that said, your implementation of the Singleton pattern is broken.
For the reasons behind this, see Jon Skeet's Singleton page at (speficially,
your code is almost identical to his canonical broken example):
http://www.yoda.arachsys.com/csharp/singleton.html

I think you should be very leery of using a Singleton in ASP.Net if you're
not aware of concurrency and the issues that can arise from it (which, based
on your singleton code, you're not really aware of yet...).

ASP.Net is a highly concurrent environment, but developers are often
shielded from that. As soon as you introduce a singleton into the mix,
you're going to run into all mannor of threading & concurrency gotchas, and
will end up with some very unexpected results if you're not carefull.

--
Chris Mullins

"RSH" <wa*************@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
If I have a static method that references the HttpContext.Current object
is it safe to be in a static method within a class under ASP .Net?

I know that static classes are shared by all sessions at a given time, so
Im not sure if this can be a static class.

public static SessionManager Instance

{

get

{

HttpContext context = HttpContext.Current;

SessionManager manager = context.Session[SESSION_MANAGER] as
SessionManager;

if (manager == null)

{

manager = new SessionManager();

context.Session[SESSION_MANAGER] = manager;

}

return manager;

}

}

Thanks!

Ron


Oct 9 '07 #2
RSH
Chris,

Thanks for the very informative post.

I was a little leery about this code, which I found on a "reputable" site,
but it did solve an issue I was dealing with, but while I certainly dont
have your level of expertise I became a little suspicious with the static
method, and the psuedo singleton implementation...especially in an ASP .Net
app.

Thanks...Im sure you saved me a lot of debugging time!
Ron
"Chris Mullins [MVP - C#]" <cm******@yahoo.comwrote in message
news:OE**************@TK2MSFTNGP06.phx.gbl...
This will be valid as long as you call it from methods that are involved
in a postback - this is, areas that actually have a context. If you call
this method from areas that don't have a context - such as a timer
callback, or a thread function, or an async method completion (that
doesn't go through the Async Manager), then this won't work.

Now, with that said, your implementation of the Singleton pattern is
broken. For the reasons behind this, see Jon Skeet's Singleton page at
(speficially, your code is almost identical to his canonical broken
example):
http://www.yoda.arachsys.com/csharp/singleton.html

I think you should be very leery of using a Singleton in ASP.Net if you're
not aware of concurrency and the issues that can arise from it (which,
based on your singleton code, you're not really aware of yet...).

ASP.Net is a highly concurrent environment, but developers are often
shielded from that. As soon as you introduce a singleton into the mix,
you're going to run into all mannor of threading & concurrency gotchas,
and will end up with some very unexpected results if you're not carefull.

--
Chris Mullins

"RSH" <wa*************@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>If I have a static method that references the HttpContext.Current object
is it safe to be in a static method within a class under ASP .Net?

I know that static classes are shared by all sessions at a given time, so
Im not sure if this can be a static class.

public static SessionManager Instance

{

get

{

HttpContext context = HttpContext.Current;

SessionManager manager = context.Session[SESSION_MANAGER] as
SessionManager;

if (manager == null)

{

manager = new SessionManager();

context.Session[SESSION_MANAGER] = manager;

}

return manager;

}

}

Thanks!

Ron



Oct 9 '07 #3
As Chris pointed out field name "Instance" indicates you are attempting to
provide a Singleton (only ever one instance can be created), and if that's
the case the implementation is broken / incomplete.
If it *isn't* intended to be a Singleton, then please use another name for
the field than "Instance".
Peter
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"RSH" wrote:
If I have a static method that references the HttpContext.Current object is
it safe to be in a static method within a class under ASP .Net?

I know that static classes are shared by all sessions at a given time, so Im
not sure if this can be a static class.

public static SessionManager Instance

{

get

{

HttpContext context = HttpContext.Current;

SessionManager manager = context.Session[SESSION_MANAGER] as SessionManager;

if (manager == null)

{

manager = new SessionManager();

context.Session[SESSION_MANAGER] = manager;

}

return manager;

}

}

Thanks!

Ron
Oct 10 '07 #4

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

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
6
by: Matt | last post by:
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.