Why we cant return List in ASMX web services?

前端 未结 5 2018
忘掉有多难
忘掉有多难 2021-01-06 10:09

As developers know we cant return List with web services, we can only return lists with converting them to .ToArray(); I\'ve searched some, but cant ge

相关标签:
5条回答
  • 2021-01-06 10:27

    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.

    0 讨论(0)
  • 2021-01-06 10:38

    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')
    
    0 讨论(0)
  • 2021-01-06 10:40

    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.

    0 讨论(0)
  • 2021-01-06 10:41

    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.

    0 讨论(0)
  • 2021-01-06 10:41

    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.

    0 讨论(0)
提交回复
热议问题