Dependency module not getting loaded

Topics: Prism v4 - Silverlight 4
Jan 21, 2012 at 9:55 AM

Hi

I have two modules in my App (Admin and Finance). Both modules are loaded on demand.

The finance module load depends on admin module. Hence when Finance module gets loaded the admin module should also be loaded.

I have specified this dependency in modulecatalog.xaml file. Now when i try to load the Finance module I am getting Service Activation Exception Error

({Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type Object, key "QSys.Finance.Views.FinanceHomeView"   at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key)   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)})

but if i remove the dependency of the module. Everything works fine. Below is the xaml markup for module catalog

<Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                          xmlns:sys="clr-namespace:System;assembly=mscorlib" 
                          xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">

    <Modularity:ModuleInfo 
            Ref="QSys.Admin.xap"
            InitializationMode="OnDemand"
            ModuleName="QSysAdminModule"/>
    <Modularity:ModuleInfo
        Ref="QSys.Finance.xap"
        InitializationMode="OnDemand"
        ModuleName="QSysFinanceModule">
        <Modularity:ModuleInfo.DependsOn>
            <sys:String>QSysAdminModule</sys:String>
        </Modularity:ModuleInfo.DependsOn>
    </Modularity:ModuleInfo>


</Modularity:ModuleCatalog>


Please help
Thanks

Developer
Jan 23, 2012 at 4:10 PM

Hi,

So far, we couldn’t find anything wrong within the code snippets that you posted, it would be helpful if you could provide us with a repro sample application, so that we can help you further with this.

Thanks,

Agustin Adami
http://blogs.southworks.net/aadami


Jan 24, 2012 at 6:40 AM

Hi Agustin

Thanks for your reply

You can download the sample from https://skydrive.live.com/embed?cid=E84CFB62692563C4&resid=E84CFB62692563C4!305&authkey=AHVTD7Hgf1P9mT4

 

Please note the example has dependency of database.

You need to create an empty database called QSys and then run the script file called script.sql (This is also there in zip file).

Please change the connectionstrings in web.config file

Login with afzal/afzal@1 credentials.

To stimulate the error follow these steps

1) Click on the admin module, it will load

2) Now click on the Finance module and you would get an error.

 

I assume it has some dependency injection problems for the IAdminService. I tried to figure it but could not find the viable solution to it.

 

Thanks again for your help.

 

Regards

 

Developer
Jan 25, 2012 at 5:39 PM
Edited Jan 25, 2012 at 5:54 PM

Hi,

We have download your application and we could reproduce the issue you are mentioning. After reviewing the application for a while, we found that this problem stopped appearing after applying the following configurations:

  • In the project QSys.App set the references to Microsoft.Practices.Prism.MefExtensions and QSys.Library to Copy Local = True.
  • In the project QSys.Finance set the references to Microsoft.Practices.Prism.MefExtensions, QSys.Library and QSys.Services to Copy Local = False.
  • In the project QSys.Admin set the references to Microsoft.Practices.Prism.MefExtensions and QSys.Library to Copy Local = False, and the reference to QSys.Services to Copy Local = True.
  • In the project QSys.Library set the references to Microsoft.Practices.Prism.MefExtensions to Copy Local = False.

Also, just in case, you might need to clean the solution and rebuild it after applying these modifications.

I believe that this problem might be related to a class being exported in the container more than one time: based on my understanding, when using MEF, if you have a project that exports types into the container, this project should be referenced with Copy Local = True in only one project, and with Copy Local = False in the rest. This is the case, for exampling, when using the Microsoft.Practices.Prism.MefExtensions.

It seems that, as the QSys.Services project was referenced with Copy Local = True in more than one project the IAdminService was exported more than once. As you can see in the configuration mentioned above, we tried to avoid this by making each library to be referenced with the Copy Local property set to True only once.

Also, after applying these configurations, we could reproduce the first issue you mentioned (the one regarding the FinanceHomeView). Based on my understanding this could be related to a know issue with MEF where if, for example, a Module A has a dependency to a Module B and both use a library, if the Module A has this library in Copy Local = False and the Module B has it in Copy Local = True an error appears because the library will not be available when the Module A requires it. You can find more information about this issue in the following work item:

As a possible work around for this, you could move the reference to the QSys.Services project to another location, for example the QSys.Library or the QSys.App project.

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Jan 26, 2012 at 6:02 AM

Hello

Thanks for your reply. I tried out the steps mentioned above but the problem still persists. I am getting the error when I click on Finance link

{Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type Object, key "QSys.Finance.Views.FinanceHomeView" ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type Object, key "QSys.Finance.Views.FinanceHomeView"
   at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key)
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService](String key)
   at Microsoft.Practices.Prism.Regions.RegionNavigationContentLoader.CreateNewRegionItem(String candidateTargetContract)}

 

I have uploaded the solution again for your reference once again (https://skydrive.live.com/embed?cid=E84CFB62692563C4&resid=E84CFB62692563C4%21308&authkey=ALD49_zkD-4Yo7o)

Feel free to change the code or references to make it work.

If you find the solution i would request you to please upload it to some public location from where I can download it.

 

Thanks once again for your help. I really appreciate.

Developer
Jan 26, 2012 at 7:17 PM

Hi,

The steps we described above were targeting to solve the error you mentioned when following the instructions of your second post (clicking Admin module first, and the clicking Finance module), which was related to a composition error.

Regarding the exception you mentioned in the first post, after reviewing your application again, we found that you are using silverlight frame navigation in prism with modules being loaded on demand. Based on my understanding, there were several users who reported to experience similar problems in related scenarios, as it seems that this is not supported from out of the box. Also, it can be seen that, regardless of the aforementioned exception, the view is still being loaded and shown in the region.

Therefore, I believe you might find the following threads useful where similar scenarios regarding silverlight frame navigation in prism with modules being loaded on demand (with possible workarounds) are being discussed:

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini