Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity does not resolve the dependency

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?

like image 765
Aref Avatar asked Mar 06 '14 07:03

Aref


People also ask

How do I force resolve in Unity?

In the Open Unity project, navigate to Assets dropdown menu and choose Play services resolver > Android Resolve. Select the Resolve or Force Resolve.

What is dependencyResolutionManagement?

The dependencyResolutionManagement repositories block accepts the same notations as in a project, which includes Maven or Ivy repositories, with or without credentials, etc. By default, repositories declared by a project will override whatever is declared in settings.

Where is manifest JSON located Unity?

You can find the project manifest file, called manifest. json , in the Packages folder under the root folder of your Unity project.


1 Answers

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

like image 154
Aref Avatar answered Oct 02 '22 07:10

Aref