Open Closed

Task Asynchronous Run Cause an error,EF Core DbCotext disposed #7177


User avatar
0
heshengli created
  • ABP Framework version: v7.2.2

  • UI Type: Blazor WASM

  • Database System: EF Core ( PostgreSQL.)

  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

  • Exception message and full stack trace:

  • Steps to reproduce the issue:

    在 Microsoft.EntityFrameworkCore.DbContext.CheckDisposed() 在 Microsoft.EntityFrameworkCore.DbContext.get_ContextServices() 在 Microsoft.EntityFrameworkCore.DbContext.get_Model() 在 Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.get_EntityType() 在 Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.CheckState() 在 Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.get_EntityQueryable() 在 Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.System.Linq.IQueryable.get_Provider() 在 System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 source, Expression1 predicate) 在 OneAdmin.BaseManagement.StudentAppService.<<BatchCreateAsync>b__2_0>d.MoveNext() 在 D:\workspace\projects\OneAdmin\src\OneAdmin.Application\BaseManagement\StudentAppService.cs 中: 第 34 行

Error=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: 'OneAdminDbContext'.

 public class StudentAppService : ApplicationService, IStudentAppService
 {
     private readonly IRepository<Student, Guid> _StudentRepository;
     public StudentAppService(IRepository<Student, Guid> studentRepository)
     {
         _StudentRepository = studentRepository;
     }

     [UnitOfWork(false)]
     public async Task<bool> BatchCreateAsync()
     {
         await Task.Factory.StartNew(async () =>
         {
             try
             {
                 for (int index = 1; index <= 10; index++)
                 {
                     await _StudentRepository.InsertAsync(new Student(GuidGenerator.Create())
                     {
                         Name = index.ToString(),
                         No = index
                     }, true);
                     await Task.Delay(TimeSpan.FromSeconds(5));
                     var query = await _StudentRepository.GetQueryableAsync();
                     var data = query.FirstOrDefault(t => t.No == index);
                     Console.WriteLine($"Name={data.Name},No={data.No}");
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine($"Error={ex.Message}");

             }
         });
         return true;
     }
 }

3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Please create an uow in your thread method. https://docs.abp.io/en/abp/latest/Unit-Of-Work#begin-a-new-unit-of-work

  • User Avatar
    0
    heshengli created

    running error

    using System;
    using System.Linq;
    using System.Threading.Tasks;
    using Volo.Abp.Application.Services;
    using Volo.Abp.Domain.Repositories;
    using Volo.Abp.Uow;
    
    namespace OneAdmin.BaseManagement
    {
        public class StudentAppService : ApplicationService, IStudentAppService
        {
            private readonly IRepository<Student, Guid> _StudentRepository;
            private readonly IRepository<Classes, Guid> _ClassesRepository;
            private readonly IRepository<ClassStudent> _ClassStudentRepository;
    
            public StudentAppService(
                IRepository<Student, Guid> studentRepository,
                IRepository<Classes, Guid> classesRepository,
                IRepository<ClassStudent> classStudentRepository
                )
            {
                _StudentRepository = studentRepository;
                _ClassesRepository = classesRepository;
                _ClassStudentRepository = classStudentRepository;
            }
    
            public async Task<bool> BatchCreateAsync()
            {
    
                await Task.Factory.StartNew(async () =>
                {
                    try
                    {
                        using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
                        {
                            for (int indexClass = 1; indexClass <= 10; indexClass++)
                            {
                                var data = await _ClassesRepository.InsertAsync(new Classes(GuidGenerator.Create())
                                {
                                    Name = indexClass.ToString(),
                                    No = indexClass
                                }, true);
                                await CreateStudentAsync(data);
                            }
                        }
    
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error={ex.Message}");
                    }
                });
                return true;
            }
    
            private async Task CreateStudentAsync(Classes classes)
            {
                var stuQuery = await _StudentRepository.GetQueryableAsync();
                var count = stuQuery.LongCount() + 1;
                for (int index = 1; index <= count; index++)
                {
                    var student = await _StudentRepository.InsertAsync(new Student(GuidGenerator.Create())
                    {
                        Name = index.ToString(),
                        No = index
                    }, true);
                    await Task.Delay(TimeSpan.FromSeconds(1));
                    if (classes != null && student != null)
                    {
                        await CreateClsStuAsync(classes, student);
                    }
                }
            }
    
            private async Task CreateClsStuAsync(Classes classes, Student student)
            {
                await _ClassStudentRepository.InsertAsync(new ClassStudent(classes.Id, student.Id), true);
                await Task.Delay(TimeSpan.FromSeconds(1));
    
            }
        }
    }
    

    single entity insert not error,muti entity running error

    alll code register single ,CommonService.BatchCreateAsync(),also running not error

    using System;
    using System.Linq;
    using System.Threading.Tasks;
    using Volo.Abp.DependencyInjection;
    
    namespace OneAdmin.BaseManagement
    {
        public class CommonService 
        {
            private readonly IStudentRepository _StudentRepository;
            private readonly IClassesRepository _ClassesRepository;
            private readonly IClassStudentRepository _ClassStudentRepository;
            //private readonly IUnitOfWorkManager UnitOfWorkManager;
            public CommonService(
                IStudentRepository studentRepository,
                IClassesRepository classesRepository,
                IClassStudentRepository classStudentRepository
                //IUnitOfWorkManager unitOfWorkManager
                )
            {
                _StudentRepository = studentRepository;
                _ClassesRepository = classesRepository;
                _ClassStudentRepository = classStudentRepository;
                //UnitOfWorkManager = unitOfWorkManager;
            }
    
    
            public async Task<bool> BatchCreateAsync()
            {
                try
                {
                    for (int indexClass = 1; indexClass <= 10; indexClass++)
                    {
                        var data = await _ClassesRepository.InsertAsync(new Classes(Guid.NewGuid())
                        {
                            Name = indexClass.ToString(),
                            No = indexClass
                        }, true);
                        await Task.Delay(TimeSpan.FromSeconds(1));
                        //var clsQuery = await _ClassesRepository.GetQueryableAsync();
                        //await Console.Out.WriteLineAsync($"new:{clsQuery.LongCount()}");
                        var stuQuery = await _StudentRepository.GetQueryableAsync();
                        var count = stuQuery.LongCount();
                        for (int index = 1; index <= indexClass; index++)
                        {
                            var student = await _StudentRepository.InsertAsync(new Student(Guid.NewGuid())
                            {
                                Name = index.ToString(),
                                No = index
                            }, true);
                            await Task.Delay(TimeSpan.FromSeconds(1));
                            if (data != null && student != null)
                            {
                                await _ClassStudentRepository.InsertAsync(new ClassStudent(data.Id, student.Id), true);
                            }
                        }
                    }
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error={ex.Message}");
                }
                return true;
            }
    
            private async Task CreateStudentAsync(Classes classes)
            {
                var stuQuery = await _StudentRepository.GetQueryableAsync();
                var count = stuQuery.LongCount() + 1;
                for (int index = 1; index <= count; index++)
                {
                    var student = await _StudentRepository.InsertAsync(new Student(Guid.NewGuid())
                    {
                        Name = index.ToString(),
                        No = index
                    }, true);
                    await Task.Delay(TimeSpan.FromSeconds(1));
                    if (classes != null && student != null)
                    {
                        await CreateClsStuAsync(classes, student);
                    }
                }
            }
    
            private async Task CreateClsStuAsync(Classes classes, Student student)
            {
                await _ClassStudentRepository.InsertAsync(new ClassStudent(classes.Id, student.Id), true);
                await Task.Delay(TimeSpan.FromSeconds(1));
    
            }
        }
    }
    
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    What are errors now?

    Please share the full stack error.

    You need to CompleteAsync the uow.

    
    using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
    {
        for (int indexClass = 1; indexClass <= 10; indexClass++)
        {
            var data = await _ClassesRepository.InsertAsync(new Classes(GuidGenerator.Create())
            {
                Name = indexClass.ToString(),
                No = indexClass
            }, true);
            await CreateStudentAsync(data);
        }
        
        await uow.CompleteAsync();
    }
    
Made with ❤️ on ABP v8.2.0-preview Updated on března 25, 2024, 15:11