Cannot access a disposed object. Object name: 'SimpleInjector.Scope'
Using WCF services, at times during execution I receive the following:
WebServices V2 - Error occured while acquiring Semaphore 'SyncEngine.UserSyncSemaphoreV2.1601':SimpleInjector.ActivationException: Cannot access a disposed object. Object name: 'SimpleInjector.Scope'. ---> System.ObjectDisposedException: Cannot access a disposed object. Object name: 'SimpleInjector.Scope'.
at SimpleInjector.Scope.ThrowObjectDisposedException()
at SimpleInjector.Scope.GetInstanceInternal[TService,TImplementation](ScopedRegistration`2 registration)
at SimpleInjector.Advanced.Internal.LazyScopedRegistration`2.GetInstance(Scope scope)
at lambda_method(Closure )
at SimpleInjector.InstanceProducer.GetInstance()
--- End of inner exception stack trace ---
at SimpleInjector.InstanceProducer.GetInstance()
at SimpleInjector.Internals.ContainerControlledCollection`1.d__24.MoveNext()
at SimpleInjector.Internals.ContainerControlledCollection`1.System.Collections.Generic.ICollection.CopyTo(TService[] array, Int32 arrayIndex)
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
Any idea what could be the happening?
Thanks
Can you elaborate some more?
at times during execution
Does this mean, sometimes as in randomly even with/without a request, all time, or during a specific type of request?
Can you show the registration for UserSyncSemaphoreV2? How it's used and what types of dependencies this component has?
@ AlthalusDGr8, if you are not running v3.3.2, please update to that version and try again. If the exception and stack trace you get when running v3.3.2 isn't exactly the same as the one posted, please post the new exception message and stack trace below. Besides, the stack trace you are posting isn't the complete stack trace; please post the complete stack trace.
I was getting a similar issue just recently while using v3.3.2 with ExecutionContextScoping. I just tried downgrading the service to a version my other services were using (v3.1.5) and the issue went away.
It seems that the services were being created successfully the first time I needed them, but would throw an exception the second time I needed them.
Hi James,
Could you post the exception details and complete dtack trace of the exception and inner exceptions?
IQ.Foundation.Messaging.AzureServiceBus.Administration.AzureTopicSubscriberRegistry: ERROR - System.ObjectDisposedException: Cannot access a disposed object. Object name: 'SimpleInjector.Container'. at SimpleInjector.Container.ThrowContainerDisposedException() at SimpleInjector.Container.GetRegistration(Type serviceType, Boolean throwOnFailure) at IQ.Foundation.Messaging.SimpleInjector.SimpleInjectorMessageHandlerResolver.Resolve(Object message) ...
You disposed the Container. You'll have to find out where the container got disposed. This will typically be your own code, or you added the container to some sort of request specific cache from a framework you use. Sometimes those frameworks go through the list of objects to call Dispose on disposable objects in the list.
We'll definitely take a look at things on our side. We find it odd, though, that this works with the older version just fine.
Certainly the objects IN the container are getting disposed at the end of the ExecutionContext, but that shouldn't affect the container itself.
What happened in a recent release is that we more actively throw exceptions in case the container gets used after it was disposed (since we didn't do this properly before - shame on us). What this means is that in previous releases you could accidentally dispose the container, but only find out much later in time because of something weird and unexpected that happened because of that disposal.
Using the container after it's disposed could obviously lead to all sorts of hard to track problems. Among other things, all created disposable singletons will be disposed when a container gets disposed and lifestyles might get torn (which means that Scoped registrations might get multiple instances per request).
So, in your case, chances are that everything kept working after the container got disposed, but this could have easily changed when you started to change your container registrations.
So I urge you to find the spot where the container got disposed instead of sticking with an old version of Simple Injector, because sooner or later you will run into trouble anyway because you disposed the container.
Good to know. I'll pass that on to our framework team.
In v4 we will add more information to the ObjectDisposedException to allow you to directly see what the stack trace was at the moment that the container got disposed.