title: ASP.NET Boilerplate: ObjectDisposedException in EventHandler
body: <p>I use ABP 5.1.0
  .Net Core,
  I have my class</p>
  <pre><code>ItemAsyncEventBaseHandler : IAsyncEventHandler&lt;TEvent&gt;, ITransientDependency
  where TEvent : EventData {…}
  </code></pre>
  <p>Then i have class:</p>
  <pre><code>CustomHandler: ItemAsyncEventBaseHandler&lt;EventData&gt;
  {
   public CustomHandler(
              IUnitOfWorkManager unitOfWorkManager,
              ISettingManager settingManager)
          {
              _unitOfWorkManager = unitOfWorkManager;
              _settingManager = settingManager;
          }
  }
  
  public override async Task HandleInternallyAsync(ItemUpdatedEvent eventData)
  {
          await _settingManager.ChangeSettingForApplicationAsync(&quot;key&quot;, value.ToString());
  }
  </code></pre>
  <p>At first glance, this works, but if I run the handler on two different items in quick succession, I get an error:
  (On some machines it is enough to run the event on two items, on others it is necessary to run the event on the other two items in order for the same error to occur.)</p>
  <pre><code>System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
  Object name: 'MyDbContext'.
     at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
     at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
     at Microsoft.EntityFrameworkCore.DbContext.Set[TEntity]()
     at Abp.EntityFrameworkCore.Repositories.EfCoreRepositoryBase`3.GetQueryable()
     at Abp.EntityFrameworkCore.Repositories.EfCoreRepositoryBase`3.GetAllIncluding(Expression`1[] propertySelectors)
     at Abp.EntityFrameworkCore.Repositories.EfCoreRepositoryBase`3.FirstOrDefaultAsync(Expression`1 predicate)
     at Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinallyAndGetResult[T](Task`1 actualReturnValue, Func`1 postAction, Action`1 finalAction)
     at Abp.Configuration.SettingStore.GetSettingOrNullAsync(Nullable`1 tenantId, Nullable`1 userId, String name)
     at Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinallyAndGetResult[T](Task`1 actualReturnValue, Func`1 postAction, Action`1 finalAction)
     at Abp.Configuration.SettingManager.InsertOrUpdateOrDeleteSettingValueAsync(String name, String value, Nullable`1 tenantId, Nullable`1 userId)
     at Abp.Configuration.SettingManager.ChangeSettingForApplicationAsync(String name, String value)
     at Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinally(Task actualReturnValue, Func`1 postAction, Action`1 finalAction)
     at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task)
     at Nito.AsyncEx.AsyncContext.&lt;&gt;c__DisplayClass15_0.&lt;Run&gt;b__0(Task t)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task&amp; currentTaskSlot, Thread threadPoolThread)
  --- End of stack trace from previous location where exception was thrown ---
     at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task)
     at Nito.AsyncEx.AsyncContext.Run(Func`1 action)
  </code></pre>
  <p>And the value in the application settings is not updated.</p>
  <p>I tried wrapping in uow but it doesn't help:</p>
  <pre><code>public override async Task HandleInternallyAsync(ItemUpdatedEvent eventData) 
  {
     using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
    {
      await _settingManager.ChangeSettingForApplicationAsync(&quot;key&quot;, value.ToString());
      await uow.CompleteAsync();
    }
  }
  </code></pre>