473,625 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When there are multiple assemblies there are multiple singletons

Hi all, I'm having a problem where in my solution that contains
multiple projects - I instantiate a singleton class in one assembly and
then if another assembly tries to use that singleton class another
instance of it is created.

Basically:

--Assembly 1 (the executable)--
-Main.cs-

Singleton singleton = Singleton.Insta nce;
singleton.setVa lue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Insta nce;
String testing = singleton.getVa lue();
Console.Out.Wri teline("test: "+testing);

The console outputs null because apparently two asseblies can't use the
singleton pattern in a cohesive manner?

Novice

Nov 17 '05 #1
8 4437
Singleton should work across assemblies if they are in the same process. Is
your implementation correct? Make sure you are using static variables
correctly. Have you debugged your code?

<6t**@qlink.que ensu.ca> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi all, I'm having a problem where in my solution that contains
multiple projects - I instantiate a singleton class in one assembly and
then if another assembly tries to use that singleton class another
instance of it is created.

Basically:

--Assembly 1 (the executable)--
-Main.cs-

Singleton singleton = Singleton.Insta nce;
singleton.setVa lue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Insta nce;
String testing = singleton.getVa lue();
Console.Out.Wri teline("test: "+testing);

The console outputs null because apparently two asseblies can't use the
singleton pattern in a cohesive manner?

Novice

Nov 17 '05 #2
This isn't completely true. Assuming the singletons are stored as
static variables, the singletons can be shared across the same app domain,
not across the process.

You could run multiple app domains in the same process, and each one
would have it's own copy of the static variable.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Rilling" <pe***@nospam.r illing.net> wrote in message
news:e4******** ******@TK2MSFTN GP12.phx.gbl...
Singleton should work across assemblies if they are in the same process.
Is your implementation correct? Make sure you are using static variables
correctly. Have you debugged your code?

<6t**@qlink.que ensu.ca> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi all, I'm having a problem where in my solution that contains
multiple projects - I instantiate a singleton class in one assembly and
then if another assembly tries to use that singleton class another
instance of it is created.

Basically:

--Assembly 1 (the executable)--
-Main.cs-

Singleton singleton = Singleton.Insta nce;
singleton.setVa lue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Insta nce;
String testing = singleton.getVa lue();
Console.Out.Wri teline("test: "+testing);

The console outputs null because apparently two asseblies can't use the
singleton pattern in a cohesive manner?

Novice


Nov 17 '05 #3
Are they *exactly* the same assembly? In particular, if they are strongly
named, then they must be *exactly* the same version, otherwise they count as
completely independent assemblies.

Marc

<6t**@qlink.que ensu.ca> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi all, I'm having a problem where in my solution that contains
multiple projects - I instantiate a singleton class in one assembly and
then if another assembly tries to use that singleton class another
instance of it is created.

Basically:

--Assembly 1 (the executable)--
-Main.cs-

Singleton singleton = Singleton.Insta nce;
singleton.setVa lue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Insta nce;
String testing = singleton.getVa lue();
Console.Out.Wri teline("test: "+testing);

The console outputs null because apparently two asseblies can't use the
singleton pattern in a cohesive manner?

Novice

Nov 17 '05 #4
Can you elaborate on what you mean Marc? I think the problem may
actually does relate to the naming, specifically the namespace. I
tried using the same pattern in a very small program and the problem
didn't occur. I thought the problem might because I a bunch of
"using" statements and somehow the same class in one namespace is
actually getting compiled into two separate classes in compiled code -
therefore, perhaps the problem wouldn't have occured if I have fully
qualified the singleton class's name.

Thoughts?

Novice

Nov 17 '05 #5
It doesn't really relate to the namespaces, per se, unless you are using
reflection to create the objects by name...

If you are strongly-naming your assemblies (that is, you are signing them
with an SNK or PFX), then when you reference that assembly the reference
includes not only the assembly name (MyLibrary), but also the exact version
number and a hash-value of the SNK. In your source-code, it might say
"MyObject.Stati cMethod()", but at runtime this is (essentially) "{MyObject
version 1.0.123.12315 signed by <hash>}.StaticM ethod()".

If you re-build your library (either as a different version or with a
different SNK) and get another assembly in the AppDomain to reference the
new version, it could ask for "{MyObject version 1.0.324.23421 signed by
<hash>}.StaticM ethod()}"; although they look similar, they are completely
different objects, and will not play well together; you would not, for
instance, be able to cast between them... Version policies in the GAC (if
you use GAC - personally I don't like to) can be used to say "if somebody
asks for 1.0.123.12315, then give them 1.0.324.23421 instead", but otherwise
you could get both in parallel... I think... maybe

Not sure if this *is* your issue, or even how much of this will happen in
reality... but the short of it is:

Objects are scoped by their assembly; strongly-named assemblies are
determined by their name, their full version, and their hash... If *any* of
these are different, you actually have 2 different objects.

Marc

<6t**@qlink.que ensu.ca> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Can you elaborate on what you mean Marc? I think the problem may
actually does relate to the naming, specifically the namespace. I
tried using the same pattern in a very small program and the problem
didn't occur. I thought the problem might because I a bunch of
"using" statements and somehow the same class in one namespace is
actually getting compiled into two separate classes in compiled code -
therefore, perhaps the problem wouldn't have occured if I have fully
qualified the singleton class's name.

Thoughts?

Novice

Nov 17 '05 #6
Actually, I misunderstood your example - it is even easier, then:

My previous statement holds: "Objects are scoped by their assembly"...

If you have Assembly1.MyObj ect and Assembly2.MyObj ect, then it doesn't
matter if MyObject is the same source-code file, they are completely
unrelated objects at runtime... if anything in Assembly1 asks for MyObject
it gets its version (from Assembly1)... and if anything in Assembly2 asks
for MyObject it gets its local version (i.e. from Assembly2).

The solution is to use what I thought you were doing... you create a shared
assembly to host your singleton...

Create MyLibrary that contains MyObject, and reference this from both
Assembly1 and Assembly2... Now when either Assembly1 or Assembly2 attempt to
use MyObject, it gets the one from MyLibrary : presto, job done, go down pub
(or whatever)

I thought that you had already done this fundamental step, and that you were
still getting 2 versions... which would only be possible if Assembly1 and
Assembly2 were somehow using parallel versions of MyLibrary.

Let me know if this helps...

Marc

"Marc Gravell" <mg******@rm.co m> wrote in message
news:Or******** ******@TK2MSFTN GP09.phx.gbl...
Are they *exactly* the same assembly? In particular, if they are strongly
named, then they must be *exactly* the same version, otherwise they count
as completely independent assemblies.

Marc

<6t**@qlink.que ensu.ca> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi all, I'm having a problem where in my solution that contains
multiple projects - I instantiate a singleton class in one assembly and
then if another assembly tries to use that singleton class another
instance of it is created.

Basically:

--Assembly 1 (the executable)--
-Main.cs-

Singleton singleton = Singleton.Insta nce;
singleton.setVa lue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Insta nce;
String testing = singleton.getVa lue();
Console.Out.Wri teline("test: "+testing);

The console outputs null because apparently two asseblies can't use the
singleton pattern in a cohesive manner?

Novice


Nov 17 '05 #7
True. I sometimes use the terms "process" and "app domain" interchangeably .
A habit that I should break.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:uQ******** ******@tk2msftn gp13.phx.gbl...
This isn't completely true. Assuming the singletons are stored as
static variables, the singletons can be shared across the same app domain,
not across the process.

You could run multiple app domains in the same process, and each one
would have it's own copy of the static variable.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Rilling" <pe***@nospam.r illing.net> wrote in message
news:e4******** ******@TK2MSFTN GP12.phx.gbl...
Singleton should work across assemblies if they are in the same process.
Is your implementation correct? Make sure you are using static variables
correctly. Have you debugged your code?

<6t**@qlink.que ensu.ca> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi all, I'm having a problem where in my solution that contains
multiple projects - I instantiate a singleton class in one assembly and
then if another assembly tries to use that singleton class another
instance of it is created.

Basically:

--Assembly 1 (the executable)--
-Main.cs-

Singleton singleton = Singleton.Insta nce;
singleton.setVa lue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Insta nce;
String testing = singleton.getVa lue();
Console.Out.Wri teline("test: "+testing);

The console outputs null because apparently two asseblies can't use the
singleton pattern in a cohesive manner?

Novice



Nov 17 '05 #8
I REALLY appreciate you taking the time to explain this - but I think
you may misunderstand (my fault no doubt). I don't actually have two
different Singleton classes in 2 different assemblies. My concern
stems from my experience with compilers and how qualified names
sometimes work in them.

Basically I had a concern that I had created a single cs file in one
project (creates a dll for that project) and this single class was
actually getting compiled into two separate managed classes in the
MSIL. Therefore, my theory was that when I used the Singleton class in
one project it would compile into a Library1.Single ton and in the other
project it woudl get General.Singlet on.

Thoughts?

Thanks,
Novice

Nov 17 '05 #9

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

Similar topics

4
2812
by: Andrew James | last post by:
Hi, I've been looking around on Google for the answer to this question, and it's beginning to really bug me. I'm making some design decisions for some code I'm writing, and I'm wondering whether (Good Design Decisions apart), there's a performance impact in importing the same module in two different files. For example, with the following: fileA.py ----------- import psycopg
4
1759
by: Derrick | last post by:
Long story short: I've been working on a project which includes both designtime and runtime components, for both the PC and Pocket PC. While testing, I've been having problems with Visual Studio loading several versions of the DLLs, resulting in Invalid Cast Exceptions. A solution which was suggested to me was to make sure all my designtime stuff is strong named and placed in the GAC. This sounds like it should work, and I am about to...
1
1430
by: loretta.stokes | last post by:
I manage our nightly builds for all of our products. Since we have added our .NET assemblies to our nightly build, it has been a learning process. I have come across a couple of situations that I am not sure how to handle without rebuilding or redeploying some of our common assemblies. First let me explain what we do: 1. Version all assemblies with 1.0.0.* 2. All assemblies are strong named 3. We use project references
3
3150
by: Dominik Rau | last post by:
Hi. I've got the following problem here: In my application, I use a lot of Singletons, that are implemented as described in Gamma et al. (shortened): //.h class Singleton{ public: static Singleton* the(); private: static Singleton* _instance;
17
51439
by: Tom | last post by:
This is not intuitivelly clear.
3
2784
by: Shikari Shambu | last post by:
Hi All, I have a situation where multiple applications are sharing some pages/ controls. So, I have a separate common project that has the common pages/ controls. My question is how do I reference these pages/ controls from my ASP.NET web projects WEbApp1 url http://localhost/app1 C:\Apps\App1
13
2301
by: Brian | last post by:
I have many similar classes in a project, one for each type of report my app can create. I want to instantiate them based on a value passed in by a scheduler module. Right now I have Sub RunReports(sReport) Select Case sReport Case "CRByDistrict" oReport = New CRByDistrict
10
3744
by: eswanson | last post by:
I have broken up my web site into smaller web site projects. When I look at the precompiled files, it always has the virtual directory in them ie: <preserve resultType="3" virtualPath="/Security/DefaultLogin.aspx" hash="fde4916e6" filehash="ffffe84d717a4765" flags="110000" assembly="App_Web_-xo1n4yg" type="ASP.defaultlogin_aspx"> <filedeps> <filedep name="/Security/DefaultLogin.aspx" /> </filedeps> </preserve>
3
2070
by: Claudio Pacciarini | last post by:
Hi everyone, I have a question about .NET code sharing and reuse, and also about application design best practices / guidelines. Currently, we have many different .NET projects in source depot. Although they are different, in some of them we share C# code by referencing source files that are external (not part of the projects) on each project. For instance, some of our projects have the typical “sources” file with:
0
8253
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8189
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
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7182
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 projectplanning, coding, testing, and deploymentwithout 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...
1
6116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
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.