Still looking for a sponsor Max Paulousky is looking for a Silverlight/.Net job in the Commonwealth

Share to Facebook Tweet this! Share to MySpace Share to Google Share to Live   Share via AddThis

Modularity in Silverlight Applications - An Issue With ModuleInitializeException

Intro.

Modularity is a great feature of Composite Application Library (Prism).

Modularity is designing a system that is divided into a set of functional units (named modules) that can be composed into a larger application. A module represents a set of related concerns. It can include a collection of related components, such as features, views, or business logic, and pieces of infrastructure, such as services for logging or authenticating users. Modules are independent of one another but can communicate with each other in a loosely coupled fashion.

You use modular design if you want to:

  • develop/design modules independently;
  • load modules from various places;
  • load modules on demand (user opens some area of an application) etc.

But you can encounter with  some unexpected issues during loading a View into a Region.

The Issue.

The typical modular application has an architecture similar to described on the figure below. Shell View (in the host application) has some regions that are bound with Views from dependent modules. Architecture of an application that uses the Composit Application Library (Prism) In the general case, the Shell View and a Dependent View can have different set of controls. For example, Dependent module 1 can have WrapPanel. Last one is implemented in System.Windows.Controls.Toolkit.dll. If the Shell Module does not host any control from System.Windows.Controls.Toolkit.dll, it will have no link to that library. As a result, application will crash at loading Dependent View. Here is the example of the stack trace.

Message: Unhandled Error in Silverlight Application 
Code: 4004    
Category: ManagedRuntimeError       
Message: Microsoft.Practices.Composite.Modularity.ModuleInitializeException: An exception occurred while initializing module 'DemoModule'. 
    - The exception message was: An exception has occurred while trying to add a view to region 'MainRegion'. 
    - The most likely causing exception was was: 'System.Windows.Markup.XamlParseException: AG_E_PARSER_BAD_TYPE [Line: 8 Position: 66]
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at SLErrorRendering.Module.DemoView.InitializeComponent()
   at SLErrorRendering.Module.DemoView..ctor()
   at BuildUp_SLErrorRendering.Module.DemoView(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)'.
    But also check the InnerExceptions for more detail or call .GetRootException(). 
    - The Assembly that the module was trying to be loaded from was:SLErrorRendering.Module, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
    Check the InnerException property of the exception for more information. If the exception occurred while creating an object in a DI container, you can exception.GetRootException() to help locate the root cause of the problem. 
   ---> Microsoft.Practices.Composite.Presentation.Regions.ViewRegistrationException: An exception has occurred while trying to add a view to region 'MainRegion'. 
    - The most likely causing exception was was: 'System.Windows.Markup.XamlParseException: AG_E_PARSER_BAD_TYPE [Line: 8 Position: 66]
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at SLErrorRendering.Module.DemoView.InitializeComponent()
   at SLErrorRendering.Module.DemoView..ctor()
   at BuildUp_SLErrorRendering.Module.DemoView(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)'.
    But also check the InnerExceptions for more detail or call .GetRootException().  
    ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type DemoView, key "" 
    ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "SLErrorRendering.Module.DemoView", name = "". Exception message is: The current build operation (build key Build Key[SLErrorRendering.Module.DemoView, null]) failed: AG_E_PARSER_BAD_TYPE [Line: 8 Position: 66] (Strategy type BuildPlanStrategy, index 3) 
    ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[SLErrorRendering.Module.DemoView, null]) failed: AG_E_PARSER_BAD_TYPE [Line: 8 Position: 66] (Strategy type BuildPlanStrategy, index 3) 
    ---> System.Windows.Markup.XamlParseException: AG_E_PARSER_BAD_TYPE [Line: 8 Position: 66]
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at SLErrorRendering.Module.DemoView.InitializeComponent()
   at SLErrorRendering.Module.DemoView..ctor()
   at BuildUp_SLErrorRendering.Module.DemoView(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)

Solution.

This happens because XAML Parser cannot read and parse xaml code. System.Windows.Controls.Toolkit.dll should be added as a reference to the Host Application to solve that issue .

Pay attention, it is not enough to add a dependent module assembly reference to the Host Application. You should add references to all assemblies that contains implementation of additional controls.

Of course, the easiest way out is just add a reference to the Host Application. But in some cases it is impossible.

For example, when a developer does not know what controls will be used in Dependent Views.

Here is the source code of the application that simulates the issue.

Conclusion.

So, it is clear why such exceptions occur and how fix them. Next time I will describe how required assemblies can be loaded and used dynamically in the described situation.

 

This work is licensed under a Creative Commons Attribution By license.

Leave a Comment [ RSS ]

Comments have been closed on this topic.