question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InvalidOperationException: Collection was modified; enumeration operation may not execute.

See original GitHub issue

Hi, Windsor, while resolving, throws exceptions like that:

Error details: An error ocurred while executing the step
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Castle.Windsor.Proxy.AbstractProxyFactory.ShouldCreateProxy(ComponentModel model)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, ConstructorCandidate constructor, Object[] arguments)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
   at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
   at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
   at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
   at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
   at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
   at Castle.Windsor.WindsorContainer.Resolve[T]()
   at DPF.Common.Step`2.GetRunnerComponents(String jobSettings, String stepSettings) 

What leads to function:

        public bool ShouldCreateProxy(ComponentModel model)
        {
            if (model.HasInterceptors)
            {
                return true;
            }

            var options = model.ObtainProxyOptions(false);
            if (options != null && options.RequiresProxy)
            {
                return true;
            }
            if (selectors != null && selectors.Any(s => s.HasInterceptors(model)))
            {
                return true;
            }

            return false;
        }

Is it problem in windsor or in my project? Shoundn’t it be thread save or something?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, Nov 18, 2016

Hey, I’ve encountered similar issue. Has anyone investigated this? Thanks

My stack:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.ObtainPropertyValue(CreationContext context, PropertySet property, IDependencyResolver resolver)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.SetUpProperties(Object instance, CreationContext context)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
   at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
   at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
   at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
   at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
   at yTos.Core.MVC.Controllers.CastleWindsorControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
1reaction
jonorossicommented, Sep 27, 2017

@Mattio91 @Beton no one is investigating this issue because we at least need a repro, something we can run that reproduces the exception.

@neutmute your problem might be the same as this defect but you didn’t include your stack trace to compare, @Mattio91’s makes no mention of WCF anywhere.

We need a repro, however we’d love a pull request with a fix, that’ll guarantee this gets fixed for you guys. The pull request doesn’t need to be perfect, we can work together once we’ve got something.

Without a repro we’ll close this issue in a few days.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Collection was modified; enumeration operation may not ...
So the issue behind this error is, we can not modify the list/dictionary while we are looping through the same. One of the...
Read more >
Why the error Collection was modified enumeration ...
This error occurs when a looping process is being running on a collection (Ex: List) and the collection is modified (data added or...
Read more >
Collection Was Modified Enumeration Operation May Not ...
The collection was modified enumeration operation may not execute exception occurs when you modify the collection while looping through the same.
Read more >
Collection was modified; enumeration operation may not ...
Solution 1 – If you're removing items, use RemoveAll() · Solution 2 – If you're adding items, put them in a temp and...
Read more >
enumeration operation may not execute. At foreach
Solution 2. You cannot enumerate a collection in a foreach block and modify the collection in the same block.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found