微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在C#webservice中使用NameValueCollection不会产生XML可序列化错误

当我尝试“添加Web引用”到我的ASMX代理项目时收到此错误消息:
**"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)"**

我需要将名称 – 值对传递给我的Web服务,这就是我想出的:

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

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

我现在意识到我需要帮助改变这一点(我是C#的新手和.Net框架的这一部分).如果我可以使用它,我的Web服务将需要由ColdFusion调用(现在我使用.aspx页面调用Web服务).因此无论我想出什么方式来传递可变数量的NAME-VALUE对,我都需要它可由CF调用.提前致谢.

解决方法

作为替代方案,您可以只定义一个简单的NameValue类并接收一个数组.
public class NameValue
{
   public string Name{get;set;}
   public string Value{get;set;}
}

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

如果在客户端有一个NameValueCollection,您可以轻松映射到您的类型.检查我在question中发布的样本

原文地址:https://www.jb51.cc/c/119243.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐