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

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.Instance;
singleton.setValue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Instance;
String testing = singleton.getValue();
Console.Out.Writeline("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 4415
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.queensu.ca> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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.Instance;
singleton.setValue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Instance;
String testing = singleton.getValue();
Console.Out.Writeline("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.com

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:e4**************@TK2MSFTNGP12.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.queensu.ca> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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.Instance;
singleton.setValue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Instance;
String testing = singleton.getValue();
Console.Out.Writeline("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.queensu.ca> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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.Instance;
singleton.setValue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Instance;
String testing = singleton.getValue();
Console.Out.Writeline("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.StaticMethod()", but at runtime this is (essentially) "{MyObject
version 1.0.123.12315 signed by <hash>}.StaticMethod()".

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>}.StaticMethod()}"; 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.queensu.ca> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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.MyObject and Assembly2.MyObject, 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.com> wrote in message
news:Or**************@TK2MSFTNGP09.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.queensu.ca> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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.Instance;
singleton.setValue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Instance;
String testing = singleton.getValue();
Console.Out.Writeline("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.com> wrote in
message news:uQ**************@tk2msftngp13.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.com

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:e4**************@TK2MSFTNGP12.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.queensu.ca> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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.Instance;
singleton.setValue("test");

--Assembly 2--
-Foo.cs-
Singleton singleton = Singleton.Instance;
String testing = singleton.getValue();
Console.Out.Writeline("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.Singleton and in the other
project it woudl get General.Singleton.

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
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...
4
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...
1
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...
3
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...
17
by: Tom | last post by:
This is not intuitivelly clear.
3
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...
13
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...
10
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"...
3
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...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.