0

Can we send a Generic List ( List) as a parameter to a WCF OperationContract?

Seems like the only way to do it is to encapsulate the List as a DataMember inside another Class and specify the class as a DataContract:

But that doesn't look right to me. Is there any other way?

EDIT1:
intended Signature:

[OperationContract]  
List<int> OperationName(List<CustomObject> objects);  

This translates to CustomObject[] at the client. I am currently passing CustomObject[] from my client and it works fine, but I want to know why I can not pass

  List <CustomObject> 

which gives me a compile error saying there is no overloaded version of the function which takes the specified parameters (type mismatch error)

EDIT 2:
Related Questions:
1) I should be able to control this from the service itself. What if I am exposing my service to the entire world and the wsdl/Proxy is the only way they would know of the signature of my OperationContract?
2) What if I want to use both System.Array and System.Generic.List in different Operation Contracts in the same Service Contract?

Gadam
  • 2,674
  • 8
  • 37
  • 56
  • can you share the intended signature ofthe opertaion contract that didnt work? – Dhawalk Mar 26 '13 at 16:28
  • Edited the question with signature*, Thanks – Gadam Mar 26 '13 at 16:50
  • This question is very similar to what's posted at http://stackoverflow.com/questions/8445502/list-vs-arrays-in-a-generated-proxy-class-in-c-sharp – Dhawalk Mar 26 '13 at 16:59
  • Thanks @Dhawalk. The link you posted suggests to change the settings on the client side service reference. I might be able to use this solution. But the broader question still remains: 1) I should be able to control this from the service itself. What if I am exposing my service to the entire world and the wsdl/Proxy is the only way they would know of the signature of my OperationContract? 2) What if I want to use both System.Array and System.Generic.List in different Operation Contracts in the same Service Contract? ... I feel This is not exactly neat. – Gadam Mar 26 '13 at 17:48
  • i would recommend that you edit your question to include the 2 mor questions you have raised – Dhawalk Mar 26 '13 at 18:20
  • The service "controls" the definition of the service (the wsdl), but how the client implements the proxy is up to the client(s). And don't forget that when using WCF you are exchanging data and not passing objects around. If the data sent by the client will fit List then it will work, otherwise it will fail. Why do you want to control how the client implements an enumeration of T? And what if the client is implemented in say Java? – Jocke Mar 26 '13 at 18:21

1 Answers1

2

List is an advanced type and may not be available to all the programming paradigms. Array, in comparision is perhaps available in all the programming paradigm. hence by default the translation will fall to array for a proxy wsdl

Now in this case, If you are sure that your client is .Net, you can change the client to use List. Since the service does not know what client's programming language is, the current design of svcutil where client decides whether it uses list or array is correct. If you make this setting available in service, you are ruling out clients which does not have the concept of List

Coming to your second question, as long as your operation contract names are different, you will be able to use the array and list distictly in the same service. Also note, OOP concepts are restricted in SOA, which means you can't have polymorphic methods.

the following question has more details about OOP and SOA

WCF Object Design - OOP vs SOA

Community
  • 1
  • 1
Dhawalk
  • 1,257
  • 13
  • 28
  • Very Good explanation, thanks. "...Now in this case, If you are sure that your client is .Net, you can change the client to use List" ... is there anyway my .Net client can know he can use List without me telling him? I think one would expect something like that. Anyway, thanks much for putting my many demons to rest... for now ;) – Gadam Mar 26 '13 at 18:51