I try to use my UnitOfWork inside the implementation IAuthorizationFilter, but after I move between several pages, I get this exception:
System.InvalidOperationException: The operation could not be completed because the DbContext was deleted.
FilterConfig.cs
filters.Add(DependencyResolver.Current.GetService(typeof(PermissionFilter)));
NinjectMappings.cs
public class NinjectMappings : NinjectModule
{
public override void Load()
{
Bind<MyContext>().ToSelf().InRequestScope();
Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
PermissionFilter.cs
public class PermissionFilter : IAuthorizationFilter
{
public PermissionFilter(IUnitOfWork unitOfWork)
{
}
}
I managed to get around this:
Bind<IUnitOfWork>()
.ToMethod(m => GetUnitOfWork())
.WhenInjectedExactlyInto(typeof(PermissionFilter));
private IUnitOfWork GetUnitOfWork()
{
return new UnitOfWork(new MyContext());
}
Now the problem is that it GetUnitOfWorkis called only once, when the application starts. I have tried alternating between InTransientScopeand to InRequestScopeno avail. Therefore, database updates are not retrieved; instead, my UnitOfWork always returns the same data.
, DbContext has been disposed, IAuthorizationFilter.
? new using() Locator .