473,779 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to share one instance of an object across an ASP dot net application.

I want to share one instance of an object across an ASP dot net
application, and I'm trying to weigh the pros and cons of the following
two approaches:

a) Storing the object in ApplicationStat e
b) Storing the object in a static member variable (utilizing a
singleton design pattern)

Any recommendations on which method would be more suitable?
What happens in each of these cases if IIS decides to spawn a new app
domain?
Are there any other approaches that might be better than the ones I
mentioned?

The object in question is a dot net class wrapping a COM component.
The class creates an SSL session to a proprietary device that manages
encryption and decryption.

Thanks,
Jonathan

May 15 '06 #1
5 1454

Check my blog:
http://spaces.msn.com/sholliday/ 10/24/2005 entry

You can easily convert this to a Application holder.

...

If you're not using a server farm, then my blog idea (converted to
application) could work.

If you're using a server farm, you may need to go to remoting, and have all
members of the web server farm talk to the 1 remoting server.

Actually, I just found the code for the Application one.
Read the blog for the explanation, and here is the code.


using System;
using System.Collecti ons;
using System.Collecti ons.Specialized ;
namespace GranadaCoder.Ca chingFramework
{

public class WebApplicationD ataStore : IDataStore
{
private static string WEB_APPLICATION _OBJECT_GUID =
"B777D4C2-1576-40C3-88F8-FA16E94DDC90"; //ensure uniqueness, other than
that doesn't serve any purpose
private static WebApplicationD ataStore singletonInstan ce = null;
private Hashtable m_memoryStore = null;

private WebApplicationD ataStore()
{
this.m_memorySt ore = new Hashtable();
}

public static WebApplicationD ataStore GetInstance()
{
if (null != System.Web.Http Context.Current )
{
if (null != System.Web.Http Context.Current .Application )
{
if (null !=
System.Web.Http Context.Current .Application[WEB_APPLICATION _OBJECT_GUID] )
{
singletonInstan ce =
((WebApplicatio nDataStore)(Sys tem.Web.HttpCon text.Current.Ap plication.Get(W E
B_APPLICATION_O BJECT_GUID)));
}
}
}
if ((singletonInst ance == null))
{
singletonInstan ce = new WebApplicationD ataStore();

System.Web.Http Context.Current .Application.Ad d(WEB_APPLICATI ON_OBJECT_GUID,
singletonInstan ce);
}
return singletonInstan ce;
}

public void Clear()
{
this.m_memorySt ore.Clear();
}

public void Add(string key, object value)
{
if (this.m_memoryS tore.Contains(k ey))
{
this.m_memorySt ore.Remove(key) ;
}
this.m_memorySt ore.Add(key, value);
}

public object Remove(string key)
{
object returnObject = null;
if (null != this.m_memorySt ore )
{
if (this.m_memoryS tore.Contains(k ey))
{
returnObject = this.m_memorySt ore[key];
this.m_memorySt ore.Remove(key) ;
}
}
return returnObject;
}

public object this[string key]
{
get
{
if (null != this.m_memorySt ore[key] )
{
return this.m_memorySt ore[key];
}
return null;
}
}

public int Size
{
get
{
if (null != this.m_memorySt ore )
{
return this.m_memorySt ore.Count;
}
return 0;
}
}
}
}


<ne************ @hotmail.com> wrote in message
news:11******** **************@ y43g2000cwc.goo glegroups.com.. .
I want to share one instance of an object across an ASP dot net
application, and I'm trying to weigh the pros and cons of the following
two approaches:

a) Storing the object in ApplicationStat e
b) Storing the object in a static member variable (utilizing a
singleton design pattern)

Any recommendations on which method would be more suitable?
What happens in each of these cases if IIS decides to spawn a new app
domain?
Are there any other approaches that might be better than the ones I
mentioned?

The object in question is a dot net class wrapping a COM component.
The class creates an SSL session to a proprietary device that manages
encryption and decryption.

Thanks,
Jonathan

May 15 '06 #2

You need to remove the Interface reference IDataStore, fyi.

Its a simple interface, forcing the Add / Clear / Size and Remove methods.
(maybe the indexer too?)

But you don't necessarily need it. Just remove it to compile.

"sloan" <sl***@ipass.ne t> wrote in message
news:es******** ******@TK2MSFTN GP03.phx.gbl...

Check my blog:
http://spaces.msn.com/sholliday/ 10/24/2005 entry

You can easily convert this to a Application holder.

..

If you're not using a server farm, then my blog idea (converted to
application) could work.

If you're using a server farm, you may need to go to remoting, and have all members of the web server farm talk to the 1 remoting server.

Actually, I just found the code for the Application one.
Read the blog for the explanation, and here is the code.


using System;
using System.Collecti ons;
using System.Collecti ons.Specialized ;
namespace GranadaCoder.Ca chingFramework
{

public class WebApplicationD ataStore : IDataStore
{
private static string WEB_APPLICATION _OBJECT_GUID =
"B777D4C2-1576-40C3-88F8-FA16E94DDC90"; //ensure uniqueness, other than
that doesn't serve any purpose
private static WebApplicationD ataStore singletonInstan ce = null;
private Hashtable m_memoryStore = null;

private WebApplicationD ataStore()
{
this.m_memorySt ore = new Hashtable();
}

public static WebApplicationD ataStore GetInstance()
{
if (null != System.Web.Http Context.Current )
{
if (null != System.Web.Http Context.Current .Application )
{
if (null !=
System.Web.Http Context.Current .Application[WEB_APPLICATION _OBJECT_GUID] )
{
singletonInstan ce =
((WebApplicatio nDataStore)(Sys tem.Web.HttpCon text.Current.Ap plication.Get(W E B_APPLICATION_O BJECT_GUID)));
}
}
}
if ((singletonInst ance == null))
{
singletonInstan ce = new WebApplicationD ataStore();

System.Web.Http Context.Current .Application.Ad d(WEB_APPLICATI ON_OBJECT_GUID, singletonInstan ce);
}
return singletonInstan ce;
}

public void Clear()
{
this.m_memorySt ore.Clear();
}

public void Add(string key, object value)
{
if (this.m_memoryS tore.Contains(k ey))
{
this.m_memorySt ore.Remove(key) ;
}
this.m_memorySt ore.Add(key, value);
}

public object Remove(string key)
{
object returnObject = null;
if (null != this.m_memorySt ore )
{
if (this.m_memoryS tore.Contains(k ey))
{
returnObject = this.m_memorySt ore[key];
this.m_memorySt ore.Remove(key) ;
}
}
return returnObject;
}

public object this[string key]
{
get
{
if (null != this.m_memorySt ore[key] )
{
return this.m_memorySt ore[key];
}
return null;
}
}

public int Size
{
get
{
if (null != this.m_memorySt ore )
{
return this.m_memorySt ore.Count;
}
return 0;
}
}
}
}


<ne************ @hotmail.com> wrote in message
news:11******** **************@ y43g2000cwc.goo glegroups.com.. .
I want to share one instance of an object across an ASP dot net
application, and I'm trying to weigh the pros and cons of the following
two approaches:

a) Storing the object in ApplicationStat e
b) Storing the object in a static member variable (utilizing a
singleton design pattern)

Any recommendations on which method would be more suitable?
What happens in each of these cases if IIS decides to spawn a new app
domain?
Are there any other approaches that might be better than the ones I
mentioned?

The object in question is a dot net class wrapping a COM component.
The class creates an SSL session to a proprietary device that manages
encryption and decryption.

Thanks,
Jonathan


May 15 '06 #3
>You can easily convert this to a Application holder.

Thanks for the reply.
I should have specified that I don't mind having one instance fof the
object for each web server in the farm.
I want to keep this solutionas simple as possible.

May 15 '06 #4
while either method works, the singleton is a better pattern because of the
strong typing.

in either case a recycle is the same, you can end up with two instances of
the com object, on in the old domain and one in the new. if this is a
problem, then you will have to add unmanged code to handle it.

also, if its a STA com component (say a vb6 object)., then special
consideration are required to for asp.net to support STA objects and there
is a performance cost.
-- bruce (sqlwork.com)

<ne************ @hotmail.com> wrote in message
news:11******** **************@ y43g2000cwc.goo glegroups.com.. .
I want to share one instance of an object across an ASP dot net
application, and I'm trying to weigh the pros and cons of the following
two approaches:

a) Storing the object in ApplicationStat e
b) Storing the object in a static member variable (utilizing a
singleton design pattern)

Any recommendations on which method would be more suitable?
What happens in each of these cases if IIS decides to spawn a new app
domain?
Are there any other approaches that might be better than the ones I
mentioned?

The object in question is a dot net class wrapping a COM component.
The class creates an SSL session to a proprietary device that manages
encryption and decryption.

Thanks,
Jonathan

May 15 '06 #5
>while either method works, the singleton is a better pattern because of the
strong typing.


Thanks for the reply!

May 16 '06 #6

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

Similar topics

21
2990
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can spare the time, I would appreciate any comments (including words like evil and disgusting, if you think they are applicable :-}) on the example here. Kenny -
1
4243
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here. From the log it looks like the problem happens when 2 threads insert 1 record each in the same table and then try to aquire a NS (Next Key Share) lock on the record inserterd by the other thread. Thanks Rohit
1
3399
by: Macca | last post by:
Hi, I have a C# Solution/Application that contain 4 projects. Each of these projects needs at some time to access the same database. I would like to know how to share a single connection between these projects as i assume this would be more efficient than having a separate connection string for each project.
1
1345
by: Jheitmuller | last post by:
Hi, What is the best way to share a C# web form across web application? I'm new to ASP.NET. I've read though the docs and I must be missing something. I do not see and appropriate library type for this. I have a .aspx web form that I want to make available for other developers in my site. It is a powerful/flexible database browsing form. I can get it to work as a form in the directory of the
12
5342
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and the windows application. I face the following problem when I send this class to the webservice.
3
7627
by: Noremac | last post by:
My google skills must be dwindling. I am trying to determine how in ASP.NET 2.0 I can get the ReturnUrl querystring variable in Forms Authentication to contain the absolute url. Just like others that have posed this question, we are an enterprise environment that has multiple websites across multiple servers and we are trying to setup Web SSO for our public internet site that will be accessible by our clients. ASP.NET seems to have...
4
3148
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
6
1420
by: JB | last post by:
Hi All, I'm building a DLL that can be used by several applications. I'd like to have one single instance of a Class stored in my DLL that would be ("shared") by all the applications using that DLL. I've tried to declare in my DLL a Class with a single "Shared" member, but each time I reference the DLL from another application, a new instance is created.
1
7113
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
0
9636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10139
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.