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
c # singleton autofac


source to share


No one has answered this question yet

Check out similar questions:

2753
Case insensitive "Contains (string)"
1918
Catching multiple exceptions at once?
1270
Why not inherit from List <T>?
7
Type "***" is not assigned to service "***" in Autofac
2
C # - Autofac allows new instances in different classes
2
Autofac error: instance registration can only support SingleInstance ()
1
MVC 3 Autofac registration and authorization
1
Autofac SingleInstance () and Xamarin Forms
0
Registering Autofac Module in ASP.NET WebAPI
0
Autofac dependency resolution error



All Articles
Loading...
X
Show
Funny
Dev
Pics