【发布时间】:2015-06-23 16:14:13
【问题描述】:
我已阅读并在 Google 上搜索了所有内容,但似乎无法让它发挥作用。我根据这些帖子在我的 MVC5 应用程序中为 Unity 创建了一个自定义 LifetimeManager:
这是我的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 对象和凭据/为他注射。这似乎是互联网上使用的常见模式,所以我想知道我错过了什么。
【问题讨论】:
标签: asp.net-mvc dependency-injection asp.net-mvc-5 unity-container