1
votes

i'm a new in a development of WPF applications with PRISM and AKKA.NET frameworks.

In my code for Shell Window I made an instance of ActorSystem.

 public partial class App
{
    private ActorSystem appActorSystem;
    protected override Window CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        appActorSystem = ActorSystem.Create(System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name);
        containerRegistry.RegisterInstance(appActorSystem);
    }


    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        moduleCatalog.AddModule<CommandBar.CommandBarModule>();
    }

    protected override void OnExit(ExitEventArgs e)
    {
        appActorSystem.Terminate();
        appActorSystem.Dispose();
        base.OnExit(e);
    }
}

The instance will be registered in DI Container (Unity) after creation.

In my application i have also a module.

 public class CommandBarModule : IModule
{
    private IContainerProvider _containerProvider;
    public void OnInitialized(IContainerProvider containerProvider)
    {
        _containerProvider = containerProvider;
        var appActorSystem = _containerProvider.Resolve<ActorSystem>();

        var regionManager = _containerProvider.Resolve<IRegionManager>();
        regionManager.RegisterViewWithRegion(regionNames.CommandBar, typeof(ViewA));
    }


    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterForNavigation<ViewA>();
    }
}

I want to get the instance of the Actorsystem in the module. It works fine in CommandBarModule Class.

But I want also to get the instance of my Actorsystem in the ViewModel of the module...

 public class ViewAViewModel : BindableBase
{

    private string _message;
    public string Message
    {
        get { return _message; }
        set { SetProperty(ref _message, value); }
    }


    public ViewAViewModel()
    {

        Message = "test";
    }
}

My First Idea was, that i can do injection of IContainerProvider into the constructor of the ViewModel, like this:

public class ViewAViewModel : BindableBase
{
    private IContainerProvider _containerProvider;

    private string _message;
    public string Message
    {
        get { return _message; }
        set { SetProperty(ref _message, value); }
    }


    public ViewAViewModel(IContainerProvider containerProvider)
    {
        _containerProvider = containerProvider;
        var appActorSystem = _containerProvider.Resolve<ActorSystem>();

        Message = "test";
    }
}

But it doesn't work...

Could you please explain me how to do it right?

1
Did you try to inject the view model with an ActorSystem instead of an IContainerProvider? - mm8

1 Answers

1
votes

Inject the dependency, not the container:

public ViewAViewModel(ActorSystem appActorSystem)
{
    Message = "test";
}

It does not matter in which module a service is registered and where it is resolved, as long as it's resolved after being registered.