会话状态 - 使用Autofac注入时的奇怪行为

时间:2016-08-29 08:58:19

标签: c# session autofac

考虑我在mvc app中配置了autofac,如下所示:

builder.RegisterModule(new AutofacWebTypesModule());

需要注入会话状态:

internal class DefaultSessionAccess : ISessionAccess
{
    private readonly HttpSessionStateBase session;

    public DefaultSessionAccess(HttpSessionStateBase session)
    {
        this.session = session;
    }

    public void Put(string key, object @object)
    {
        this.session.Add(key, @object);
    }
}

问题是以下代码放在某个控制器中:

public class SomeController : Controller
{
    private ISessionAccess session;

    public SomeController(ISessionAccess session)
    {
        this.session = session;
    }


    public ActionResult SomeAction()
    {
        this.session.Put("key", "value");
        var isNull = this.HttpContext.Session["key"] == null;
    }
}

问题是我的本地计算机(IIS)isNull false - 对象在会话中正确保存。但是当我将它部署到azure时, isNull是......真实!某种程度上,对象不会保存在会话中。

当我直接说出来时:

 this.HttpContext.Session["key"] = "value";

它在两台服务器上都能正常工作。

根据文档,HttpContext.Current.Session已注册为HttpSessionStateBase

为什么会这样?

0 个答案:

没有答案