私はアプリケーションMVCでUnity Iocを使用しています。クラスMVCのサービスをUnity Iocで使用する
宣言し、initサービス:クラスで
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;
}
ファイルです、次のところでエラーを返します。HttpContex t.Current.Items [assemblyQualifiedName];
エラー:
Additional information: Object reference not set to an instance of an object.
私は私のクラスでサービスを使用してすることができますどのように。ありがとう!
サービスをクラスコンストラクタに挿入すると同じことが起こりますか? – JB06
@ JB06私がコントローラで使用するとき、それは動作し、何のエラーもありません。クラスでのみ使用すると、以上のエラーになります –