I use Unity with Unity.MVC5. The class that registers the types and the dependency resolver is as below:
我将Unity与Unity.MVC5一起使用。注册类型和依赖项解析程序的类如下:
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:
这是我的Global.asax代码:
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?
我希望这个属性可以由Unity自动设置,但它始终为null。有人能帮我弄清楚我做错了什么吗?
1 个解决方案
#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
谢谢你的帮助。我删除了Unity.MVC5并安装了Unity plus Unity.Bootstrapper。现在一切正常。我按照这篇文章解决了这个问题:http://msdn.microsoft.com/en-us/library/dn178463(v = pandp.30).aspx
#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
谢谢你的帮助。我删除了Unity.MVC5并安装了Unity plus Unity.Bootstrapper。现在一切正常。我按照这篇文章解决了这个问题:http://msdn.microsoft.com/en-us/library/dn178463(v = pandp.30).aspx