Ben*_*ter 3 structuremap ravendb asp.net-web-api
为了在ASP.NET WebApi中刷新对RavenDB的更改,我创建了以下Action Filter:
public class RavenDbUnitOfWorkAttribute : ActionFilterAttribute
{
public Func<IDocumentSession> SessionFactory { get; set; }
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var session = SessionFactory.Invoke();
if (session != null && actionExecutedContext.Exception == null)
{
session.SaveChanges();
}
base.OnActionExecuted(actionExecutedContext);
}
}
Run Code Online (Sandbox Code Playgroud)
为了注入,IDocumentSession我创建了一个IFilterProvider循环遍历每个过滤器的自定义,并使用setter注入(StructureMap)设置任何依赖项.
我想知道IFilterProvider作用域的实例.阅读这篇文章,看起来每个请求都会创建控制器.
目前,我正在IDocumentSession使用StructureMap 明确确定每个请求的范围.问题是,如果我依赖IDependencyScope(使用嵌套容器)将相同的实例IDocumentSession注入我的Action Filter?
进一步测试似乎Action Filters不使用与Controller相同的依赖范围.我宁愿不在控制器中使用代码来刷新会话.
正如你practicly回答自己的问题JabbR,使用GetDependencyScope扩展方法对HttpRequestMessage应该解决您的问题:
var session = Request.GetDependencyScope().GetService(typeof(IDocumentSession));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1304 次 |
| 最近记录: |