none
Composite WPF IUnityContainer RRS feed

  • Question

  • Hi all. I'm learning modularity in Composite Application Guidance - Oct2009 and i have following problem:
    UnityServiceLocatorAdapter.cs :
    protected override object DoGetInstance(Type serviceType, string key)
    {  return _unityContainer.Resolve(serviceType, key);

    // Error: Resolution of the dependency failed, type = "ShedulerClientGridUI.Views.ClientGridView", name = "". Exception message is: The current build operation (build key Build Key[ShedulerClientGridUI.Views.ClientGridView, null]) failed: The parameter viewModel could not be resolved when attempting to call constructor ShedulerClientGridUI.Views.ClientGridView

    ClientGridView Module:
        public class Module : IModule
        { IRegionManager regionManager;
            public Module(IRegionManager regionManager)
            {
                this.regionManager = regionManager;
            }
            public void Initialize()
            {
                regionManager.RegisterViewWithRegion("ClientGridView", typeof(ClientGridView));
            }
        }

    ClientGridViewModel.cs :
        public class ClientGridViewModel : INotifyPropertyChanged
        {
            private IClientService service;
            private ICommand changeTaskCommand;
            private IEventAggregator eventAggregator;
            public event PropertyChangedEventHandler PropertyChanged;
            public ClientGridViewModel(IClientService service, IEventAggregator eventAggregator)
            {
                this.service = service;
                this.eventAggregator = eventAggregator;
                //...
             }
         }

    Thanks for answers!
    • Edited by kosmich74 Sunday, January 24, 2010 1:44 PM
    Thursday, January 14, 2010 5:40 AM

Answers

  • A couple of things to try.

    First set a break point in your ClientGridViewModel constructor.  Check and see if service has a value.  If you never get to the break point, then I would bet that it is because you have registered ClientService as a ClientService instead of as a IClientService and the container doesnt resolve it correctly. 

    If that is the case, change your client service registration to:

    container.RegisterInstance<IClientService>(new ClientService());

    and see what happens. 

    If that doesnt work, set break points in the module initializations and class constructors, and watch the order of creation.  You have the dependancy set in your config file, so it should be correct.

    Follow the creation process through its steps.  The module first, then the Client Service should get created, set a break point there, then the next module, then the View Model should be created and then the view.  ( ignoring the module creation for the moment. )

    Paul
    • Marked as answer by kosmich74 Friday, January 22, 2010 7:26 AM
    Thursday, January 21, 2010 12:51 PM

All replies

  • need to see "ClientGridView" but from the error, it sounds as if that class has the class "ClientGridView" injected in some fashion.  If that is the case, I dont see where the type "ClientGridView" is registered with th DI container so it can understand how to create one.

    Paul
    Tuesday, January 19, 2010 7:37 PM
  • I have a UserControl ClientGridView:
    public partial class ClientGridView : UserControl
        {
            public ClientGridView(ClientGridViewModel viewModel)
            {
                InitializeComponent();
                this.Loaded += (s, e) =>
                {
                    this.DataContext = viewModel;
                };
            }
        }
    Explain please.
    Wednesday, January 20, 2010 4:19 AM
  • Your ClientGridView is not Registered with UnityContainer.
    Wednesday, January 20, 2010 7:46 AM
  • I have corrected ClientGridView Module as was shown in CodePlex - but nothing changed.
    public class Module : IModule
        {
            private IUnityContainer container;
            private IRegionManager regionManager;

            public Module(IUnityContainer container, IRegionManager regionManager)
            {
                this.regionManager = regionManager;
                this.container = container;
            }

            public void Initialize()
            {
                container.RegisterType<ClientGridView, ClientGridView>();
                container.RegisterType<ClientGridViewModel, ClientGridViewModel>();
                regionManager.RegisterViewWithRegion("ClientGridView", typeof(ClientGridView));
            }
        }
    Error the same: The parameter viewModel could not be resolved when attempting to call constructor ShedulerClientGridUI.Views.ClientGridView      o_O
    Wednesday, January 20, 2010 10:34 AM
  • You need to have an interface defined for the ClientGridView and Implement The ClientGridView from that.
    e.g :
    public partial class ClientGridView : IClientGridView {
    }


    Then Register as follows.

    container.RegisterType<IClientGridView, ClientGridView>();

    Wednesday, January 20, 2010 10:46 AM
  • I can't extract interface for ClientGridView because ClientGridView have only constructor which can't be located in interface.
    I also inspect  http://channel9.msdn.com/posts/mtaulty/Prism--Silverlight-Part-10-A-Larger-Example-Email-Client/  -
    in this example only regions in UserControls were declared.
    Thursday, January 21, 2010 5:43 AM
  • If you are talking of Silverlight, then i am not very sure if i have good knowledge in silverlight. More Over this forum is on WPF. There is a seperate forum on silverlight. You could post your answer there.

    http://forums.silverlight.net/forums/
    Thursday, January 21, 2010 6:03 AM
  • No - i'm using WPF. But technique the same. And Silverlight and WPF examples were described there.
    Thursday, January 21, 2010 6:11 AM
  • How should describe parameter viewModel when calling constructor ShedulerClientGridUI.Views.ClientGridView()  ?
    Thursday, January 21, 2010 11:25 AM
  • If you look at the nested exceptions you shold be able to find out which parameter of ClientGridViewModel's constructor failed to resolve.
    Thursday, January 21, 2010 12:49 PM
  • A couple of things to try.

    First set a break point in your ClientGridViewModel constructor.  Check and see if service has a value.  If you never get to the break point, then I would bet that it is because you have registered ClientService as a ClientService instead of as a IClientService and the container doesnt resolve it correctly. 

    If that is the case, change your client service registration to:

    container.RegisterInstance<IClientService>(new ClientService());

    and see what happens. 

    If that doesnt work, set break points in the module initializations and class constructors, and watch the order of creation.  You have the dependancy set in your config file, so it should be correct.

    Follow the creation process through its steps.  The module first, then the Client Service should get created, set a break point there, then the next module, then the View Model should be created and then the view.  ( ignoring the module creation for the moment. )

    Paul
    • Marked as answer by kosmich74 Friday, January 22, 2010 7:26 AM
    Thursday, January 21, 2010 12:51 PM
  • I'm fool :(  I was confused that service was created and initialized normally in Watch List of Debagger. And IntelliSense doesn't initiate an error when:
    container.RegisterInstance<ClientService>(new ClientService());
    I have corrected on - container.RegisterInstance<IClientService>(new ClientService())   - and all is ok.
    Many thanks!
    Friday, January 22, 2010 7:26 AM