wcf从wcf服务返回一个列表

时间:2010-09-19 20:40:34

标签: c# asp.net wcf

如何从WCF服务返回某种列表,这是我的WCF服务中的方法。

我的界面:

[OperationContract]
[WebInvoke(Method = "GET",
 ResponseFormat = WebMessageFormat.Json,
 BodyStyle = WebMessageBodyStyle.Wrapped,
 UriTemplate = "Locations")]
IList<Location> GetLocations();

public IList<Location> GetLocations()
{
   Pazar.Data.Repositories.LocationRepository locRepository = 
       new Pazar.Data.Repositories.LocationRepository();
   return locRepository.GetRootLocations().ToList<Location>();
 }

以及我的GetRootLocations看起来如何,它返回IQueryable,我想知道我是否可以从我的WCF服务返回IQueryable

public IQueryable<Location> GetRootLocations()
{
   IQueryable<Location> locations = GetAll().Where(p => !p.ID_Parent.HasValue).OrderBy(p => p.Sequence);
   return locations;
}

2 个答案:

答案 0 :(得分:5)

当List作为SOAP数据包传输时,List的序列化方式与数组相同 - 它只是XML。由您的客户决定是否应将该集合放入List而不是Array。

如果您使用.NET客户端(并使用“添加服务引用...”工具)来使用该服务,这非常简单。在“添加服务引用”弹出窗口中,单击“高级”(或者如果您已有服务引用,右键单击它并选择“配置服务引用...”),您将看到服务引用的配置屏幕。

这里有一个下拉菜单,允许您选择“收集类型”,其中默认值为System.Array。只需将其更改为System.Collections.Generic.List即可。每当我以这种方式添加服务引用时,我通常会这样做。

答案 1 :(得分:3)

无法从WCF返回List,因为这是.NET特定类型。 WCF旨在供任何客户端使用,而不仅仅是.NET客户端。要在客户端上获取List,您必须获取线路中的数组并在客户端代码中创建List。