Why we cant return List<T> in ASMX web services?

我们两清 提交于 2020-01-21 05:22:11

问题


As developers know we cant return List<T> with web services, we can only return lists with converting them to .ToArray(); I've searched some, but cant get effective answer about Why we cant retun List with web services. Why we must convert them ToArray(); ?


回答1:


Web services are supposed to be interoperable with many languages. Nearly all languages have arrays, but only .NET has the specific implementation of List<T> that you're using.




回答2:


There is nothing whatsoever preventing you from returning List<T> from an ASMX web service. I have no idea why you believe that.

What may be confusing you is that XML Schema (used by the WSDL) cannot describe "lists", per se. In fact, it cannot describe arrays, either. It can describe a series of repeating elements. All collections, including arrays, are returned as sets of repeating elements.

On the client side, the client has no way to know whether the server returned List<T>, T[], or IEnumerable<T>, and no reason to care, either.




回答3:


It depends on the interoperability settings of the webservice, an object like int[] is easier to understand for a non .NET language then List<int>. If you develop your web service under WCF, List<T> is supported as a return type.




回答4:


What's described in the web services is a "collection". It's up to the client to determine what type of "collection" to use. If the client's .Net, when adding the service reference, click Advanced, and you'll have the ability to choose a generic list.




回答5:


As far as I am aware as long as you explicitly declare you are returning a List (of T) method title you can return the object; otherwise you will receive a serialize error.

e.g

<WebMethod()> _
Public Function Search(ByVal SearchTerm As String) As List(Of 'object here')


来源:https://stackoverflow.com/questions/4249000/why-we-cant-return-listt-in-asmx-web-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!