views:

2619

answers:

2

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.

+2  A: 

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.

Andrew Hare
I am not hung up on implementing this with a NameValueCollection; can you suggest a technique with simpler types that are serializable? Also, I forgot to add ColdFusion as a tag.
John Galt
I you want a true dictionary-like type then what I posted is about as simple as its going to get. If you just want to send the data then Freddy's answer is probably the way to go.
Andrew Hare
Added the ColdFusion tag.
flipdoubt
+4  A: 

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

eglasius
Any chance you'd be willing to sketch out a bit more of that idea?
John Galt
@unknown check my updated version. I am unsure what else you wanted to know, can you be more specific? You probably need to give it a try, check how it works and come back with any doubts - unless there is something else?
eglasius
You can also use List<NameValue>, and the client will still see NameValue[].
John Saunders