5

I use Unity with Unity.MVC5. The class that registers the types and the dependency resolver is as below:

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        container
            .RegisterType<ILogger, Nlogger>()
            .RegisterType<IDataAccessLayer, SqlDataAccessLayer>()
            .RegisterType<IEventBusiness, EventBusiness>();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));

    }
}

And here is my Global.asax code:

void Application_Start(object sender, EventArgs e)
{
    UnityConfig.RegisterComponents();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    RouteConfig.RegisterRoutes(RouteTable.Routes);  
}

In one of my controllers I have a property like this:

[Dependency]
public IEventBusiness EventBusiness { get; set; }

I expect that this property be set automatically by Unity but it's always null. Can someone help me to figure out what I am doing wrong?

3
  • You might also try supplying an InjectionProperty config on the registration. Alternatively, why not try constructor injection instead? Commented Mar 6, 2014 at 8:14
  • Are you registering your controllers into the container? Commented Mar 6, 2014 at 21:12
  • @TylerOhlsen, no I don't register the controllers. Do I have to? Inside the controller when I check DependencyResolver.Current, it gives me the correct instance of UnityContainer with all the registered types. Commented Mar 6, 2014 at 22:16

1 Answer 1

5

Thanks for your help guys. I removed Unity.MVC5 and installed Unity plus Unity.Bootstrapper instead. Now everything works fine. I followed this article to resolve the issue: http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.