ISettingService: "The operation cannot be completed because the DbContext has been disposed using"

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
I do not know if this is by design or a bug: If I use ISettingService as instance variable everything works fine, for example:


        private readonly IThemeContext _themeContext;
        private readonly ISettingService _settingService;
        private readonly MobileRedirectSettings _mySettings;

        public MobileRedirectService(IThemeContext themeContext
            ISettingService settingService)
        {
            _themeContext = themeContext;
            _settingService = settingService;

            _mySettings = _settingService.LoadSetting<MobileRedirectSettings>(storeContext.CurrentStore.Id);
        }


If I change ISettingService to local variable in constructor, I will get the error "The operation cannot be completed because the DbContext has been disposed.", for example:


        private readonly IThemeContext _themeContext;
        private readonly MobileRedirectSettings _mySettings;

        public MobileRedirectService(IThemeContext themeContext
            ISettingService settingService)
        {
            _themeContext = themeContext;

            _mySettings = settingService.LoadSetting<MobileRedirectSettings>(storeContext.CurrentStore.Id);
        }


My guess is, that Autofac will dispose ISettingService after constructor has run and DbContext is disposed too at this time. Using instance variable, disposing is done after class is terminated.

Is this a bug or by design?

Here is the error message:


Serverfehler in der Anwendung /.

The operation cannot be completed because the DbContext has been disposed.

Beschreibung: Unbehandelte Ausnahme beim Ausführen der aktuellen Webanforderung. Überprüfen Sie die Stapelüberwachung, um weitere Informationen über diesen Fehler anzuzeigen und festzustellen, wo der Fehler im Code verursacht wurde.

Ausnahmedetails: System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.

Quellfehler:


Zeile 164:                            ga.KeyGroup == keyGroup
Zeile 165:                            select ga;
Zeile 166:                var attributes = query.ToList();
Zeile 167:                return attributes;
Zeile 168:            });

Quelldatei: C:\tfs\Webseite\Main\Web2016\Libraries\Nop.Services\Common\GenericAttributeService.cs    Zeile: 166

Stapelüberwachung:


[InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.]
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +1014
   System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator() +43
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +432
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +70
   Nop.Services.Common.<>c__DisplayClass11_0.<GetAttributesForEntity>b__0() in C:\tfs\Webseite\Main\Web2016\Libraries\Nop.Services\Common\GenericAttributeService.cs:166
   Nop.Core.Caching.CacheExtensions.Get(ICacheManager cacheManager, String key, Int32 cacheTime, Func`1 acquire) in C:\tfs\Webseite\Main\Web2016\Libraries\Nop.Core\Caching\Extensions.cs:50
   Nop.Core.Caching.CacheExtensions.Get(ICacheManager cacheManager, String key, Func`1 acquire) in C:\tfs\Webseite\Main\Web2016\Libraries\Nop.Core\Caching\Extensions.cs:24
   Nop.Services.Common.GenericAttributeService.GetAttributesForEntity(Int32 entityId, String keyGroup) in C:\tfs\Webseite\Main\Web2016\Libraries\Nop.Services\Common\GenericAttributeService.cs:160
   Nop.Services.Common.GenericAttributeExtensions.GetAttribute(BaseEntity entity, String key, IGenericAttributeService genericAttributeService, Int32 storeId) in C:\tfs\Webseite\Main\Web2016\Libraries\Nop.Services\Common\GenericAttributeExtensions.cs:42
   Nop.Web.Framework.Themes.ThemeContext.get_WorkingThemeName() in C:\tfs\Webseite\Main\Web2016\Presentation\Nop.Web.Framework\Themes\ThemeContext.cs:51
   Nop.Plugin.CU.MobileRedirect.Services.MobileRedirectService.Execute(HttpContextBase httpContext) +530
   Nop.Plugin.CU.MobileRedirect.Filters.MobileRedirectActionFilter.OnActionExecuting(ActionExecutingContext filterContext) +175
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +268
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +1079
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__31(AsyncCallback asyncCallback, Object asyncState) +93
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +170
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters, AsyncCallback callback, Object state) +292
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +1167
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +170
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +455
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +42
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +67
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +170
   System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +897
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +170
   System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +711
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +94
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +67
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +170
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +555
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +923
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +137
7 years ago
Meanwhile I got the error also using instance variable. Seems that there is another problem and it was only coincidence that it worked after changing to instance variable.

:-(
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.