我想创建一个过滤器并将其放置在需要访问数据库的操作上。此过滤器将处理当前的工作单元,因此我不需要在一个操作中手动调用它一次或多次。我正在这样做:
public class DisposeUnitOfWorkAttribute :FilterAttribute, IActionFilter
{
public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
IUnitOfWork currentUnitOfWork = UnitOfWork.Current;
if (currentUnitOfWork != null)
{
currentUnitOfWork.Dispose();
}
return null;
}
public bool AllowMultiple
{
get { return false; }
}
}
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new DisposeUnitOfWorkAttribute());
filters.Add(new HandleErrorAttribute());
}
}
[DisposeUnitOfWork]
public ViewResult Index()
{
var user = _usersRepository.Get(x => x.Username == "jack").ToList();
//UnitOfWork.Current.Dispose();
return View();
}
我得到了例外:The given filter instance must implement one or more of the following filter interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.
我该如何纠正这个?