0

我目前正在将 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 引用传递时,列表变为数组(如下所示)

[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 服务中维护集合类型,或者我可以在客户端实现的解决方法?

谢谢,哈登

4

2 回答 2

0

AFAIK 你不能。网络参考不允许 List

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

于 2013-08-21T07:53:39.653 回答
0

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

于 2013-08-21T07:55:59.767 回答