How to interact between projects within the same .NET solution?

I have a .NET Core (UWP) application that has 3 different projects (let them name A, B and C).

A and B are Windows runtime components, and C is a simple class library.

Projects A and B have a link to project C.

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

I thought of one template (of course) using the Lazy.NET 4 method

The problem is that the instance is not the same every time projects A and B access the instance. The Lazy method creates a new instance of the class because it does not seem to have been created before. I wonder if I can share one single between different solution projects. I read that the project is associated with a process, and each process has its own memory space that cannot be used. Is there a solution to my problem?

EDIT : Here is 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 exchange a singleton connection between different assemblies using tools such as Castle Windsor, Ninject, Autofac, or Unity?

+5
source share
1 answer

The Singleton class will not work here - each process will have its own memory and, therefore, its own singleton.

AFAIK in UWP for interprocess communication (between background tasks and the main user interface, except for audio recordings) you need some kind of broker. For short synchronization you can use LocalSettings, for more complex scenarios, perhaps a file / database in LocalFolder. If you decide to use the file, you will need to synchronize the processes, for this you have objects such as Mutex nad WaitHandles.

+1
source

Source: https://habr.com/ru/post/1244888/


All Articles