Are*_*ref 5 asp.net-mvc dependency-injection unity-container
我使用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));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Global.asax代码:
void Application_Start(object sender, EventArgs e)
{
UnityConfig.RegisterComponents();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Run Code Online (Sandbox Code Playgroud)
在我的一个控制器中,我有一个这样的属性:
[Dependency]
public IEventBusiness EventBusiness { get; set; }
Run Code Online (Sandbox Code Playgroud)
我希望Unity自动设置此属性,但它始终为null.有人能帮助我弄清楚我做错了什么吗?
谢谢你的帮助.我删除了Unity.MVC5并安装了Unity plus Unity.Bootstrapper.现在一切正常.我按照这篇文章解决了这个问题:http://msdn.microsoft.com/en-us/library/dn178463(v = pandp.30).aspx