現在Castle WindsorからSimple Injectorに移行しようとしています。サービス内ウィンザーからシンプルインジェクターへの移行、HttpContext.Current.Sessionがnull
私はサービスのためにコンストラクタの下に示すようにHttpSessionStateBaseを注入しようとしています:
public UserSession(IResourceRepository resourceRepository, IUser user,
IRoleResolver roleResolver, IApproverRepository approverRepository,
IActiveDirectory activeDirectory, HttpSessionStateBase httpSession,
Settings appSettings)
{
_resourceRepository = resourceRepository;
_user = user;
_roleResolver = roleResolver;
_approverRepository = approverRepository;
_activeDirectory = activeDirectory;
_httpSession = httpSession;
_allowedUsernamePostfixes = appSettings.AuthenticationAllowedUsernamePostfixes;
}
ウィンザーでこれを行うには、私は新しいHttpSessionStateWrapperとしてHttpSessionStateBaseを注入するFactoryMethodを使用することができます。
private static IWindsorContainer BuildWindsorContainer()
{
var container = new WindsorContainer();
container.Kernel.Resolver.AddSubResolver(
new CollectionResolver(container.Kernel, true));
container.Register(
Component.For<IWindsorContainer>().Instance(container)
);
container.Install(FromAssembly.This());
container.Register(
Component.For<HttpRequestBase>()
.UsingFactoryMethod(() => new HttpRequestWrapper(HttpContext.Current.Request))
.LifestyleTransient(),
Component.For<HttpSessionStateBase>()
.UsingFactoryMethod(() => new HttpSessionStateWrapper(HttpContext.Current.Session))
.LifestyleTransient(),
return container;
}
このメソッドは、Global.asax.csファイル内のASP.NET MVC 4プロジェクトのApplication_Startメソッドで呼び出されたときに動作します。
私は今、シンプルジェクトコード以下のように2つのウィンザーFactoryMethodsを配置することにより、簡単な注入するためにそれを変換しようとしています:
container.Register<HttpRequestBase>(
() => new HttpRequestWrapper(HttpContext.Current.Request),
Lifestyle.Transient);
container.Register<HttpSessionStateBase>(
() => new HttpSessionStateWrapper(HttpContext.Current.Session),
Lifestyle.Transient);
問題は、私はのHttpContextその後、Application_Startメソッドでこのメソッドを呼び出すときということです。シンプルインジェクションがコンテナ検証メソッドを呼び出すとき、Current.Sessionはnullになります。
どれでも解決方法はありますか?
こんにちは、あなたはそのコードがランタイムデータを使用して臭いを覚えています。悲しいことに、アプリケーションの創始者にランタイムデータを使用しているサービスの周りの認証に基づいて... 私はいくつかのコードを試しましたが、HttpSessionStateWrapperとHttpSessionStateの間に暗黙の型がないため、HttpSessionStateBaseを動作させることができません。 – McBoman
申し訳ありませんが、私はコードをテストする時間がありませんでした。うまくいけば、これはあなたにこれを回避する方法のいくつかの指針を与えるでしょう。 – Steven
もちろん、助けてくれてありがとう。 – McBoman