I know that what is singleton pattern and how to create it and how to make it thread safe using locking and double checking but all I want to know " let's consider a scenario I have a .dll which does a have class that is singleton. Now there are two application which are using the .dll and accessing the singleton class object." So, singleton pattern provide single object application wide or it works across applications. in case, it doesn't work across application, then how to create a application wide singleton object without using any WCF or Web Service. Basically I want that a class object should be accessed across application should be same, using MarshalByRef or something .
-
Assuming your instance is in a static variable then it will be one instance per app domain. – Ben Robinson Nov 13 '14 at 13:01
-
@BenRobinson, per app domain is nothing but per application. Isn't it? – Rahul Nov 13 '14 at 13:03
-
@Rahul Usually yes, but it is possible to have more than one app domain in a process. – Ben Robinson Nov 13 '14 at 13:04
-
You can create WCF server or service which will keep singleton and every client will ask and work with this object. Or another way is to use system pipes to communicate between applications. Last "solution" is to keep object serialized somewhere and access this serialized file which is "singleton" - but thats just stupid :) – Martin Ch Nov 13 '14 at 13:11
1 Answers
You cannot, using language features alone, create an object instance that is a singleton across app domains, never mind processes. So, no…what you're asking to do is not literally possible
It seems you have specifically excluded various remoting APIs too, so you'll have to implement this at a lower level of abstraction. I.e. explicitly managed inter-process communications, such as named pipes or sockets.
I would try very hard to avoid even having this requirement. But assuming you have some single machine-wide resource that truly needs to be treated as a singleton, IMHO the best way to approach it would be to create a service that is always running, and which serves up whatever resource is supposed to be shared by all of the running instances of your program.
Finally note that you can easily ensure only one instance of your program (for a given user login session or machine-wide, depending on implementation). The usual way of doing this is to use a named mutex, which your program will create/check on initialization, and if some other instance of the program has already created it, the new instance will exit. See How to restrict a program to a single instance for more details on that.

- 1
- 1

- 68,759
- 7
- 102
- 136
-
@user3085342: not sure what your point is. Even a cursory reading of Cleary's article conveys clearly that the data structure created is not a true singleton. It relies on proxies in the other app domains to talk to the instance in the default app domain. And indeed, cross-domain communication is closely related to the IPC I describe above as the necessary implementation detail to support this scenario. – Peter Duniho Jul 14 '17 at 05:44