ฉันมีแอปพลิเคชัน 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];
ข้อผิดพลาด:
ข้อมูลเพิ่มเติม: การอ้างอิงวัตถุไม่ได้ตั้งค่าเป็นอินสแตนซ์ของวัตถุ
ฉันจะใช้บริการในชั้นเรียนได้อย่างไร ขอบคุณ!