带有Collection属性的SOAP类

时间:2014-08-04 21:32:52

标签: c# wcf silverlight soap

是否可以将一组项目作为属性传递给肥皂参考?

我有一个类,我正在使用它作为报表的数据源,我希望能够将包含详细项目集合的标题项目传递给报表。我已经设置了以下类和方法,该方法返回标题详细信息,但项目集合不会发送到客户端。

public class PriceHeader
{
    [Key]
    public string Desc { get; set; }
    public ObservableCollection<Item> Items { get; set; }
}

public class Item
{
    [Key]
    public string Field { get; set; }
    public string Value1 { get; set; }
    public string Value2 { get; set; }
}

[Query(IsComposable = false)]
public IEnumerable<PriceHeader> GetPriceHeaders()
{
    return this.DataContext.Prices.Select(i => new PriceHeader()
    {
        Desc = i.Description,

        Items = new System.Collections.ObjectModel.ObservableCollection<Item>(i.LinkedItems.Select(rc => new Item()
        {
            Field = rc.FieldName,
            Value1 = rc.Value,
            Value2 = rc.Value2
        }))
    });
}

我将Siverlight与WCF一起使用,报告类使用soap客户端链接到WCF服务。

感谢

1 个答案:

答案 0 :(得分:1)

我无法重现这个问题,即我可以从服务中将一组项目传递给客户端。您是否完全确定您的linq正确填充了PriceHeader对象中的Items列表?我的猜测是你将PriceHeader对象的集合返回给其Items属性总是空列表的客户端......

这是我尝试的内容:

服务方:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    IEnumerable<PriceHeader> GetData();
}

public class Service1 : IService1
{
    public IEnumerable<PriceHeader> GetData()
    {
        return PriceHeader.GetPriceHeaders();
    }
}

public class PriceHeader
{
    public string Desc { get; set; }

    public ObservableCollection<Item> Items { get; set; }

    public static IEnumerable<PriceHeader> GetPriceHeaders()
    {
        var list = new List<PriceHeader>
        {
            new PriceHeader(){Desc = "Desc1", Items=new ObservableCollection<Item>{new Item(){Field = "field1", Value1 = "val11", Value2 = "val21"}}},
            new PriceHeader(){Desc = "Desc2", Items=new ObservableCollection<Item>{new Item(){Field = "field2", Value1 = "val12", Value2 = "val22"}}}
        };

        return list;
    }
}

客户方:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var client = new Service1Client();
    client.GetDataCompleted += client_GetDataCompleted;
    client.GetDataAsync();

}

private void client_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
    //here I inspected the results and even the Items property of PriceHeader objects were properly filled
    var results = e.Result as IEnumerable<PriceHeader>;

    //not sure if this is necessary here
    (sender as Service1Client).CloseAsync();
}

有趣的是观察到没有必要用DataContract和DataMember属性(分别)来装饰PriceHeader及其属性,因为服务代理生成会自动执行此操作。