加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 无法获得有效的Unity Session Lifetime Manager,

发布时间:2020-12-16 09:41:23 所属栏目:asp.Net 来源:网络整理
导读:我已经阅读并使用Google搜索了所有内容,但似乎无法让它发挥作用.我根据这些帖子在我的MVC5应用程序中创建了一个自定义的LifetimeManager for Unity: MVC3 Unity Framework and Per Session Lifetime Manager This may be the issue I am experiencing 这是
我已经阅读并使用Google搜索了所有内容,但似乎无法让它发挥作用.我根据这些帖子在我的MVC5应用程序中创建了一个自定义的LifetimeManager for Unity:

> MVC3 Unity Framework and Per Session Lifetime Manager
> This may be the issue I am experiencing

这是我的SessionLifetimeManager

public class SessionLifetimeManager : LifetimeManager
{
    private string key = Guid.NewGuid().ToString();

    public override object GetValue()
    {
        return HttpContext.Current.Session[key];
    }

    public override void RemoveValue()
    {
        HttpContext.Current.Session.Remove(key);
    }

    public override void SetValue(object newValue)
    {
        HttpContext.Current.Session[key] = newValue;
    }
}

我只有几种我正在玩的类型,这是UnityConfig.cs中的相关注册:

container.RegisterType<IEpiSession,EpiSession>(new SessionLifetimeManager(),new InjectionConstructor(config.AppServerURI,config.PathToSysConfig));
container.RegisterType<IReportRepository,EpicorReportRepository>(new TransientLifetimeManager());

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

请注意,EpicorReportRepository通过构造函数注入依赖于IEpiSession.

public class EpicorReportRepository : IReportRepository
{
    private IEpiSession session;

    // DI constructor
    public EpicorReportRepository(IEpiSession session) {
        this.session = session;
    }
// ...
}

我的问题:在第一个用户/会话连接到应用程序之后,之后的每个新用户/会话似乎仍在使用第一个用户为他创建/注入的EpiSession对象和凭据.这似乎是互联网上使用的常见模式,所以我想知道我错过了什么.

解决方法

你是如何测试IEpiSession在不同的会话中是否相同的?

尝试从不同的浏览器打开您的应用程序.如果在同一浏览器中打开多个选项卡,则使用相同的会话.

我检查了你的代码,它对我有用.
SetResolver()中唯一的区别是:

DependencyResolver.SetResolver(
    type => container.Resolve(type),types => container.ResolveAll(types));

完整的注册码如下:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ...
        var container = new UnityContainer();
        container.RegisterType<IEpiSession,EpiSession>(
            new SessionLifetimeManager(),new InjectionConstructor("config.AppServerURI","config.PathToSysConfig"));
        container.RegisterType<IReportRepository,EpicorReportRepository>(new TransientLifetimeManager());

        DependencyResolver.SetResolver(
            type => container.Resolve(type),types => container.ResolveAll(types));
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读