PIAB wrapped object into CAB suddenly causes ModuleLoadException

Topics: Policy Injection Application Block
Aug 8, 2008 at 5:59 AM
Hi!

I use CAB to create a WinForms application. For Access to my middle-tier, I created a couple of interfaces and providers which implements them. In ModuleInit I register these providers by first creating instances of them:
var iProvider = WorkItem.Items.AddNew<Services.InfrastructureProvider>();
var pProvider = WorkItem.Items.AddNew<Services.PatientProvider>();

then I register a PIAB wrapped version by their interface for public consumption:
WorkItem.RootWorkItem.Services.Add(typeof(IInfrastructureProvider), PolicyInjection.Wrap<IInfrastructureProvider>(iProvider));
WorkItem.RootWorkItem.Services.Add(
typeof(IMetadataProvider), PolicyInjection.Wrap<IMetadataProvider>(iProvider));
WorkItem.RootWorkItem.Services.Add(typeof(IPatientProvider), PolicyInjection.Wrap<IPatientProvider>(iProvider));


This worked just fine and still does for all but the IPatientProvider. This line suddenly throws the following exception. If I remove the PIAB wrap from PatientProvider, all is well again.

Microsoft.Practices.CompositeUI.Services.ModuleLoadException was unhandled
  Message="Failed to load module from assembly 6f979a5f-da3e-4997-9cfd-06f35a948681. Error was:\r\nFailed to load module from assembly SmartClient.SynergyDataProvider, Version=4.0.0.0, Culture=neutral, PublicKeyToken=6ce566de05261837. Error was:\r\nObject does not match target type."
  Source="SmartClient.Infrastructure.Library"
  StackTrace:
       at SmartClient.Infrastructure.Library.Services.DependentModuleLoaderService.ModuleMetadata.ThrowModuleLoadException(Exception innerException) in d:\Source\MAIN\CLIENT\Infrastructure.Library\Services\DependentModuleLoaderService.cs:line 385
       at SmartClient.Infrastructure.Library.Services.DependentModuleLoaderService.ModuleMetadata.InitializeModuleClasses(WorkItem workItem) in d:\Source\MAIN\CLIENT\Infrastructure.Library\Services\DependentModuleLoaderService.cs:line 351
       at SmartClient.Infrastructure.Library.Services.DependentModuleLoaderService.InnerLoad(WorkItem workItem, IModuleInfo[] modules) in d:\Source\MAIN\CLIENT\Infrastructure.Library\Services\DependentModuleLoaderService.cs:line 115
       at SmartClient.Infrastructure.Library.Services.DependentModuleLoaderService.Load(WorkItem workItem, IModuleInfo[] modules) in d:\Source\MAIN\CLIENT\Infrastructure.Library\Services\DependentModuleLoaderService.cs:line 73
       at Microsoft.Practices.CompositeUI.CabApplication`1.LoadModules()
       at Microsoft.Practices.CompositeUI.CabApplication`1.Run()
       at SmartClient.Infrastructure.Shell.ShellApplication.RunInDebugMode() in d:\Source\MAIN\CLIENT\Shell\ShellApplication.cs:line 44
       at SmartClient.Infrastructure.Shell.ShellApplication.Main() in d:\Source\MAIN\CLIENT\Shell\ShellApplication.cs:line 36
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Reflection.TargetException
       Message="Object does not match target type."
       Source="mscorlib"
       StackTrace:
            at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
            at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
            at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
            at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
            at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
            at Microsoft.Practices.ObjectBuilder.PropertySetterStrategy.InjectProperties(IBuilderContext context, Object obj, String id)
            at Microsoft.Practices.ObjectBuilder.PropertySetterStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.CreationStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.ReflectionStrategy`1.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.ReflectionStrategy`1.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.ReflectionStrategy`1.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.SingletonStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.TypeMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
            at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
            at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
            at Microsoft.Practices.CompositeUI.Collections.ServiceCollection.BuildFirstTimeItem(Type typeToBuild, Type typeToRegisterAs, Object item)
            at Microsoft.Practices.CompositeUI.Collections.ServiceCollection.Build(Type typeToBuild, Type typeToRegisterAs, Object serviceInstance)
            at Microsoft.Practices.CompositeUI.Collections.ServiceCollection.Add(Type serviceType, Object serviceInstance)
            at SmartClient.SynergyDataProvider.ModuleController.AddServices() in d:\Source\MAIN\CLIENT\Modules\SynergyDataProvider\ModuleController.cs:line 42
            at SmartClient.SynergyDataProvider.ModuleController.Run() in d:\Source\MAIN\CLIENT\Modules\SynergyDataProvider\ModuleController.cs:line 25
            at SmartClient.SynergyDataProvider.Module.Load() in d:\Source\MAIN\CLIENT\Modules\SynergyDataProvider\Module.cs:line 36
            at SmartClient.Infrastructure.Library.Services.DependentModuleLoaderService.ModuleMetadata.InitializeModuleClasses(WorkItem workItem) in d:\Source\MAIN\CLIENT\Infrastructure.Library\Services\DependentModuleLoaderService.cs:line 344
       InnerException:
Aug 8, 2008 at 12:26 PM

I found a solution/workaround and posted it here: http://utillity.blogspot.com/2008/08/moduleloadexception-object-does-not.html

Regards, Tilli