StructureMap Bootstrapper?

Jan 30, 2011 at 9:46 AM

Hey guys,

I've got a problem: I'm new to Caliburn Micro and started with the HelloWP7 project from the samples. I wanted to try to exchange the HelloWP7Bootstrapper against a StructureMapBootstrapper with the following code:

    public class StructureMapBootstrapper : PhoneBootstrapper
    {
        IContainer _container;

        protected override void Configure()
        {
            _container = new Container(x =>
            {
                x.For<MainPageViewModel>().Use<MainPageViewModel>();
                x.For<PageTwoViewModel>().Use<PageTwoViewModel>();
                x.For<TabViewModel>().Use<TabViewModel>();

                x.For<INavigationService>().Use(new FrameAdapter(RootFrame));
                x.For<IPhoneService>().Use(new PhoneApplicationServiceAdapter(PhoneService));
            });

            AddCustomConventions();
        }

        private void AddCustomConventions()
        {
            ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) =>
                {
                    ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention);
                    ConventionManager
                        .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
                    ConventionManager
                        .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, viewModelType);
                };

            ConventionManager.AddElementConvention<Panorama>(Panorama.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) =>
                {
                    ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention);
                    ConventionManager
                        .ConfigureSelectedItem(element, Panorama.SelectedItemProperty, viewModelType, path);
                    ConventionManager
                        .ApplyHeaderTemplate(element, Panorama.HeaderTemplateProperty, viewModelType);
                };
        }

        protected override object GetInstance(Type serviceType, string key)
        {
            return _container.GetInstance(serviceType, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return _container.GetAllInstances(serviceType).Cast<object>();
        }

        protected override void BuildUp(object instance)
        {
            _container.BuildUp(instance);
        }
    }

 

In the App.xaml, I've exchanged the bootstrapper resource:

    <Application.Resources>
        <local:StructureMapBootstrapper x:Key="bootstrapper" />
        <!--<local:HelloWP7Bootstrapper x:Key="bootstrapper" />-->
    </Application.Resources>

 

Now I wanted to start the application, but even before I get into the constructor of the base "Bootstrapper" class, I get the following exception in App.g.i.cs (when calling "System.Windows.Application.LoadComponent(this, new System.Uri("/Caliburn.Micro.HelloWP7;component/App.xaml", System.UriKind.Relative));"):

System.InvalidProgramException: InvalidProgramException
   at System.RuntimeType.GetConstructorImplInternal(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers, Boolean verifyAccess)
   at System.RuntimeType.GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
   at System.Type.GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
   at System.Type.GetConstructor(Type[] types)
   at MS.Internal.TypeProxy.GetCreateObjectDelegate(UInt32 customTypeId)
   at MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)
   at MS.Internal.FrameworkCallbacks.CreateKnownObject(IntPtr nativeRootPeer, UInt32 customTypeId, String initializationString, IntPtr& nativePeer, UInt32 isCreatedByParser)
   at MS.Internal.XcpImports.Application_LoadComponentNative(IntPtr pContext, IntPtr pComponent, UInt32 cUriStringLength, String uriString, UInt32 cXamlStrLength, Byte* pXamlStr)
   at MS.Internal.XcpImports.Application_LoadComponent(INativeCoreTypeWrapper componentAsDO, String resourceLocator, UnmanagedMemoryStream stream, UInt32 numBytesToRead)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at Caliburn.Micro.HelloWP7.App.InitializeComponent()
   at Caliburn.Micro.HelloWP7.App..ctor()
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
   at MS.Internal.TypeProxy.<>c__DisplayClass30.b__2a()
   at MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)
   at MS.Internal.FrameworkCallbacks.CreateKnownObject(IntPtr nativeRootPeer, UInt32 customTypeId, String initializationString, IntPtr& nativePeer, UInt32 isCreatedByParser)
   at MS.Internal.FrameworkCallbacks.CreateUnknownObject(String assemblyName, String typeName, IntPtr nativeRootPeer, String initializationString, UInt32& customTypeId, UInt32& coreTypeId, UInt32& typeFlags, IntPtr& nativePeer)
 [Line: 0 Position: 0]
   --- Inner Exception ---
InvalidProgramException

 

I have no clue what I'm doing wrong. I set breakpoints in every method of the StructureMapBootstrapper, but none is hit. Suggestions anyone?

Thanks in advance,
Matthias

Jan 30, 2011 at 10:43 AM

Sorry for the silly question, but I'm not familiar with StructureMap: is it supported on WP7 platform? 

Reading this recent post http://groups.google.com/group/structuremap-users/browse_thread/thread/ab9e7660831076b0, I guess it's not. 

Jan 30, 2011 at 11:01 AM

Meeep, this could indeed be the problem -.-

I've seen some posts on Cb.µ and StructureMap and implicitly that it would work. But I didn't check the compatibility to WP7, narf. I'm just wondering why I didn't get an error when integrating the StructureMap DLL into my WP7 project...

 

Thanks a lot, I will switch to another DI container.

~ Matthias

Jan 30, 2011 at 1:26 PM

The exception is actually pretty obscure :-)