3

I'm trying to consume a WCF service that returns a custom list in the form List<StockData>.

Here's the method signature from IService.cs:

 [OperationContract]
     List<StockData> orderStockData(string compName1, string compName2, string compName3);

But when I try referencing it in my website through a service reference:

List<StockData> list = new List<StockData>();
list = myProxy.orderStockData(txtinput1.Text, txtinput2.Text, txtinput3.Text);

I get the following error:

Cannot implicitly convert type 'ServiceReference1.StockData[] ' to Systems.Collections.Generic.List

Any help with solving this would be great. Thanks!

SharpC
  • 6,974
  • 4
  • 45
  • 40
seeker
  • 6,841
  • 24
  • 64
  • 100
  • Why are you creating a `new List<>`, just to assign a different result to that reference? Just do `var list = myProxy....` – Jonathon Reinhart Mar 22 '13 at 05:23
  • @JonathonReinhart, that gets rid of the error, but I'm not being able to access the elements of list later! – seeker Mar 22 '13 at 05:33
  • Why not? What's not working? – Jonathon Reinhart Mar 22 '13 at 05:34
  • @JonathonReinhart: Ive placed both the code of both the service implementation and the proxy. The proxy now throws an index out of bounds error Iservice.cs :https://gist.github.com/KodeSeeker/5219192 Aspx.cs: https://gist.github.com/KodeSeeker/5219195 – seeker Mar 22 '13 at 05:39

4 Answers4

18

When you added the reference to your site then under DataType ==> Collection Type you specified System.Array, (which is default as well), that is why your proxy is returning you an array instead of list.

enter image description here

When adding reference to the web service go to advance and specify System.Collection.Generic.List and you will get the same return type as in your contract.

But if you don't want to do that you can still use the Array and convert it to List using ToList

EDIT:

Like:

List<StockData> list = new List<StockData>();
list=(myProxy.orderStockData(txtinput1.Text, txtinput2.Text, txtinput3.Text)).ToList();
Habib
  • 219,104
  • 29
  • 407
  • 436
  • How exactly do you pull up this screen? As in from where can I access it – seeker Mar 22 '13 at 05:30
  • How did you add the Service References ??usually, right click on the Service References under project in Visual studio, Add service reference, there you will find a button `Advanced`, from there you can find that screen. You can even click on your existing service reference and click update, see if you find the advanced button there. – Habib Mar 22 '13 at 05:38
  • @KodeSeeker, you can also check the edited part of the answer, where you can convert the array to List – Habib Mar 22 '13 at 05:39
  • 1
    @Habib- I tried the converting array to list part but it throws an error saying - `Cannot implicilty convert type Systems.Collections.Generic.List to Systems.Collections.Generic.List ` – seeker Mar 22 '13 at 05:43
  • Also when I click update, it automatically downloads the service information once again ,there is no `update` option. – seeker Mar 22 '13 at 05:45
  • @KodeSeeker, that is because the proxy generated its own class, Try if you can get to the above screen, there is an option to reuse types in all assembillies, check that as well. – Habib Mar 22 '13 at 05:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26689/discussion-between-habib-and-kodeseeker) – Habib Mar 22 '13 at 05:45
  • @KodeSeeker, remove the reference and try adding it again – Habib Mar 22 '13 at 05:45
  • I tried setting the return type to System.Collection.Generic.List in the advanced settings but I get this error again `Cannot implicilty convert type Systems.Collections.Generic.List to Systems.Collections.Generic.List ` – seeker Mar 22 '13 at 05:51
  • @KodeSeeker, check out this post http://stackoverflow.com/questions/8286775/wcf-common-types-not-reused – Habib Mar 22 '13 at 05:56
0

You should use IList<StockData>, since StockData[] implements this interface it should make everything work.

Also, you don't need to call new List<T>() as you are immediately overwriting it with another value.

Rhys Bevilaqua
  • 2,102
  • 17
  • 20
  • ` IList list; list=myProxy.orderStockData(txtinput1.Text, txtinput2.Text, txtinput3.Text);` Doesnt work either – seeker Mar 22 '13 at 05:32
0

You can change in the service reference settings if you want the proxy to use List<T> or T[].

To change the settings, right click the service reference in the project in solution explorer. Pick "Configure Service Reference". In "Collection type" pick System.Collections.Generic.List.

Sakthivel
  • 1,890
  • 2
  • 21
  • 47
0

I was looking into this post and I too faced similar error before but none of the above answers solved the issue.

What did work for me is using var like below

//List<StockData> list = new List<StockData>(); // no need of this

var list=myProxy.orderStockData(txtinput1.Text, txtinput2.Text, txtinput3.Text).Tolist();
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
gvs
  • 9
  • 1