3

オブジェクトをクラスに挿入したいだけHttpSessionStateBaseなので、テスト可能にするという単純な問題があります。HttpSessionStateBaseは に関連しているため、HttpContextBaseWeb リクエストごとに変更する必要があるため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.

「セッション」コンストラクター引数を削除しても、「テスト」プロパティを残しただけで、まだこのようなエラーが発生します!

4

1 に答える 1

3

問題は、HttpSessionStateが から継承されないことですHttpSessionStateBaseHttpSessionStateBaseは ASP MVC の新しい概念です。詳細はこちら: ASP.NET に互換性のないセッション状態の種類が 2 つあるのはなぜですか?

でラップHttpContex.Current.SessionしてみてくださいHttpSessionStateWrapper

.WithConstructorArgument("session", x => HttpContext.Current == null ? 
                                                                null : 
                                                                new HttpSessionStateWrapper(HttpContext.Current.Session));
于 2013-06-10T07:02:54.477 に答える