canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.
MIT License
988 stars 143 forks source link

Possible omission from Autofac bootstrapper #158

Closed LarryThermo closed 3 years ago

LarryThermo commented 3 years ago

First, of all thank you for the well written project and software.

I prefer to use autofac because that is what I am familiar with. Everything has worked well up unto today when I attempted to show a message box with

var result = _windowManager.ShowMessageBox("Hello");

where _windowManager is passed in through ioc.

I get the following exception: {"The requested service 'Stylet.MessageBoxView' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency."}

I added the following line to AutofacBootstrapper.DefaultConfigureIoC : builder.RegisterType();

so that DefaultConfigureIoC now looks like:

        protected virtual void DefaultConfigureIoC(ContainerBuilder builder)
        {
            var viewManagerConfig = new ViewManagerConfig()
            {
                ViewFactory = this.GetInstance,
                ViewAssemblies = new List<Assembly>() { this.GetType().Assembly }
            };
            builder.RegisterInstance<IViewManager>(new ViewManager(viewManagerConfig));
            builder.RegisterType<MessageBoxView>(); // added

            builder.RegisterInstance<IWindowManagerConfig>(this).ExternallyOwned();
            builder.RegisterType<WindowManager>().As<IWindowManager>().SingleInstance();
            builder.RegisterType<EventAggregator>().As<IEventAggregator>().SingleInstance();
            builder.RegisterType<MessageBoxViewModel>().As<IMessageBoxViewModel>().ExternallyOwned(); // Not singleton!
            builder.RegisterAssemblyTypes(this.GetType().Assembly).ExternallyOwned();
        }

and the message box now appears to work correctly.

Could you please confirm this is a good fix?

Lars

canton7 commented 3 years ago

I think that's the right thing to do: that matches Stylet's bootstrapper. I'll update the Autofac sample, thanks!

canton7 commented 3 years ago

Fixed in 1.3.5