我有一个代码,控制器调用服务和服务使用工作单元来处理DB。我使用Unity作为依赖注入。我需要在请求作用域结束后自动处理unity(dbContext)。我没有在UnityCofig.cs中引用PerRequestLifetimeManager。有什么建议吗?
发布于 2016-06-17 14:12:36
尝试创建生存期管理器的新派生类型
public class PerHttpRequestLifetime : LifetimeManager
{
// This is very important part and the reason why I believe mentioned
// PerCallContext implementation is wrong.
private readonly Guid _key = Guid.NewGuid();
public override object GetValue()
{
return HttpContext.Current.Items[_key];
}
public override void SetValue(object newValue)
{
HttpContext.Current.Items[_key] = newValue;
}
public override void RemoveValue()
{
var obj = GetValue();
HttpContext.Current.Items.Remove(obj);
}
}来源:MVC, EF - DataContext singleton instance Per-Web-Request in Unity
https://stackoverflow.com/questions/37874236
复制相似问题