public async Task Invoke(HttpContext context) { IDisposable dbCtx = DbContext.CreateContext(Settings.ConnectionString); IDisposable secCtx = null; WebSecurityUtil.Authenticate(context); var identity = (context.User as ErpPrincipal)?.Identity as ErpIdentity; if (identity != null) { secCtx = SecurityContext.OpenScope(identity.User); } await next(context); await Task.Run(() => { if (dbCtx != null) { dbCtx.Dispose(); } if (secCtx != null) { secCtx.Dispose(); } }); }
public async Task Invoke(HttpContext context, IRequestService requestService) { var dbCtx = DbContext.CreateContext(Settings.ConnectionString); requestService.AddObjectToDispose(dbCtx); WebSecurityUtil.Authenticate(context); var identity = (context.User as ErpPrincipal)?.Identity as ErpIdentity; if (identity != null) { var securityContext = SecurityContext.OpenScope(identity.User); requestService.AddObjectToDispose(securityContext); } await next(context); }