Using NameValueCollection in C# webservice gives not XML serializable error
Asked Answered
F

3

7

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.

Finn answered 19/3, 2009 at 19:21 Comment(0)
P
5

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

Pinball answered 19/3, 2009 at 19:53 Comment(3)
Any chance you'd be willing to sketch out a bit more of that idea?Finn
@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?Pinball
You can also use List<NameValue>, and the client will still see NameValue[].Expect
H
2

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.

Hybrid answered 19/3, 2009 at 19:26 Comment(2)
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.Finn
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.Hybrid
M
-1

If you use object as a type, the ScriptService will automatically/secretly serialize it to a dictionary, for you to use like this:

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public object MyServiceMethod(int recordid, object data)
{
   Dictionary<String, Object> noway = (Dictionary<String, Object>)data;
   // ...
   return noway;

}

Marcel answered 30/11, 2011 at 21:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.