- ABP Framework version: v7.0.0
- UI type: Angular
- DB provider: EF Core
- Tiered (MVC) or Identity Server Separated (Angular): yes / no
- Exception message and stack trace:
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. ObjectDisposed_ObjectName_Name
- Steps to reproduce the issue:
- Followed steps as shown here along with a few changes to get it to actually work at all: [Scoped Db Context #1265](https://support.abp.io/QA/Questions/1265), [Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it (or one of its parent scopes) has already been disposed. #1698](https://support.abp.io/QA/Questions/1698/Instances-cannot-be-resolved-and-nested-lifetimes-cannot-be-created-from-this-LifetimeScope-as-it-or-one-of-its-parent-scopes-has-already-been-disposed)
- Issue occurs randomly when using the Report Viewer, along with memory constantly increasing every time a report is viewed.
- I have made a github repo with the relevant code from a fresh project so feel free to lookover that and give me some suggestions, thanks. Github
32 Answer(s)
-
0
hi
Please share your project source code and steps with liming.ma@volosoft.com
-
0
Hey maliming,
I have sent through a few emails with the project source code as per requested, feel free to take a look and either respond here or via email. Thanks for the help in advance.
Regards, Josh.
-
0
ok
I will check your project as soon as possible
-
0
Thank you.
Appreciate you looking into this.
-
0
hi
I can't restore the
DevExpress.*
packages. Please share a nuget source. liming.ma@volosoft.com -
0
Hey Maliming,
I don't believe it's possible for me to directly share them with you nor legal for me to, but you can simply signup for the free trial at https://nuget.devexpress.com/, which gives you access to the relevant packages. You can also download other parts of the DevExpress Trial such as the Report Explorer at https://go.devexpress.com/DevexpressDownload_UniversalTrial.aspx.
Other information on obtaining and using the trial NuGet Feed can be found at the following: Obtaining Trail Nuget Feed: https://docs.devexpress.com/GeneralInformation/120534/installation/install-devexpress-controls-using-nuget-packages/net-core-controls-via-nuget Setup VS Studio Nuget Package Manager: https://docs.devexpress.com/GeneralInformation/116698/installation/install-devexpress-controls-using-nuget-packages/setup-visual-studios-nuget-package-manager
If you need any more help in getting those packages let me know and I'm happy to assist further. And thank you again for the help with this issue, I appreciate it.
Thanks, Josh.
-
0
hi
I will check this asap. Thanks
-
0
-
0
Hey Maliming,
Yeah, weird it just occurs randomly from refreshing for us, so apart from saying just refresh some more which isn't very helpful, do you have Discord or Microsoft Teams which would allow us to hop into a call and try reproduce the issue together giving you a better idea of the issue since you are having issues reproducing it yourself?
Regards, Josh.
-
0
I need to reproduce the problem locally to troubleshoot, Please share the full error context log.
-
0
Here are a few errors and stack traces I managed to quickly get for you, some in debug and some not:
Disposed of error out of debug: https://pastebin.com/uuajyzsF
Disposed of error in debug: https://pastebin.com/F7MZK388
Another random error: https://pastebin.com/ck1da3tn
Thanks, Josh.
-
0
The connection does not support MultipleActiveResultSets.
Do you have
MultipleActiveResultSets=True
in your connection string?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: 'BookStoreDbContext'.
Code from a client. They don't have this problem, please try.
using System; using Microsoft.Extensions.DependencyInjection; namespace Acme.Bookstore { public interface IScopedServiceProvider<TService> { ServiceScopeScope<TService> GetService(); } public class ScopedServiceProvider<TService> : IScopedServiceProvider<TService> { private readonly IServiceProvider _provider; public ScopedServiceProvider(IServiceProvider provider) { this._provider = provider ?? throw new ArgumentNullException(nameof(provider)); } public ServiceScopeScope<TService> GetService() { var scope = _provider.CreateScope(); return new ServiceScopeScope<TService>(scope); } } public class ServiceScopeScope<TService> : IDisposable { private readonly IServiceScope _scope; public ServiceScopeScope(IServiceScope scope) { _scope = scope ?? throw new ArgumentNullException(nameof(scope)); Service = scope.ServiceProvider.GetRequiredService<TService>(); } public TService Service { get; } public void Dispose() { _scope.Dispose(); } } }
using Acme.Bookstore.Books; using Acme.Bookstore.Repositories; using Acme.Bookstore.Users; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Volo.Abp.Application.Services; using Volo.Abp.DependencyInjection; using Volo.Abp.Uow; namespace Acme.Bookstore.ReportingServices { [ExposeServices(typeof(ReportingDataSourceServiceDecorator))] public class ReportingDataSourceServiceDecorator : IReportingDataSourceService, ITransientDependency { private readonly IScopedServiceProvider<ReportingDataSourceService> _scopedServiceProvider; public ReportingDataSourceServiceDecorator(IScopedServiceProvider<ReportingDataSourceService> scopedServiceProvider) { _scopedServiceProvider = scopedServiceProvider; } public IList<BookDto> GetAllBooks() { using (var scopeServer = _scopedServiceProvider.GetService()) { return scopeServer.Service.GetAllBooks(); } } } public class ReportingDataSourceService : ApplicationService, IReportingDataSourceService { private readonly IBookCustomRepository _bookRepo; public ReportingDataSourceService() { // We use this parameterless constructor in the Data Source Wizard only, and not for the actual instantiation of the repository object. throw new NotSupportedException(); } public ReportingDataSourceService(IBookCustomRepository bookRepo) { _bookRepo = bookRepo; } [UnitOfWork] public virtual IList<BookDto> GetAllBooks() { var query = _bookRepo.GetAllBooksAsync(); return ObjectMapper.Map<IList<Book>, IList<BookDto>>( query.Result.ToList() ); } } }
-
0
Hey,
I don't believe I have MultipleActiveResultSets=True in the connection strings, how you like me to try it with it?
Also, I believe I have already tried those examples as they seem to be the same as the examples I mentioned above which was the original base of the project. There are just a few changes from testing things, but yeah.
Regards, Josh.
-
0
Yes
Please try to add
MultipleActiveResultSets=true
.they seem to be the same as the examples
They are different. Please remove the
scopeServer.Dispose();
public BookStore.Test.Test Get() { using (var scopeServer = _scopedServiceProvider.GetService()) { var res = scopeServer.Service.Get(); //scopeServer.Dispose(); return res; } }
-
0
Hey,
I added it since then I haven't seen the other issue but the disposed is still occuring, the Dispose call was only added as a test as that was already occuring and once removed it continues to occur, it occurs at random but not highly frequant, I did notice when in debug it seems to occur faster.
[16:24:03 ERR] StartBuild error System.AggregateException: One or more errors occurred. (Error when trying to populate the datasource. The following exception was thrown: 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: 'BookStoreDbContext'.) ---> DevExpress.XtraReports.DataRetrievalException: Error when trying to populate the datasource. The following exception was thrown: 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: 'BookStoreDbContext'. ---> System.AggregateException: One or more errors occurred. (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: 'BookStoreDbContext'.) ---> 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: 'BookStoreDbContext'. at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed() at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices() at Microsoft.EntityFrameworkCore.DbContext.get_ChangeTracker() at Volo.Abp.EntityFrameworkCore.AbpDbContext`1.Initialize(AbpEfCoreDbContextInitializationContext initializationContext) at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.CreateDbContextAsync(IUnitOfWork unitOfWork, String connectionStringName, String connectionString) at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.GetDbContextAsync() at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository`2.GetDbSetAsync() at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository`2.GetQueryableAsync() at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync() at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed) at Volo.Abp.Domain.Repositories.RepositoryAsyncExtensions.FirstAsync[T](IReadOnlyRepository`1 repository, CancellationToken cancellationToken) --- End of inner exception stack trace --- at DevExpress.DataAccess.Native.ObjectBinding.ObjectDataSourceFillHelper.BrowseForResult(Object instance, String dataMember, ParameterList parameters) at DevExpress.DataAccess.ObjectBinding.ObjectDataSource.GetFillResult(IEnumerable`1 sourceParameters, Boolean schemaOnly, Object dataSource, String dataMember, ParameterList parameters, ObjectConstructorInfo constructor) at DevExpress.DataAccess.ObjectBinding.ObjectDataSource.Fill(IEnumerable`1 sourceParameters, Boolean schemaOnly) at DevExpress.DataAccess.ObjectBinding.ObjectDataSource.Fill(IEnumerable`1 sourceParameters) at DevExpress.DataAccess.ObjectBinding.ObjectDataSource.DevExpress.Data.IListAdapter.FillList(IServiceProvider servProvider) at DevExpress.XtraReports.Native.ListAdapterFiller`1.ExecuteCore() at DevExpress.XtraReports.Native.DataSourceFiller.Execute() --- End of inner exception stack trace --- at DevExpress.XtraReports.Native.DataSourceFiller.Execute() at DevExpress.XtraReports.UI.XtraReportBase.FillDataSources(Boolean skipIfFilled) at DevExpress.XtraReports.UI.XtraReport.FillDataSources() at DevExpress.XtraReports.UI.XtraReport.DevExpress.XtraReports.IReport.CreateDocumentCore(Single progressRange, Boolean buildForInstantPreview) at DevExpress.XtraReports.Native.DocumentCreator.Create(Single progressRange, Boolean buildForInstantPreview) at DevExpress.XtraReports.UI.XtraReport.CreateDocument(Single progressRange, Boolean buildForInstantPreview) at DevExpress.XtraReports.UI.XtraReport.CreateDocument(Boolean buildForInstantPreview) at DevExpress.XtraReports.UI.XtraReport.CreateDocument() at DevExpress.XtraPrinting.Caching.CachedReportSourceBase.CreateDocumentCore() at DevExpress.XtraReports.Web.WebDocumentViewer.Native.BuildTaskFactoryWeb.<>c__DisplayClass9_0.<CreateTask>b__0() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location --- at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) --- End of inner exception stack trace ---
Regards, Josh.
-
0
hi
Can the Get method be asynchronous?
Otherwise, try AysncHelper
[HighlightedMember] [UnitOfWork(isTransactional: false)] public virtual BookStore.Test.Test Get() { return AsyncHelper.RunSync(() => _repo.FirstAsync()); }
-
0
Hey,
I don't believe you can do this part async sadly, and the AsyncHelper didn't help, it still was occuring.
Regards, Josh.
-
0
HI
Does it go error randomly or every time?
If it
every time
please share the latest project. liming.ma@volosoft.com -
0
Hey,
It seems to be random for me, running it with visual studio debug, and duplicating tables and mass refreshing them seems to make the occurrence more likely, but still occurs at random either way. My current project is pretty much identical to what I last sent you as it was a test project for this issue, but it may still be easier for us to call and try reproduce this issue for you locally otherwise solve it for me by looking for environment differences etc. Since it seems to occur quite often for me.
Regards, Josh.
-
0
Can other people's computers reproduce the problem?
-
0
Yeah, our other developers are having the same issue, in addition to have a very simular issue when hosting on azure.
-
0
Thanks I will test it in deep.
-
0
Unfortunately I still can't reproduce the problem.
-
0
That's weird, the only thing I can suggest is hopping on a call and work on reproducing it for you or fixing it for me together, that way we can compare environments etc. As I have no clue why you are not experiencing the issue like we are.
-
0
https://us05web.zoom.us/j/86335037655?pwd=Z1VjQms5cUJheVpENjI0SVlJcDJkZz09