Ninject - In what scope DbContext should get binded when RequestScope is meaningless?
If you decide to go on with custom scope, the solution is:
public sealed class CurrentScope : INotifyWhenDisposed{ [ThreadStatic] private static CurrentScope currentScope; private CurrentScope() { } public static CurrentScope Instance => currentScope ?? (currentScope = new CurrentScope()); public bool IsDisposed { get; private set; } public event EventHandler Disposed; public void Dispose() { this.IsDisposed = true; currentScope = null; if (this.Disposed != null) { this.Disposed(this, EventArgs.Empty); } }}
Binding:
Bind<DbContext>().To<MyDbContext>().InScope(c => CurrentScope.Instance)
And finally:
using (CurrentScope.Instance){ // your request... // you'll get always the same DbContext inside of this using block // DbContext will be disposed after going out of scope of this using block}