NameValueCollections in WebMethod

Discussion in 'ASP .Net Web Services' started by Chris Arnold, Feb 26, 2006.

  1. Chris Arnold

    Chris Arnold Guest

    Hi All,

    Has anyone been able to return a
    System.Collection.Specialized.NameValueCollection object from a WebMethod?

    Compiles correctly, but then throws an InvalidOperationException on viewing
    the asmx file...

    "You must implement the Add(System.String) method on
    System.Collections.Specialized.NameValueCollection because it inherits from
    ICollection."

    Fair enough, but ICollection does not require an Add method and neither does
    IEnumerable (which ICollection extends).

    Is this just a quirk of some strange serialization the CLR is attempting to
    perform?

    Chris
    Chris Arnold, Feb 26, 2006
    #1
    1. Advertising

  2. Chris Arnold

    Josh Twist Guest

    Hi Chris,

    Yes - it's because the NameValueCollection isn't XmlSerializable.

    "To be XML serializable, types which inherit from ICollection must have
    an implementation of Add(System.String) at all levels of their
    inheritance hierarchy.
    System.Collections.Specialized.NameValueCollection does not implement
    Add(System.String)."

    You could easily create your own custom collection by inheriting from
    CollectionBase (.NET 1.x:
    http://msdn.microsoft.com/library/d...systemcollectionscollectionbaseclasstopic.asp)
    or using generics (.NET 2.0:
    http://msdn2.microsoft.com/en-us/library/ms132397(VS.80).aspx). This
    should be XmlSerializable without any trouble.

    Josh

    Josh
    Josh Twist, Feb 27, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Temp
    Replies:
    1
    Views:
    1,084
    Natty Gur
    Aug 11, 2003
  2. sunnyz
    Replies:
    1
    Views:
    2,938
    Kevin Spencer
    Oct 15, 2004
  3. Kevin Spencer

    Strange Exception with WebMethod

    Kevin Spencer, Oct 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    419
    Kevin Spencer
    Oct 28, 2004
  4. Alex
    Replies:
    3
    Views:
    383
  5. Franck
    Replies:
    1
    Views:
    473
    Karl Seguin
    Oct 4, 2005
Loading...

Share This Page