2013-08-21 24 views
0

我正在將HttpListener應用程序轉換爲WCF Web服務。 Web服務將由運行.NETCF 2.0的Windows CE 5.0客戶端使用。我已經通過Web引用(不是服務引用)正常工作了。在這裏值得一提的是,我無法將框架版本從2.0升級到3.0或3.5。如何在Web服務中維護集合類型(使用Web引用而非服務引用)

我遇到的問題是,我有包含列表屬性(如下面的一個)

[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,但通常的解決方案是使用Service Reference,我無法使用它。

有沒有一種方法可以維護整個Web服務的集合類型,或者我可以實現客戶端的解決方法?

感謝, 哈登

回答

0

假設無法通過添加Web引用來完成此操作。 XmlSerializer會將所有集合轉換爲數組,並且您無法避免這種情況。這是WCF用於指定生成的代理集合類型的數據接觸的功能之一,但僅當您使用svcutil添加服務引用時纔可用。假設你可以做的唯一事情就是用你的手編輯ypur契約,並指定適當的集合而不是數組。

0

AFAIK你不能。 Web引用不允許列表

但是,如果您將其更改爲服務引用,則可以通過轉到屬性並指定要使用的集合類型來實現此目的。