具有集合属性的SOAP类
本文关键字:SOAP 属性 集合 | 更新日期: 2023-09-27 18:16:26
是否可以将项的集合作为soap引用中的属性传递?
我有一个类,我使用它作为报表的数据源,我希望能够将带有详细项目集合的标题Items传递给报表。我已经设置了下面的类和方法,该方法返回标题的详细信息,但项目的集合不发送到客户端。
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
}))
});
}
我使用silverlight与WCF和报告类链接到WCF服务与soap客户端。
谢谢
我无法重现这个问题,也就是说,我可以将一组项从服务传递给客户机。你绝对确定你的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及其属性,因为服务代理生成会自动这样做。