У меня есть приложение 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];
Ошибка:
Дополнительная информация: Ссылка на объект не указывает на экземпляр объекта.
Как я могу использовать сервис в своем классе. Спасибо!