在 C# Web 服务中使用 NameValueCollection 不会出现 XML 可序列化错误

发布于 2024-07-15 12:25:27 字数 795 浏览 5 评论 0原文

当我尝试“添加 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 服务)。 因此,无论我想出什么方法来传递可变数量的名称-值对,我都需要它可以由 CF 调用。 提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心荒芜 2024-07-22 12:25:27

作为替代方案,您可以只定义一个简单的 NameValue 类并接收一个数组。

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)

如果在客户端您有一个 NameValueCollection,您可以轻松映射到您的类型。 检查我在此问题中发布的示例

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

墨落画卷 2024-07-22 12:25:27

不幸的是,所有 .NET 库字典结构都不可序列化。 我听说过关于这段代码的好消息 - 也许那会有帮助的。

基本上,您将使用我链接的代码创建一个新类型,该类型具有自定义序列化方法,允许您将字典序列化为 XML。 如果这是您选择的路线,最好在其自己的程序集中编译此类型,以便您可以与服务的客户端共享该程序集,这样它就可以用作服务方法的参数。

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.

何以笙箫默 2024-07-22 12:25:27

如果您使用对象作为类型,ScriptService 会自动/秘密地将其序列化为字典,供您使用,如下所示:

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

}

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;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文