First Application in PRISM4 SL4

Topics: Prism v4 - Silverlight 4
Jan 25, 2011 at 1:06 PM

Dear All,

I have reched PRISM after a lot of reading amd decided to use it as a framework to develop my new Silverlight 4 application.

I layout my project into 5 different projects, then I define the module catalog as follows:

the cataloge xaml file :- 

<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="GL.Modules.AVT.xap" InitializationMode="WhenAvailable">
		<Modularity:ModuleInfo ModuleName="AVTModule"/>
	</Modularity:ModuleInfoGroup>

</Modularity:ModuleCatalog>
Then, I build the AVTModel class into the AVT project as follows:
namespace GL.Modules.AVT
{
	[ModuleExport(typeof(AVTModule))]
	public class AVTModule : IModule
	{
		[Import]
		public IRegionManager RegionManager;

		public void Initialize()
		{
			RegionManager.RegisterViewWithRegion(RegionNames.MainNavigationRegion, 
						typeof(Views.CurrencyNavigationItemView));
		}
	}
}
And also defined the bootstrapper class as follows:
	public class GlBootStrapper : MefBootstrapper
	{
       private const string ModuleCatalogUri = "/GL;component/ModulesCatalog.xaml";

		protected override DependencyObject CreateShell()
		{
			return this.Container.GetExportedValue<Shell>();
		}
		protected override void InitializeShell()
		{
			base.InitializeShell();
			Application.Current.RootVisual = (UIElement)this.Shell;
		}
		protected override IModuleCatalog CreateModuleCatalog()
		{
			return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(
																				new Uri(ModuleCatalogUri, UriKind.Relative));
		}
		protected override void ConfigureAggregateCatalog()
		{
			base.ConfigureAggregateCatalog();

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

	}
When I run the application I got an exception as follows:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.6; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
Timestamp: Tue, 25 Jan 2011 12:24:34 UTC


Message: Unhandled Error in Silverlight Application
Code: 4004   
Category: ManagedRuntimeError      
Message: Microsoft.Practices.Prism.Modularity.ModuleTypeLoadingException: Failed to load type for module AVTModule.

If this 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.

Error was: The package downloaded successfully but an error occurred while reading the contents of the package. See the inner exception for more details.. ---> System.InvalidOperationException: The package downloaded successfully but an error occurred while reading the contents of the package. See the inner exception for more details. ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at System.ComponentModel.Composition.Hosting.AssemblyCatalog.get_InnerCatalog()
   at System.ComponentModel.Composition.Hosting.AssemblyCatalog.get_Parts()
   at System.ComponentModel.Composition.Hosting.DeploymentCatalog.DiscoverParts(IEnumerable`1 assemblies)
   at System.ComponentModel.Composition.Hosting.DeploymentCatalog.HandleOpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   at Microsoft.Practices.Prism.Modularity.ModuleManager.HandleModuleTypeLoadingError(ModuleInfo moduleInfo, Exception exception)
   at Microsoft.Practices.Prism.Modularity.ModuleManager.IModuleTypeLoader_LoadModuleCompleted(Object sender, LoadModuleCompletedEventArgs e)
   at Microsoft.Practices.Prism.MefExtensions.Modularity.MefXapModuleTypeLoader.RaiseLoadModuleCompleted(LoadModuleCompletedEventArgs e)
   at Microsoft.Practices.Prism.MefExtensions.Modularity.MefXapModuleTypeLoader.RaiseLoadModuleCompleted(ModuleInfo moduleInfo, Exception error)
   at Microsoft.Practices.Prism.MefExtensions.Modularity.MefXapModuleTypeLoader.HandleDownloadCompleted(DeploymentCatalog deploymentCatalog, AsyncCompletedEventArgs e)
   at Microsoft.Practices.Prism.MefExtensions.Modularity.MefXapModuleTypeLoader.DeploymentCatalog_DownloadCompleted(Object sender, AsyncCompletedEventArgs e)
   at System.ComponentModel.Composition.Hosting.DeploymentCatalog.OnDownloadCompleted(AsyncCompletedEventArgs e)
   at System.ComponentModel.Composition.Hosting.DeploymentCatalog.HandleOpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OpenReadOperationCompleted(Object arg)    

Line: 56
Char: 13
Code: 0
URI: http://localhost:4692/GLTestPage.aspx

 

I already checked the Mef assembly copy local is false in the other project !!

 

What could be the reason of this error ..

Thanks and best regards

Waleed

Developer
Jan 25, 2011 at 2:48 PM
Edited Jan 25, 2011 at 9:40 PM

Hi Waleed,

The exception you're experiencing might be because you're setting the Region Manager as an import, and using it in the Initialize method, where it might have not been resolved.

One possible workaround for that would be to make use of constructor injection, so as to make sure that the import of type IRegionManager has been resolved at the time you call the RegisterViewWithRegion method in your module's Initialize method. To illustrate this:

 

    [ModuleExport(typeof(AVTModule))]
    public class AVTModule : IModule
    {
        private IRegionManager regionManager;

        [ImportingConstructor]
public AVTModule(IRegionManager regionManager) { this.regionManager = regionManager; } public void Initialize() { RegionManager.RegisterViewWithRegion(RegionNames.MainNavigationRegion, typeof(Views.CurrencyNavigationItemView)); } }

 

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 25, 2011 at 9:16 PM

Hello Guido,

Appreciate your reply indeed ... I understand that passing the regionmanager in the constructor is NOT MEF .. I am not sure if this is true....

I try your solution but still not working ... still getting the same exception :(

Regards

Developer
Jan 25, 2011 at 9:42 PM
Edited Jan 25, 2011 at 9:43 PM

Hi Waleed,

Constructor injection is supported by MEF. In order to achieve it, you must mark your constructor with the ImportingConstructor attribute. You can read more about it in this page from the Mef Programming Guide.

I've modified my previous response to reflect that, since I had missed to include that in the code I had written. Sorry for the inconveniences.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 26, 2011 at 11:45 AM

Hello Guido,

Here is my current code

	[ModuleExport(typeof(AVTModule))]
	public class AVTModule : IModule
	{
		//[Import]
		public IRegionManager RegionManager;

		[ImportingConstructor]
		public AVTModule(IRegionManager regionManager)
		{
			this.RegionManager = regionManager;
		}

		public void Initialize()
		{
			RegionManager.RegisterViewWithRegion(RegionNames.MainNavigationRegion, 
			typeof(Views.CurrencyNavigationItemView));
		}
	}

Still not working .... some sort of hidden stuff needed ... I guess.

Regards

Jan 26, 2011 at 12:02 PM

Important to notice that the debugger doesn't hit this class at all .... If I insert a breakpoint in the first line ... nothing is hit.

WHY ???

 

Jan 26, 2011 at 12:57 PM

Dear Guido,

I just figure it out ... None of the PRISM dll's should be copy local , all should be set to FALSE.

It just worked !!

Regards,

Waleed

Developer
Jan 26, 2011 at 2:42 PM

Hi Waleed,

I'm glad that you've found a solution to your problem. Thank you for sharing this with the rest of the community, as other users facing similar problems might benefit from this.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi