Search code examples
c#dependency-injectioncaliburn.microeventaggregator

Caliburn Micro Constructor Injection Failed


I am learning Caliburn Micro and try to make use of the EventAggregator from the official site.

However, I got an exception

"No parameterless constructor defined for this object."

The message itself is clear but the example doesn't include a parameterless constructor either. If i add one, the constructor with parameter is not hit and the IEventAggregator is still not injected correctly.

Here is my publisher VM after adding the parameterless constructor (without it, exception will be thrown):

    public MainViewModel() { }

    public MainViewModel(IEventAggregator ea) : this()
    {
        eventAggregator = ea;
    }

And here is the bootstrapper i am using:

public class AppBootstrapper : Bootstrapper<MainViewModel>
{
    private readonly SimpleContainer container = new SimpleContainer();

    protected override void Configure()
    {
        container.Singleton<IEventAggregator, EventAggregator>();
    }
}

Here is the example from CM:

// Creating the EventAggregator as a singleton.
public class Bootstrapper : BootstrapperBase {
    private readonly SimpleContainer _container =
        new SimpleContainer();

     // ... Other Bootstrapper Config

    protected override void Configure(){
        _container.Singleton<IEventAggregator, EventAggregator>();
    }

    // ... Other Bootstrapper Config
}

// Acquiring the EventAggregator in a viewModel.
public class FooViewModel {
    private readonly IEventAggregator _eventAggregator;

    public FooViewModel(IEventAggregator eventAggregator) {
        _eventAggregator = eventAggregator;
    }
}

I checked this post (Inject EventAggregator into ViewModel with Caliburn Micro) but it simply said nothing why the CM do not invoke the constructor with injection.

I also checked the CM's sample solution but it uses MEF as the DI solution.

What do I miss?


Solution

  • // ... Other Bootstrapper Config
    

    It's the other bootstrapper config that's important.

    The best option is to install Caliburn.Micro via the Caliburn.Micro.Start NuGet package, and have a look at the provided bootstrapper which also uses the SimpleContainer provided by Caliburn.Micro.

    Here it is in full:

    public class AppBootstrapper : BootstrapperBase
    {
         SimpleContainer container;
    
         public AppBootstrapper()
         {
             Start();
         }
    
         protected override void Configure()
         {  
             container = new SimpleContainer();
             container.Singleton<IWindowManager, WindowManager>();
             container.Singleton<IEventAggregator, EventAggregator>();
             container.PerRequest<IShell, ShellViewModel>();
         }
    
         protected override object GetInstance(Type service, string key)
         {
             var instance = container.GetInstance(service, key);
             if (instance != null)
                 return instance;
             throw new InvalidOperationException("Could not locate any instances.");
         }
    
         protected override IEnumerable<object> GetAllInstances(Type service)
         {
             return container.GetAllInstances(service);
         }
    
         protected override void BuildUp(object instance)
         {
             container.BuildUp(instance);
         }
    
         protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
         {
             DisplayRootViewFor<IShell>();
         }
    }