【发布时间】:2016-08-17 09:24:04
【问题描述】:
我有应用程序 MVC 使用 Unity Ioc。
声明和初始化服务:
public static void Initialize()
{
IUnityContainer container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
private static IUnityContainer BuildUnityContainer()
{
IUnityContainer container = new UnityContainer();
container.RegisterType<ImyService, myService>(new HttpContextLifetimeManager<ImyService>());;
return container;
}
在课堂上我使用下面的代码:
var service = DependencyResolver.Current.GetService<ImyService>();
这是文件 UnityControllerFactory.cs
public override object GetValue()
{
var assemblyQualifiedName = typeof(T).AssemblyQualifiedName;
if (assemblyQualifiedName != null)
return HttpContext.Current.Items[assemblyQualifiedName];
return null;
}
当我运行应用程序时,它在以下位置返回错误:HttpContext.Current.Items[assemblyQualifiedName];
错误:
附加信息:对象引用未设置为 对象。
我如何在课堂上使用服务。谢谢!
【问题讨论】:
-
如果将服务注入到类构造函数中会不会发生同样的事情?
-
@JB06 当我在控制器中使用时,它可以工作并且没有任何错误。只能在课堂上使用,上面会报错
标签: asp.net-mvc unity-container ioc-container