Why would a WCF method's return type be changed from a generic collection to an array?

前端 未结 4 1524
一整个雨季
一整个雨季 2021-01-21 13:23

I have a WCF service method which I have written for return type as collection type.

But if I generate a proxy class and consume the method from the client the methods r

相关标签:
4条回答
  • 2021-01-21 13:46

    You need to tell svcutil to generate the types as whatever generic type you want. You do this by adding this switch to the svcutil command:

    svcutil ..blah... /ct:System.Collections.ObjectModel.Collection`1
    

    The /ct is short for /collectionType. In this case I specified Collection<T> but you could have just as easily used System.Collections.Generic.List<T>.

    0 讨论(0)
  • 2021-01-21 13:47

    If you're using Visual Studio you can just right click your service reference to configure it. You'll then have an option (in advanced settings?) to tell your app how to handle arrays as collections.

    0 讨论(0)
  • 2021-01-21 14:00

    If you generate the proxy using "Add Service Reference" in Visual Studio there is an "Advanced" button that brings up a dialog that allows you to specify what the return type should be (including Generic types).

    0 讨论(0)
  • 2021-01-21 14:04

    Remember - WCF is also an interoperable system, e.g. your other end of the wire could be a PHP or Java or Ruby client which will not be able to understand the .NET generic list!

    You can specify you want generic lists with the two options Andrew and Jimmie have mentioned - this works, if and only if you also use the DataContractSerializer (which is the default choice for WCF). However, if your service and/or data contract for whatever reason needs to use the XmlSerializer instead, then these settings won't help - your lists will be turned into arrays once again.

    So try these options shown, and if they work for you - great! But be aware that there are good reasons why your generic list might just have to be turned into a more interoperable array of objects.

    Marc

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