Monday, October 8, 2012

"An item with the same key has already been added." Exception

"An item with the same key has already been added." Exception

We have a Silverlight 4 RIA that has been working great.  Per our regulations, we are forced to upgrade to VS 2010 SP1.  After applying it, we are getting the following exception every time we run our application. 

{System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.ServiceModel.DomainServices.Client.MetaType..ctor(Type type)
   at System.ServiceModel.DomainServices.Client.MetaType.GetMetaType(Type type)
   at System.ServiceModel.DomainServices.Client.Entity.get_MetaType()
   at System.ServiceModel.DomainServices.Client.Entity.RaiseDataMemberChanged(String propertyName)
   at xxxxxxx.Silverlight.Web.Models.organization_node.set_create_dttm(DateTime value)
   at Readorganization_nodeFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
   at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, DataContract& dataContract)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
   at ReadArrayOfanyTypeFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )
   at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, DataContract& dataContract)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
   at ReadQueryResultOfprojectFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
   at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, DataContract& dataContract)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
   at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName)
   at System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.CompositeClientFormatter.DeserializeReply(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeEndService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

This is thrown every time a value is set on a public property in one of the RIA Entity objects.  The error will throw when ValidateProperty or RaiseDataMemberChanged is called as part of the setter for the property.  I have tried a bunch of work arounds with no success.  I have upgraded to Visual Studio 2012, upgraded project to Silverlight 5, upgraded our 3rd party controls, nothing works.  I have Googled this and many others have had similar problems, but no solutions have been helpful.

The call we are using to load our entities:

            context.Load<project>(context.GetProjectByProjectIdQuery(projectId), delegate(LoadOperation<project> operation)
            {
                project currentProject = operation.Entities.First();
                CurrentProject = currentProject;

                CurrentProject.proj_stat_cd = ProjectStatus.Active.GetAttributeValue<CodeValueAttribute, string>();

                callback.Invoke(currentProject);

                ExitBusy();
            }, null);

It dies before it can get to the first line in the delegate.

We are not using the [Composition] attribute and do not have a column "HasChanges".

Answers & Comments...

Answer: 1

http://forums.silverlight.net/t/61049.aspx/1

http://forums.silverlight.net/t/2328.aspx/1

http://www.sitefinity.com/devnet/forums/sitefinity-3-x/set-up-installation/error-an-item-with-the-same-key-has-already-been-added.aspx



Answer: 2

Thanks for the reply maggicarter2012.  The first link I have already looked at and doesn't do anything for our problem.  The 2nd link refers to the setting of cookies, it dies before the silverlight page is even starting to render....we are getting the error when Linq is getting the data/populating the entities.  And the 3rd link didn't wouldn't load...so I don't know what information it provides.



Answer: 3

Try this: http://forums.silverlight.net/t/255040.aspx/1 



Answer: 4

Thanks for everyones eyes and any comments that have been posted. 

We have found our problems after MONTHS of searching and tearing apart our code.  As in my original post, I noted where we had read that if you have "HasChanges" in your code, SP1 breaks.  Well..we have found that we ran across another place Microsoft made changes and we got bit!  We had a property on one of our entities that was called "ValidationErrors".  No problems prior to VS 2010 SP1....once the SP1 was installed...bam...our code no longer worked. 

Our solution was that we needed to rename this property. 

It would be nice if Microsoft would publish new properties/functions/methods that THEY added when a SP or release is released so we, the consumers of their frameworks and products, know what to look for when something like this happens.  Do they realize just how many man hours (and dollars) we spent trying to figure out what the non-descriptive error message it was giving us was not happy about?  If we just had the list of those kinds of changes, we could have done a search on our code base and quickly found what the problem was. 





No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog