I have an application that dynamically starts different processes. Each one of that processes consumes an autofac CoreModule, and each one has his own Module for that very process.
CoreModule defines a few components that must be SingleInstance across all the processes of the application.
For example let's say I have registered:
builder.RegisterType<AcrossComponent>().As<IAcrossComponent>().SingleInstance();
The thing is that on every Bootstrapper that registers CoreModule, they create one instance of AcrossComponent that is SingleInstance on that very IContainer.
I managed to solve it by using AcrossComponent as a facade of a book singleton
namespace Sample
{
public class AcrossComponent : IAcrossComponent
{
public AcrossComponent()
{
}
public void DoSomething()
{
RealAcrossComponent.Instance.DoSomething();
}
}
private class RealAcrossComponent
{
private static RealAcrossComponent _instance;
public static RealAcrossComponent Instance
{
get
{
if(_instance == null)
_instance = new RealAcrossComponent();
return _instance;
}
}
private RealAcrossComponent()
{
}
public void DoSomething()
{}
}
}
Of course this is not the way I'd like to solve this need, Autofac probably solves it in anyway I could not find.
(Already tried by registering same instance of CoreModule, but it seems it changes nothing, its just a facade for the registrations)
Thanks in advance!
Edit 1:
The application is a Windows Form, where user configure stuff, and contains multiple buttons, each one hits the entry point:
public static class BackGroundProcess1{
public static void StartBackground()
{
//creates container with CoreModule, BackGround1Module and starts everything.
_container.Resolve<IMainComponent>().StartBackground();
}
}
There are 4 classes like that, that acts like an entry point for all the things that start processing in background. I would like all those 4 to share the CoreModule instances.