Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NameValueCollection in C# webservice gives not XML serializable error

I am getting this error message when I try to "Add Web Reference" to my ASMX proxy project:

**"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)"**

I need to pass name-value pairs to my web service and here is what I came up with:

public class FileService : System.Web.Services.WebService
{

    [WebMethod]
    public string UploadFile(byte[] incomingArray
        , string FileName
        , long FileLengthInBytes
        , NameValueCollection nvcMetaData)

I now realize I need help changing this (I am new to C# and this part of the .Net framework). If I can get this working, my web service will need to be invoked by ColdFusion (right now I am using an .aspx page for calling the web service). So whatever means I come up with for passing a variable number of NAME-VALUE pairs I need it callable by CF. Thanks in advance.

like image 766
John Adams Avatar asked Mar 19 '09 19:03

John Adams


2 Answers

As an alternative, you can just define a simple NameValue class and receive an array.

public class NameValue
{
   public string Name{get;set;}
   public string Value{get;set;}
}

[WebMethod]
public string UploadFile(byte[] incomingArray
    , string FileName
    , long FileLengthInBytes
    , NameValue[] nvcMetaData)

If on the client side you have a NameValueCollection you can easily map to your type. Check the sample I posted in this question

like image 95
eglasius Avatar answered Oct 14 '22 07:10

eglasius


Unfortunately all of the .NET library dictionary structures are not serializable. I have heard good things about this code - maybe that will help.

Basically what you will be doing with the code I linked is creating a new type that has custom serialization methods that will allow you to serialize a dictionary to XML. If this is the route you choose it would be best to compile this type in its own assembly so that you can share that assembly with the client of your service that way it can be used as a parameter to the service method.

like image 43
Andrew Hare Avatar answered Oct 14 '22 08:10

Andrew Hare