?
Solved

Web Service with nested key collecitions

Posted on 2007-10-15
2
Medium Priority
?
424 Views
Last Modified: 2013-11-07
I'm trying to create a web service that will expose an object with nested key collections.  When I test out the web service I get this error:

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.ObjectModel.KeyedCollection`2.AddKey(TKey key, TItem item)
   at System.Collections.ObjectModel.KeyedCollection`2.InsertItem(Int32 index, TItem item)
   at System.Collections.ObjectModel.Collection`1.Add(T item)
   at Test.WebServices.ModelService.Test() in C:\Dev 09\Test.WebServices\ModelService.asmx.cs:line 75

I've tested just one key collection and it works great it's when I nest key collections is when it throws the errors.

This is a simplify version of the objects involve and test method I used:
    [WebMethod]
    public Class1 Test()
    {
      Class1 x1 = new Class1();

      Class2 x2 = new Class2();
      testObj y = new testObj();
      for (int i2 = 0; i2 < 5; i2++)
      {
        testObj2 y2 = new testObj2();
        for (int i3 = 0; i3 < 5; i3++)
        {
          y2.ID = i3;
          y2.Value = "test obj " + i3.ToString();
          x2.Add(y2);
        }
        y.ID = i2;
        y.Obj2 = x2;
        x1.Add(y);
      }
      return x1;
    }
  public class Class1 : KeyedCollection<int, testObj>
  {
    protected override int GetKeyForItem(testObj item)
    {
      return item.ID;
    }
  }

  public class Class2 : KeyedCollection<int, testObj2>
  {
    protected override int GetKeyForItem(testObj2 item)
    {
      return item.ID;
    }
  }

  public class testObj
  {
    private int _id;
    private Class2 _obj2;

    public int ID
    {
      get { return _id; }
      set { _id = value; }
    }

    public Class2 Obj2
    {
      get { return _obj2; }
      set { _obj2 = value; }
    }
  }

  public class testObj2
  {
    private int _id;
    private string _value;
    public int ID
    {
      get { return _id; }
      set { _id = value; }
    }

    public string Value
    {
      get { return _value; }
      set { _value = value; }
    }
  }
0
Comment
Question by:malibuboats
2 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20080018
Make sure all the keys within the keycollection are unique.
0
 
LVL 22

Accepted Solution

by:
JimBrandley earned 500 total points
ID: 20080095
The problem is the second pass through the inner loop - change the key as shown below, and it works.

public Class1 Test()
{
   Class1 x1 = new Class1();

   Class2 x2 = new Class2();
   testObj y = new testObj();
   testObj2 y2;
   for (int i2 = 0; i2 < 5; i2++)
   {
      y2 = new testObj2();
      for (int i3 = 0; i3 < 5; i3++)
      {
         y2.ID = i3 + (i2 * 10); <-- Problem is here on second pass - key already exists
         y2.Value = "test obj " + i3.ToString();
         x2.Add(y2);
      }
      y.ID = i2;
      y.Obj2 = x2;
      x1.Add(y);
   }
   return x1;
}

Jim
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question