Collection was modified; enumeration operation may not execute

May 30, 2011 at 2:57 AM

Exception Detail:

Collection was modified; enumeration operation may not execute. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
Source Error: 

Line 109:                if (_instrumentationProvider == null)Line 110:                {Line 111:                    if (Container.Current.IsRegistered<IInstrumentationProvider>())Line 112:                        _instrumentationProvider = Container.Current.Resolve<IInstrumentationProvider>(Line 113:                            new ParameterOverride("instanceName", name)); 
Source File: E:\working folder\remp2\trunk\code\software\1.Remp.Framework\1.Projects\Remp.Repository\DataAccess\DbConnectionScope.cs    Line: 111 
Stack Trace: 

[InvalidOperationException: Collection was modified; enumeration operation may not execute.]   System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52   System.Collections.Generic.Enumerator.MoveNext() +9645125   Microsoft.Practices.Unity.UnityContainer.FillTypeRegistrationDictionary(IDictionary`2 typeRegistrations) +273   Microsoft.Practices.Unity.UnityContainer.get_Registrations() +75   Microsoft.Practices.Unity.UnityContainerExtensions.IsRegistered(IUnityContainer container, Type typeToCheck, String nameToCheck) +71   Microsoft.Practices.Unity.UnityContainerExtensions.IsRegistered(IUnityContainer container, Type typeToCheck) +21   Microsoft.Practices.Unity.UnityContainerExtensions.IsRegistered(IUnityContainer container) +48

I always thought IsRegistered method is thread safe but it is not, and I how to fix the issue? Lock? I think it not a good idea.

May 30, 2011 at 8:59 AM

Hi,

I haven't personally tried unity with multithreading. It would be nice if you can provide us a sample repro so we can look further on the issue.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 30, 2011 at 9:42 AM

Hi

a simple test:

class Program
    {
        static UnityContainer container;
        static bool stop;
        static void Main(string[] args)
        {
            container = new UnityContainer();

            var t1 = new Thread(new ThreadStart(Read));
            var t2 = new Thread(new ThreadStart(Write));

            t1.Start();
            t2.Start();

            Console.ReadLine();
            stop = true;
        }

        static void Read()
        {
            while (true)
            {
                if (container.IsRegistered<int>("a"))
                    Console.WriteLine("OK");
                Thread.Sleep(10);
                if (stop)
                    break;
            }
        }

        static void Write()
        {
            int index = 0;
            while (true)
            {
                container.RegisterType<Program>(index.ToString());
                Console.WriteLine("Regist " + index);
                Thread.Sleep(10);
                index++;
                if (stop)
                    break;
            }
        }
    }

this code will throw a InvalidOperationException.

May 30, 2011 at 11:00 AM

I believe the container itself is not thread safe as Chris Tavares mentioned on this thread. I'm not sure what is your exact requirements but why do you need to register and check the registrations from the container at the same time?

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 6, 2012 at 2:45 AM

I faced the same problem and coded a thread-safe method to access the container. See my blog post here: A Thread-Safe, Global Unity Container.

Sep 12, 2012 at 12:00 PM
LSpencer777 wrote:

I faced the same problem and coded a thread-safe method to access the container. See my blog post here: A Thread-Safe, Global Unity Container.

Link is not work

Sep 13, 2012 at 5:46 AM
ShurikEv wrote:
LSpencer777 wrote:

I faced the same problem and coded a thread-safe method to access the container. See my blog post here: A Thread-Safe, Global Unity Container.

Link is not work

Yes, link is broken

Sep 21, 2012 at 8:55 AM
Edited Sep 21, 2012 at 8:57 AM

Read http://philipm.at/2011/0819/ and it might give you an idea on what is the problem. Also read this discussion http://unity.codeplex.com/discussions/268223 where it is stated that : "IsRegistered is intended for debugging your apps only".....