Autofac - SingleInstance for multiple containers

I have an application that dynamically starts different processes. Each of these processes consumes an autofac CoreModule, and each of them has its own module for this very process.

CoreModule defines several components that must be SingleInstance across all processes in the application.

For example, let's say I registered:

builder.RegisterType<AcrossComponent>().As<IAcrossComponent>().SingleInstance();

      

The fact is that on each Bootstrapper that registers CoreModule, they create one AcrossComponent instance, which is a SingleInstance on this very IContainer.

I managed to solve this by using AcrossComponent as singleton book facade

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 how I would like to solve this problem, Autofac probably solves it, somehow I could not find it.

(Already tried registering the same CoreModule instance, but it doesn't seem to change anything, its just a facade to register)

Thanks in advance!

Edit 1:

The application is a Windows Form where the user configures the material and contains several buttons, each of which hits an 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 that act as the entry point for all things that start processing in the background. I would like all these 4 to share instances of CoreModule.

+3


source to share





All Articles