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?
InjectionPropertyconfig on the registration. Alternatively, why not try constructor injection instead?