CSLA Light - An item with the same key has already been added.

CSLA Light - An item with the same key has already been added.

Old forum URL: forums.lhotka.net/forums/t/6954.aspx


CampbellCM posted on Friday, May 15, 2009

I have an EditableChildList and I'm getting an ArgumentException with this message:

an item with the same key has already been added

Here's the code where this is happening:

private void btnDelete_Click(object sender, RoutedEventArgs e)
{
if (dgApplications.SelectedIndex >= 0)
{
_applications.Remove(_applications[dgApplications.SelectedIndex]);
_applications.BeginSave();
}
}

The error occurs at the call to BeginSave.

Any ideas?

CampbellCM replied on Saturday, May 16, 2009

Here's the stack trace:

System.ArgumentException was unhandled by user code
Message="An item with the same key has already been added."
StackTrace:
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 Csla.Serialization.Mobile.SerializationInfo.AddValue(String name, Object value, Boolean isDirty)
at Csla.Core.FieldManager.FieldDataManager.OnGetState(SerializationInfo info, StateMode mode)
at Csla.Core.MobileObject.Csla.Serialization.Mobile.IMobileObject.GetState(SerializationInfo info)
at Csla.Serialization.Mobile.MobileFormatter.SerializeObject(Object obj)
at Csla.Core.BusinessBase.OnGetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.Core.MobileObject.Csla.Serialization.Mobile.IMobileObject.GetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.Serialization.Mobile.MobileFormatter.SerializeObject(Object obj)
at Csla.Core.MobileBindingList`1.OnGetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.BusinessListBase`2.OnGetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.Core.MobileBindingList`1.Csla.Serialization.Mobile.IMobileObject.GetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.Serialization.Mobile.MobileFormatter.SerializeObject(Object obj)
at Csla.Serialization.Mobile.MobileFormatter.Serialize(XmlWriter writer, Object graph)
at Csla.Serialization.Mobile.MobileFormatter.Serialize(Stream serializationStream, Object graph)
at Csla.Serialization.Mobile.MobileFormatter.Serialize(Object obj)
at Csla.DataPortalClient.WcfProxy`1.BeginUpdate(Object obj, Object userState)
at Csla.DataPortal`1.BeginUpdate(Object obj, Object userState)
at Csla.DataPortal.BeginUpdate[T](Object obj, EventHandler`1 callback, Object userState)
at Csla.DataPortal.BeginUpdate[T](Object obj, EventHandler`1 callback)
at Csla.BusinessListBase`2.BeginSave(EventHandler`1 handler, Object userState)
at Csla.BusinessListBase`2.BeginSave(EventHandler`1 handler)
at Fidelity.Management.Console.ApplicationListing.btnDelete_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
InnerException:

sergeyb replied on Saturday, May 16, 2009

Do you have OnGetState overridden somewhere?

Sergey Barskiy
Principal Consultant
office: 678.405.0687 | mobile: 404.388.1899

Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

-----Original Message-----
From: CampbellCM [mailto:cslanet@lhotka.net]
Sent: Saturday, May 16, 2009 10:42 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] CSLA Light - An item with the same key has already been added.

Here's the stack trace:

System.ArgumentException was unhandled by user code
Message="An item with the same key has already been added."
StackTrace:
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 Csla.Serialization.Mobile.SerializationInfo.AddValue(String name, Object value, Boolean isDirty)
at Csla.Core.FieldManager.FieldDataManager.OnGetState(SerializationInfo info, StateMode mode)
at Csla.Core.MobileObject.Csla.Serialization.Mobile.IMobileObject.GetState(SerializationInfo info)
at Csla.Serialization.Mobile.MobileFormatter.SerializeObject(Object obj)
at Csla.Core.BusinessBase.OnGetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.Core.MobileObject.Csla.Serialization.Mobile.IMobileObject.GetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.Serialization.Mobile.MobileFormatter.SerializeObject(Object obj)
at Csla.Core.MobileBindingList`1.OnGetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.BusinessListBase`2.OnGetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.Core.MobileBindingList`1.Csla.Serialization.Mobile.IMobileObject.GetChildren(SerializationInfo info, MobileFormatter formatter)
at Csla.Serialization.Mobile.MobileFormatter.SerializeObject(Object obj)
at Csla.Serialization.Mobile.MobileFormatter.Serialize(XmlWriter writer, Object graph)
at Csla.Serialization.Mobile.MobileFormatter.Serialize(Stream serializationStream, Object graph)
at Csla.Serialization.Mobile.MobileFormatter.Serialize(Object obj)
at Csla.DataPortalClient.WcfProxy`1.BeginUpdate(Object obj, Object userState)
at Csla.DataPortal`1.BeginUpdate(Object obj, Object userState)
at Csla.DataPortal.BeginUpdate[T](Object obj, EventHandler`1 callback, Object userState)
at Csla.DataPortal.BeginUpdate[T](Object obj, EventHandler`1 callback)
at Csla.BusinessListBase`2.BeginSave(EventHandler`1 handler, Object userState)
at Csla.BusinessListBase`2.BeginSave(EventHandler`1 handler)
at Fidelity.Management.Console.ApplicationListing.btnDelete_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
InnerException:

CampbellCM replied on Saturday, May 16, 2009

I figured out what the problem was:

private static PropertyInfo UsesHostTypeAsIntProperty = RegisterProperty
(typeof(Application), new PropertyInfo("UsesHostType", "Uses Host Type"));
public int UsesHostTypeAsInt
{
get
{
return GetProperty(UsesHostTypeAsIntProperty);
}
}

private static PropertyInfo UsesHostTypeProperty = RegisterProperty
(typeof(Application), new PropertyInfo("UsesHostType", "Uses Host Type"));
public HostTypes UsesHostType
{
get
{
return (HostTypes)GetProperty(UsesHostTypeAsIntProperty);
}
}

The two properties above are a workaround for the fact that enumerations are not serializable by CSLA Light. As you can see there are two properties, one named UsesHostTypeAsInt and UsesHostType. The error is that the first parameter of the PropertyInfo constructors are both the same. I changed the first one to "UsesHostTypeAsInt" and it now works as expected.

sergeyb replied on Saturday, May 16, 2009

Just a suggestion - you really do not need a backing field for enum property, you can use backing field from AsInt property inside enum based property - this will make communication slightly more efficient, albeit by only a few bytes.

Sergey Barskiy
Principal Consultant
office: 678.405.0687 | mobile: 404.388.1899

Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation


-----Original Message-----
From: CampbellCM [mailto:cslanet@lhotka.net]
Sent: Saturday, May 16, 2009 11:45 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: CSLA Light - An item with the same key has already been added.

I figured out what the problem was:

private static PropertyInfo UsesHostTypeAsIntProperty = RegisterProperty
(typeof(Application), new PropertyInfo("UsesHostType", "Uses Host Type"));
public int UsesHostTypeAsInt
{
get
{
return GetProperty(UsesHostTypeAsIntProperty);
}
}

private static PropertyInfo UsesHostTypeProperty = RegisterProperty
(typeof(Application), new PropertyInfo("UsesHostType", "Uses Host Type"));
public HostTypes UsesHostType
{
get
{
return (HostTypes)GetProperty(UsesHostTypeAsIntProperty);
}
}

The two properties above are a workaround for the fact that enumerations are not serializable by CSLA Light. As you can see there are two properties, one named UsesHostTypeAsInt and UsesHostType. The error is that the first parameter of the PropertyInfo constructors are both the same. I changed the first one to "UsesHostTypeAsInt" and it now works as expected.

Copyright (c) Marimer LLC