wcf从wcf sservice返回一个列表

Tro*_*roj 1 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>();
 }
Run Code Online (Sandbox Code Playgroud)

这就是我的GetRootLocations样子,它返回IQueryable,我想知道我是否可以IQueryable从我的WCF服务返回?

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

Kir*_*rst 5

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

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

这里有一个下拉列表,允许您选择"集合类型",默认值为System.Array.只需将其改为即可System.Collections.Generic.List完成.每当我以这种方式添加服务引用时,我通常会这样做.