ModuleTypeLoadingException was unhandled by user code

Topics: Prism v4 - Silverlight 4
Mar 17, 2011 at 8:09 PM
Good morning,

I am trying to build a modular application in Silverlight 4 with Prism (MEF) but I have this error :

Failed to load type for module ModuleC. his error occurred when using MEF in a Silverlight application, please ensure that the CopyLocal property of the reference to the MefExtensions assembly is set to true in the main application/shell and false in all other assemblies. 
Inner exception :
{System.Net.WebException: An exception occurred during a WebClient request. ---> System.NotSupportedException: The URI prefix is not recognized.
   at System.Net.WebRequest.Create(Uri requestUri)
   at System.Net.WebClient.GetWebRequest(Uri address)
   at System.Net.WebClient.OpenReadAsync(Uri address, Object userToken)
   --- End of inner exception stack trace ---}
I always go in the : 
 
private void IModuleTypeLoader_LoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e)
 
My CopyLocal property is to true in my shell project.
Here is my bootStrapper :
public class BootStrapper : MefBootstrapper
    {
        private const string _moduleCatalogUri = "/Learning.Prism;component/ModulesCatalog.xaml";

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();

            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));
        }

        protected override System.Windows.DependencyObject CreateShell()
        {
            return this.Container.GetExportedValue<Shell>();
        }

        protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
        {
            return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri(_moduleCatalogUri, UriKind.Relative));
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            Application.Current.RootVisual = (UIElement)this.Shell;
        }
    }
My xaml ModulesCatalog.xaml :
 
<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:ModuleInfoGroup Ref="Learning.Prism.ModuleA.xap" InitializationMode="WhenAvailable">
        <Modularity:ModuleInfo ModuleName="ModuleA"/>
    </Modularity:ModuleInfoGroup>
    <Modularity:ModuleInfoGroup Ref="Learning.Prism.ModuleB.xap" InitializationMode="WhenAvailable">
        <Modularity:ModuleInfo ModuleName="ModuleB"/>
    </Modularity:ModuleInfoGroup>
    <Modularity:ModuleInfoGroup Ref="Learning.Prism.ModuleC.xap" InitializationMode="WhenAvailable">
        <Modularity:ModuleInfo ModuleName="ModuleC"/>
    </Modularity:ModuleInfoGroup>
</Modularity:ModuleCatalog>
 
My shell
 
[Export]
    public partial class Shell : UserControl, IPartImportsSatisfiedNotification
    {
        private const string _moduleAName = "ModuleA";
        private static Uri _moduleAViewUri = new Uri("/ModuleAView", UriKind.Relative);


        [Import(AllowRecomposition = false)]
         public IModuleManager ModuleManager;

        [Import(AllowRecomposition = false)]
        public IRegionManager RegionManager;

        public Shell()
        {
            InitializeComponent();
        }

        public void OnImportsSatisfied()
        {
            this.ModuleManager.LoadModuleCompleted += 
                (s, e) =>
                {
                    if (e.ModuleInfo.ModuleName == _moduleAName)
                        this.RegionManager.RequestNavigate(RegionNames.MainContentRegion, _moduleAViewUri);
                };
        }
    }
My module C : 
[ModuleExport(typeof(ModuleC))]
    public class ModuleC : IModule
    {
        public void Initialize()
        {
        }
    }
 
 Thank you for your help !
Regards,
S
Mar 18, 2011 at 3:33 PM

Hi,

The exception you are experiencing might be caused by the way you are declaring the ModuleInfo´s Ref property in you ModulesCatalog.xaml file.

The correct way to indicate the name of the module to load in the Ref property is to put the name of your xap file, without any additional information such as the namespace. For example, in your case, you should put just ModuleA.xap. In your ModulesCatalog.xaml could look like this:

<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:ModuleInfoGroup Ref="ModuleA.xap" InitializationMode="WhenAvailable">
       <Modularity:ModuleInfo ModuleName="ModuleA"/>
   </Modularity:ModuleInfoGroup>
   <Modularity:ModuleInfoGroup Ref="ModuleB.xap" InitializationMode="WhenAvailable">
       <Modularity:ModuleInfo ModuleName="ModuleB"/>
   </Modularity:ModuleInfoGroup>
   <Modularity:ModuleInfoGroup Ref="ModuleC.xap" InitializationMode="WhenAvailable">
        <Modularity:ModuleInfo ModuleName="ModuleC"/>
   </Modularity:ModuleInfoGroup>
</Modularity:ModuleCatalog>

Setting the complete namespace into the Ref property will cause an error, such as the one you’ve experimented.

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Mar 20, 2011 at 6:45 PM

Thank you for your help.

I have tried what you said but I still have the same error.

If you want you can download my VS Solution : here ! I will really appreciate your help because I would like to create a post for my blog on how to create a modular application with Prism ;)

(RAR File is around 4mo because I am using the PRISM project instead of DLL)

Thank you,

S

 

Mar 22, 2011 at 6:55 PM

Hi,

After examining your solution, we’ve found that a possible cause for the issue you are experiencing could be that the XAP files you’re trying to load in the ModulesCatalog are not present in the Web Project’s ClientBin folder.
You can read this thread useful, in which there is an explanation of how to output a XAP file for every module in your application.

Additionally, you might find the Prism Training Kit handy, specifically Exercise 2 in the Modularity Hands-On Lab. In this HOL you will find the steps you need to complete in order to fulfill your requirement.
You might also find the Modularity QuickStart for Silverlight useful to see an example of modular application development with Prism.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Mar 22, 2011 at 7:33 PM

Thank you Miguel.

I will check the TK to understand the Modularity and solve my problem later.

Thx

 

Mar 30, 2011 at 4:28 PM

i  got the exception the same as monk, and i found the exception caused by webclient base address  internal in Microsoft.Practices.Prism.

so you need check your application  which set the hostting web project as  the startproject