オブジェクトをクラスに挿入したいだけHttpSessionStateBase
なので、テスト可能にするという単純な問題があります。HttpSessionStateBase
は に関連しているため、HttpContextBase
Web リクエストごとに変更する必要があるためInRequestScope()
、オブジェクトのスコープに使用します。
これが私のモジュール定義です:
public class WebBusinessModule : NinjectModule
{
public override void Load()
{
this.Bind<CartManager>().ToSelf().InSingletonScope();
this.Bind<ISessionManager>().To<SessionManager>().InRequestScope()
.WithConstructorArgument("session", ctx => HttpContext.Current == null ? null : HttpContext.Current.Session)
.WithPropertyValue("test", "test");
}
}
そしてここにSessionManager
クラスがあります:
public class SessionManager : ISessionManager
{
[Inject]
public SessionManager(HttpSessionStateBase session)
{
this.session = session;
}
public SessionModel GetSessionModel()
{
SessionModel sessionModel = null;
if (session[SESSION_ID] == null)
{
sessionModel = new SessionModel();
session[SESSION_ID] = sessionModel;
}
return (SessionModel)session[SESSION_ID];
}
public void ClearSession()
{
HttpContext.Current.Session.Remove(SESSION_ID);
}
private HttpSessionStateBase session;
[Inject]
public string test { get; set; }
private static readonly string SESSION_ID = "sessionModel";
}
簡単ですが、プロジェクトを開始すると、以下の例外がスローされます。
Error activating HttpSessionStateBase
No matching bindings are available, and the type is not self-bindable.
Activation path:
3) Injection of dependency HttpSessionStateBase into parameter session of constructor of type SessionManager
2) Injection of dependency SessionManager into property SessionManager of type HomeController
1) Request for HomeController
Suggestions:
1) Ensure that you have defined a binding for HttpSessionStateBase.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
5) If you are using automatic module loading, ensure the search path and filters are correct.
「セッション」コンストラクター引数を削除しても、「テスト」プロパティを残しただけで、まだこのようなエラーが発生します!