Spring.Objects.Factory.ObjectCreationException: System.ArgumentNullException: Key cannot be null

StackOverflow https://stackoverflow.com/questions/13931591

  •  10-12-2021
  •  | 
  •  

Domanda

This is a long shot...

I'm using spring 1.3.2 and at random times I get ArgumentNullException exception (See stack trace below).

I'm configuring the container in a mix between XML and Code (by using ObjectDefinitionBuilder directly, no code config). And the registrations take place in parallel (5 threads loading definitions).

All my objects use Autowiring via Constructor, the error happens in both components that have items in the constructor or not.

I'm doing the following call after everything was registered in the container

context.GetObjectsOfType(typeof (IFoo)).OfType<DictionaryEntry>().Select(d => (IFoo) d.Value)

From the stack trace and looking at the code, I see that the call to IsAlias seems to be failing, but not sure I understand how this can happen.

Any thoughts/Ideas?

Stack Trace:

Spring.Objects.Factory.ObjectCreationException: Error creating object with name 'My.App.SomeFooImplementation' : Initialization of object failed : Key cannot be null.
Parameter name: key ---> System.ArgumentNullException: Key cannot be null.
Parameter name: key
   at System.Collections.Hashtable.ContainsKey(Object key)
   at System.Collections.Specialized.OrderedDictionary.Contains(Object key)
   at Spring.Objects.Factory.Support.AbstractObjectFactory.IsAlias(String name)
   at Spring.Objects.Factory.Support.DefaultListableObjectFactory.DoGetObjectNamesForType(Type type, Boolean includeNonSingletons, Boolean allowEagerInit)
   at Spring.Objects.Factory.Support.DefaultListableObjectFactory.GetObjectNamesForType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects)
   at Spring.Objects.Factory.ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(IListableObjectFactory factory, Type type, Boolean includePrototypes, Boolean includeFactoryObjects)
   at Spring.Objects.Factory.Support.DefaultListableObjectFactory.FindAutowireCandidates(String objectName, Type requiredType, DependencyDescriptor descriptor)
   at Spring.Objects.Factory.Support.DefaultListableObjectFactory.ResolveDependency(DependencyDescriptor descriptor, String objectName, IList autowiredObjectNames)
   at Spring.Objects.Factory.Support.ConstructorResolver.CreateArgumentArray(String objectName, RootObjectDefinition rod, ConstructorArgumentValues resolvedValues, ObjectWrapper wrapper, Type[] paramTypes, MethodBase methodOrCtorInfo, Boolean autowiring, UnsatisfiedDependencyExceptionData& unsatisfiedDependencyExceptionData)
   at Spring.Objects.Factory.Support.ConstructorResolver.AutowireConstructor(String objectName, RootObjectDefinition rod, ConstructorInfo[] chosenCtors, Object[] explicitArgs)
   at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.CreateObjectInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments)
   at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure)
   --- End of inner exception stack trace ---
   at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure)
   at Spring.Objects.Factory.Support.AbstractObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments)
   at Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure)
   at Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name)

Edit: I just got all the definition names and somehow there is a null entry in there, that seems to be the issue, not sure why it is happening yet...

È stato utile?

Soluzione

The only fix I could come up with was to change the internal objectDefinitionNames ArrayList to be wrapped with ArrayList.Synchronized with reflection. Not nice at all but seems to do the job.

        var field = factory.GetType().GetField("objectDefinitionNames", BindingFlags.NonPublic | BindingFlags.Instance);
        if (field == null)
            throw new InvalidOperationException("Could not get Definitions field from application context.");

        var value = (ArrayList) field.GetValue(factory);
        field.SetValue(factory, ArrayList.Synchronized(value));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top