Unity - Prism IoC problem

Oct 25, 2014 at 12:54 PM
Edited Oct 25, 2014 at 12:55 PM
Hallo,

i am trying to modular applications with prism & unity.

i created a Shell an 4 modules.
  1. ServiceModule
  2. ToolBarModule
  3. StatusbarModule
  4. ContentModule
my ContenModule has a dependency to the ServiceModule:
[Module(ModuleName = "ContentModule")]
[ModuleDependency("ServiceModule")]
public class ContentModule : IModule
{
}
in my ServiceModule i Register a service in the UnityContainer:
public ServiceModule(IRegionManager regionManager, IModuleCatalog catalog, IUnityContainer container)
        {
            this.regionManager = regionManager;
            this.catalog = catalog;
            this.container = container;
            // register Services
            var service = new MockServices.ArtikelDataMockService();
            this.container.RegisterInstance<Contracts.interfaces.IArtikelDataService>(service);
}
now i want to inject the service in my ContenModule.ContentViewModel:
public class ContentViewModel
{
        private IArtikelDataService dataService;
        public ContentViewModel(IArtikelDataService artikelDataService)
        {
            dataService = artikelDataService;
        }
    }
but then i the viewmodelLocator throws an XmlParse exception.

can anybody helps me ?


thx
Jens
Oct 27, 2014 at 3:50 PM
I'm not really a Prism expert but if Unity were having an issue resolving I would expect an exception (e.g. ResolutionFailedException) thrown (unless Prism is masking that exception?) Perhaps the Prism forum or Stack Overflow would be a better place for you question?

However, if you upload a sample project (e.g. OneDrive, etc.) that reproduces the issue, I can take a look for you.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Oct 28, 2014 at 2:37 PM
Hallo,

here you can find my sample project:
PrismUnityWpf

the error is thrown, when i want to inject the DataService in my ViewModel

thx
Oct 30, 2014 at 6:39 AM
Edited Oct 30, 2014 at 6:40 AM
The root problem is that there is a MissingMethodException: "No parameterless constructor defined for this object." being thrown.

What is happening is that when ViewModelLocator.AutoWireViewModel="True" is set the ViewModelLocator will try to use the default constructor (see Prism for Windows Store: Views, ViewModels and navigation for more information about that). However, the ContentViewModel does not have a default constructor defined. It has the following defined:
// Constructor that throws an Exception
public ContentViewModel(Infrastructure.IDataService dataService)
{
    Debugger.Break();
}

If you add a default constructor then the view model can be instantiated and no exception is thrown. However, that is not a good solution since you obviously want to inject an IDataService. As mentioned in the article above, you can tell ViewModelLocator which constructor to use with the ViewModelLocationProvider.Register() method.

My solution was to set this up in the Bootstrapper since I can use the container to resolve the view model and have the object Built Up by Unity:
        protected override void ConfigureContainer()
        {
            ViewModelLocationProvider.Register(typeof(Content).ToString(),
                () => Container.Resolve<ContentViewModel>());
            
            base.ConfigureContainer();
        }

Once I did that, the correct constructor was called with the dependency injected. Now, as I mentioned, I'm not a Prism expert so I'm not sure if that is the preferred approach but it does work.


~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 11/3/2014 at 7:37 AM
Nov 3, 2014 at 8:52 AM
Thanks,


this works for me.


Jens