5

I have a .NET Core (UWP solution) application which has 3 different projects (let's call them A, B and C).

A and B are Windows Runtime Components, and C is a simple Class Library.

Projects A and B have a reference to the project C.

I would like to access a class of the project C, whose the instance would be shared between projects A and B.

I thought to a singleton pattern (of course), using the Lazy method of .NET 4

Problem is that the instance is not the same each time A and B projects access the instance. The Lazy method creates a new instance of the class because it seems not to be previously created. I wonder if I can share a singleton between different projects of a solution. I've read that a project is associated to a process, and each process has its own memory space which cannot be shared. Is there a solution to my issue?

EDIT: Here's my implementation of my HubService class:

    private static readonly Lazy<HubService> Lazy =
        new Lazy<HubService>(() => new HubService(), LazyThreadSafetyMode.ExecutionAndPublication);

    private HubService()
    {
        _asyncQueue = new AsyncQueue<Guid>();
    }

    public static HubService Instance => Lazy.Value;

Also, is it possible to share a singleton across different assemblies using tools like Castle Windsor, Ninject, Autofac or Unity?

  • We need to see your implementation of the singleton, as well as the way you use it in both A and B. – Glubus Mar 11 '16 at 15:04
  • 1
    It's not the same instance, so you can't. Your project C assembly is loaded into A and B separately, and it doesn't just magically share memory between 2 processes – libertylocked Mar 11 '16 at 15:04
  • Do a static sealed singleton implementation of your class initialization. This will prevent multiple threads accessing your lazy initialization. – Maertin Mar 11 '16 at 15:06
  • @Glubus I've edited my question –  Mar 11 '16 at 15:07
  • @LibertyLocked Yeah that's what I'm thinking about. Is there an alternative way? –  Mar 11 '16 at 15:07
  • You might also want to do a lock. the section at the bottom should help. https://msdn.microsoft.com/en-us/library/ff650316.aspx – Maertin Mar 11 '16 at 15:09
  • @Maertin Lazy is a better implementation of a thread-safe singleton. No need to use lock I think when using Lazy –  Mar 11 '16 at 15:09
  • possible duplicate of http://stackoverflow.com/questions/9803432/sharing-variables-between-running-applications-in-c-sharp – libertylocked Mar 11 '16 at 15:21
  • Consider using Windows Communication Foundation (WCF) for inter process communication. – Ɖiamond ǤeezeƦ Mar 11 '16 at 17:07
  • @ƉiamondǤeezeƦ I think WCF is overkill here, especially for a UWP app. What about Autofac or any other DI framework? Is it possible to share a singleton across different assemblies using tools like Castle Windsor, Ninject, Autofac or Unity? –  Mar 11 '16 at 21:29
  • You could use an out of process cache to store objects/data. AppFabric for example. – Ɖiamond ǤeezeƦ Mar 11 '16 at 22:45

1 Answers1

1

Singleton class won't work here - every process will has its own memory and so its own singleton.

AFAIK in UWP for inter process (between background tasks and main UI, apart from audio task) communication you will need some kind of a broker. For short synchronization you may think of using LocalSettings, for more complicated scenarios maybe a file/database in LocalFolder. If you decide to use a file you will need to synchronize processes, for this purpose you have objects like Mutex nad WaitHandles.

Romasz
  • 29,662
  • 13
  • 79
  • 154