Derik Whittaker



Null Reference from Simple Container in Caliburn Micro

I am building out a new Windows Phone 7 application and this time I am finally going to use the Caliburn Micro framework (I have been telling Rob I was going to for a long, long time).  Of course one of the great features of this framework is that it was built from the beginning to support DI (Dependency Injection) with its own built in, light DI container. 

When setting up the container you have the ability to provide keys for the items which you register.  However, one thing I was not aware of (it may be documented someplace) is that not all items in the container can/should have keys.  I have a ViewModel which I have registered using the standard way (seen below)

container.RegisterPerRequest( typeof( DashboardViewModel ), "DashboardViewModel", typeof( DashboardViewModel ) );

and this View Model needed a dependency injected into it (an IDashboardService).  In order to inject this IDashboardService I created the line below:

container.RegisterPerRequest( typeof( IDashboardService ), "MlbDashboardService", typeof( MlbDashboardService ) );

However when I ran the application I received a NullReferenceException inside SimpleContainer.ActivateInstace.  At first I thought I did not have the interface/concretes setup correctly inside the container but after a bit of double checking I was able to verify I did.  After a bit of debugging and walking though the code (not to mention pulling out more of my hair, which by the way I need to keep) I realized that the cause was that I have my IDashboardService a key. After realizing this I changed my registration line to be

container.RegisterPerRequest( typeof( IDashboardService ), null, typeof( MlbDashboardService ) );

Once I made this change and re-ran my application I was able to have my ViewModel created correctly and have the IDashboardService injected as expected.

Now I do not know if this behavior is by intent or what but since I racked my brains trying to figure out what was causing the issues I figured I would share my thoughts just in case others where having the same issue.

Till next time,

Posted 04-10-2011 7:59 AM by Derik Whittaker



Rob Eisenberg wrote re: Null Reference from Simple Container in Caliburn Micro
on 04-10-2011 1:01 PM

Thanks for blogging this. I'll see if I can make some API improvements or add some comments in the samples so that this is more easily avoided. You aren't the first developer who has had that problem, so that tells me I need to make some improvements in that area. I'm planning to RTW tomorrow, so I'll try and get it in this afternoon if I can. Overall, I steel feel that CM shines mostly with WPF and Silverlight and "helps" with WP7. It's not quite where I want it to be and I'm hoping to make big improvements on that in the next version. I'd love to get feedback from you on your experience as you develop your app. Feel free to email me at any time.

Derik Whittaker wrote re: Null Reference from Simple Container in Caliburn Micro
on 04-10-2011 3:04 PM


I will of course let the world know how awesome this framework is as i learn more :)

Rob Eisenberg wrote re: Null Reference from Simple Container in Caliburn Micro
on 04-10-2011 8:16 PM

I made some improvements. When you get a chance, have a look at the WP7 samples. There's some new utility methods that make things a little nicer. If you haven't seen it yet, you should look at the WP7 WindowManager sample anyways. It was a recent addition and makes modal popups in WP7 a snap.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)