【问题标题】:How to maintain collection type across a web service (using Web Reference not Service Reference)如何跨 Web 服务维护集合类型(使用 Web 参考而不是服务参考)
【发布时间】:2013-08-21 07:48:40
【问题描述】:

我目前正在将 HttpListener 应用程序转换为 WCF Web 服务。 Web 服务将由运行 .NETCF 2.0 的 Windows CE 5.0 客户端使用。我已经通过 Web 参考(不是服务参考)让这个工作正常。值得一提的是,我无法将框架版本从 2.0 升级到 3.0 或 3.5。

我遇到的问题是我有多个包含 List 属性的类(如下所示)

[Serializable]
[XmlRoot]
public class Products
{
    public Products()
    {
        Items = new List<Product>();
        Errors = new List<Error>();
    }

    [XmlElement]
    public List<Product> Items
    { get; set; }

    [XmlElement]
    public List<Error> Errors
    { get; set; }

    [XmlElement]
    public long DeviceId
    { get; set; }

    [XmlElement]
    public User UserId
    { get; set; }

    [XmlElement]
    public long UserColour
    { get; set; }
}

当这个属性通过 Web Reference 传递时,List 变成了一个数组(如下所示)

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://server-path/Service.svc")]
public partial class Products {

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Items")]
    public Product[] Items;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Errors")]
    public Error[] Errors;

    /// <remarks/>
    public long DeviceId;

    /// <remarks/>
    public User UserId;

    /// <remarks/>
    public long UserColour;
}

我知道使用服务引用可以更改通过服务传递的集合的集合类型,但是我不知道如何使用 Web 引用来实现这一点。

我见过很多其他类似的问题,例如thisthis,但通常的解决方案是使用服务参考,但我无法使用。

有没有一种方法可以在 Web 服务中维护集合类型,或者我可以在客户端实现一种解决方法?

谢谢, 哈登

【问题讨论】:

    标签: c# wcf web-services


    【解决方案1】:

    假设没有办法通过添加Web Reference来做到这一点。 XmlSerializer 会将所有集合转换为数组,您无法避免这种情况。这是 WCF 的数据契约的特性之一,用于指定生成的代理集合的类型,但只有在使用 svcutil 添加服务引用时才可用。假设您唯一能做的就是手动编辑 ypur 合约,并指定正确的集合而不是数组。

    【讨论】:

      【解决方案2】:

      AFAIK 你不能。网络引用不允许列表

      但是,如果您将其更改为服务引用,您可以通过转到属性并指定要使用的集合类型来做到这一点。

      【讨论】:

        猜你喜欢
        • 2011-01-10
        • 2014-07-19
        • 2010-11-15
        • 2012-12-31
        • 1970-01-01
        相关资源
        最近更新 更多