IoC.Get<Type>() ?

Dec 14, 2010 at 5:07 PM

I am attempting to use MEF with my global application settings class that I created.

I want a singleton instance of my ApplicationSettings class. So in the bootstrapper is where i reference it for the first time, and I am doing this:

private ApplicationSettings _appSettings = IoC.Get<ApplicationSettings>();

I get Object Reference not set to an instance of an object error with that line. Shouldn't that create a new instance of the object if it doesn't already exists? This has been my experience when using that with ViewModels.

I'd like to reference the instance in other areas of my app like this:

private ApplicationSettings _appSettings;



Dec 14, 2010 at 5:48 PM

If you are calling this in the Config overrride, the IoC functions are not yet available. At this point in the application lifecycle, you should be working directly with your container.

Dec 14, 2010 at 5:59 PM

I moved the call to my Bootstrapper constructor. Should that work there? Now it says:

Could not locate any instances of contract Infrastructure.ApplicationSettings

Dec 14, 2010 at 6:16 PM

Are you exporting the type?

Dec 14, 2010 at 6:31 PM


    public class ApplicationSettings

Dec 14, 2010 at 6:39 PM

ApplicationSettings is in a separate project called Infrastructure that I am referencing in my main app. Would this be a problem?

Dec 14, 2010 at 6:48 PM

Is that assembly being added to a catalog?

Dec 14, 2010 at 6:48 PM
Edited Dec 14, 2010 at 6:49 PM

Is your catalog setup properly? I mean, are you sure that the IoC is able to locate the export (i.e you created a proper catalog for the external assembly or you added the Infrastructure Assembly to the AssemblySource.Instance collection)?

Edit: Ops, I was late...

Dec 14, 2010 at 7:15 PM

I added it to the AssmeblySource.Instance collection and it now works. Thanks very much.