abp icon indicating copy to clipboard operation
abp copied to clipboard

Cannot access a disposed context instance

Open ihoeysun opened this issue 5 years ago • 4 comments

hello,

my abp version is 4.0.0, when i call grpc throw exception with dbcontext is disposed.

exception message:

System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance 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 instance, or wrapping it 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: 'TransactionDbContext'.
   at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.get_ChangeTracker()
   at Microsoft.EntityFrameworkCore.Query.CompiledQueryCacheKeyGenerator.GenerateCacheKeyCore(Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.RelationalCompiledQueryCacheKeyGenerator.GenerateCacheKeyCore(Expression query, Boolean async)
   at Pomelo.EntityFrameworkCore.MySql.Query.Internal.MySqlCompiledQueryCacheKeyGenerator.GenerateCacheKey(Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstOrDefaultAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at Grace.Shop.Transaction.GrpcServices.UserWealthGrpcServiceImpl.QueryUserBalance(QueryBalanceRequest request, ServerCallContext context) in D:\company\code\server-lab\Grace.Shop\modules\transaction\src\Grace.Shop.Transaction.Application\GrpcServices\UserWealthGrpcServiceImpl.cs:line 89
   at Grpc.Shared.Server.UnaryServerMethodInvoker`3.AwaitInvoker(Task`1 invokerTask, GrpcActivatorHandle`1 serviceHandle)
   at Grpc.Shared.Server.UnaryServerMethodInvoker`3.AwaitInvoker(Task`1 invokerTask, GrpcActivatorHandle`1 serviceHandle)
   at Grpc.AspNetCore.Server.Internal.CallHandlers.UnaryServerCallHandler`3.HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
   at Grpc.AspNetCore.Server.Internal.CallHandlers.ServerCallHandlerBase`3.<HandleCallAsync>g__AwaitHandleCall|8_0(HttpContextServerCallContext serverCallContext, Method`2 method, Task handleCall)

code:

 [UnitOfWork]
        public override  async Task<QueryBalanceResult> QueryUserBalance(QueryBalanceRequest request, ServerCallContext context)
        {
            var wealth = await _userWealthRepository.Where(x => x.UserId == Guid.Parse(request.UserId)).FirstOrDefaultAsync();

            if (wealth is null)
            {
                return new QueryBalanceResult() { ArtistBalance = 0, Coin = 0, Points = 0 };
            }

            return new QueryBalanceResult() { ArtistBalance = 99, Coin = 0, Points = 0 };

            //return new QueryBalanceResult() { ArtistBalance = (double)wealth.ArtistBalance, Coin = wealth.Coin, Points = wealth.Points };
        }

How to solve? thanks

ihoeysun avatar Dec 09 '20 10:12 ihoeysun

See https://docs.abp.io/en/abp/latest/Unit-Of-Work#unitofworkattribute

realLiangshiwei avatar Dec 15 '20 07:12 realLiangshiwei

Did you find a answer to this issue?

rygar08 avatar Feb 05 '21 19:02 rygar08

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 06 '21 23:04 stale[bot]

Hi ! Did you find a answer to this issue? Please.

ghost avatar Jan 06 '25 01:01 ghost