Why does WCF return myObject[] instead of List<T> like I was expecting?
Asked Answered
V

4

63

I am returning a List from my WCF method. In my client code, it's return type shows as MyObject[]. I have to either use MyObject[], or IList, or IEnumerable...

WCFClient myClient = new WCFClient();

    MyObject[] list = myClient.GetMyStuff();
or
    IList<MyObject> list = myClient.GetMyStuff();
or
    IEnumerable<MyObject> list = myClient.GetMyStuff();

All I am doing is taking this collection and binding it to a grid. What is the best object to assign my returned collection?

Vulturine answered 29/1, 2009 at 20:56 Comment(0)
T
83

You can specify that you want to use a generic list instead of an array by clicking the advanced button when you add a reference, or you can right click on the service reference and choose configure to change it in place.

The reason is that WCF serializes Generic lists as arrays to send across the wire. The configuration is just telling svcutil to create a proxy that converts them back to a generic list for your convenience.

Tom answered 29/1, 2009 at 21:3 Comment(2)
Can you do this within the service project itself, so that when you generate the WCF test client you get LIST<TYPE> instead of TYPE[] ?Feather
There is a way to configure that from the svcutil directly?, I need to create it that way by the svcutil because im serializing various webservices in one for the namespaces.. how can I configure that with the svcutil to return a list?Jamieson
T
13

When you use svcutil.exe to create you client code you need to tell it how to resolve certain references that are not available to it.

This is how you would do it for List<T>:

svcutil /o:YourService.cs /ct:System.Collections.Generic.List`1 http://example.com/mex
Triptych answered 29/1, 2009 at 21:0 Comment(1)
This is a good solution but doesn't handle the generic case of 'I'm using C# with C#, and do not need the extra steps of having to always manually resolve common collection types with the plethora of services I'm making and deploying'Feather
A
8

Stever B is correct. WCF tries really hard not to be coupled to .NET. You may want to allow a Java client to connect to your component. Arrays are interoperable. Generic .NET lists aren't.

However, you're more than welcome to create your own proxy class that will convert the array back into a List or anything else that you'd like. The nice thing about manually creating your own proxies is that you're in complete control of what they do.

Abomasum answered 29/1, 2009 at 21:16 Comment(0)
I
2

When you add the service reference to the client project click the advanced button and change collection type from array to what you want it to be...

Insignificance answered 29/1, 2009 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.