Ian Warburton
Ian Warburton

Reputation: 15676

Activation error occured while trying to get instance of type

I'm using Unity with an Asp.net MVC 3 app. Here is some code running in Application_Start...

        UnityContainer container = new UnityContainer();

        new UnityMappings(container).RegisterTypes();

        DependencyResolver.SetResolver(new UnityServiceLocator(container));

The controllers are registered with the UnityMappings instance like so...

IEnumerable<Type> controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                            where typeof (IController).IsAssignableFrom(t)
                                            select t;

    foreach (Type t in controllerTypes)
    {
        container.RegisterType(t);
    }

When I request a page I get the following errors... (The last one is specific to the view being requested.)

Activation error occured while trying to get instance of type IControllerFactory, key ""

Activation error occured while trying to get instance of type IControllerActivator, key ""

Activation error occured while trying to get instance of type IViewPageActivator, key ""

Activation error occured while trying to get instance of type ModelMetadataProvider, key ""

And then strangely, I can click through all the exceptions and the page works absolutely fine! All the other dependencies are resolved just fine.

Its not a Visual Studio issue because it does it in different instances from different machines. I've had to turn off breaking on all exceptions so that I can get anything done.

Any ideas?

Upvotes: 1

Views: 4956

Answers (2)

Ian Warburton
Ian Warburton

Reputation: 15676

This error seems to have gone away when I use this...

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

... where UnityDependencyResolver is defined as...

public class UnityDependencyResolver : IDependencyResolver
{
    private readonly IUnityContainer container;

    public UnityDependencyResolver(IUnityContainer container)
    {
        this.container = container;
    }

    #region IDependencyResolver Members

    public object GetService(Type serviceType)
    {
        try
        {
            return container.Resolve(serviceType);
        }
        catch
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return container.ResolveAll(serviceType);
        }
        catch
        {
            return new List<object>();
        }
    }

    #endregion
}

Before I was using 'UnityServiceLocator' which is defined in 'Microsoft.Practices.Unity' like this...

DependencyResolver.SetResolver(new UnityServiceLocator(container));

Is 'UnityServiceLocator' not supposed to be used in this way?

Upvotes: 2

Aleks
Aleks

Reputation: 1689

I had similar injection problems using Unity with an MVC3 site that utilizes compiled views.

It turned out registering things in Application_Start meant they happened too late in the application start-up process.

I used the PreApplicationStartMethod attribute as explained here to run all the code for registering types in a separate static method that runs before the Application_Start event.

I wasn't getting exactly the same errors, but it was in the same area. It shouldn't take too much effort to test, so it might be worth a try.

EDIT (because the comment field is too small)

OK. In my experience the excluding an avenue of investigation because of an assumption usually leads to me wasting a lot of time. Granted you should always try the simplest or most obvious explanations first, however you seem to have already done that.

The code itself may be fine, it may just be that the injection request is being called on earlier then you would expect from your other experience because of some other subtle difference in your application.

In my case, the same code had worked just fine in another application but was failing in this one because I was using a compiled view... something I thought at the time was completely irrelevant.

It could one of many things. All the interfaces you listed are related to the System.Web.MVC namespace, so it may be an issue with finding the (correct) assembly. If as you assert it’s not the code, the fact that it occurs in this app only but on multiple machines suggests it may be an issue in one of the web.config files, possibly to do with assembly references. The exception thrown would provide more info, in particular the InnerException should give more detail on what actually caused the activation error. Failing that you could use the Fusion log viewer to drill into assembly loading.

Without knowing more about the application or error itself, it’s probably going to be difficult to offer a more concise suggestion.

Upvotes: 0

Related Questions