Tony_Henrich
Tony_Henrich

Reputation: 44205

Why does adding a typed dataset in a WCF service force the client to use arrays for collections?

I have a WCF service and a WCF client. The service uses methods which return generic lists and the client's service reference collection type is set to generic list. Everything works fine.

I added a method to the WCF service which returns a typed dataset. First operation contract to return a dataset. Updated the service reference in the client and it broke everything. All the lists were converted to arrays even though the collection type is set to generic list.

I thought something went bad so I deleted the service reference, reran VS, created the service reference from scratch and still all the collections have been converted to arrays instead of lists.

I confirmed that the dataset caused this because when I removed the new method from the WCF service and updated the reference, all the collections went back to lists.

I need to use the dataset as is. How can I use it and leave the rest of the lists intact? Why is the typed dataset interfering with the type of the rest of the collections being used? I am using VS 2013 update 2 with .NET 4.5.1.

enter image description here

Upvotes: 0

Views: 575

Answers (2)

Tony_Henrich
Tony_Henrich

Reputation: 44205

I moved the service method to a legacy web service (asmx) service.

Upvotes: 0

tom redfern
tom redfern

Reputation: 31780

The reason for this is that when you add the service reference via visual studio, under the hood, Visual Studio calls svcutil.exe to generate your proxy. This is good, because as your screen shot shows, svcutil is able to interpret collections on the exposed service definition wsdl as List<T>.

This generally works well for simple contracts.

Now, for an obscure reason known only to Microsoft, svcutil has a rather fussy interpretation of XSD, which means that for service definitions which fall outside this interpretation, VS will fall back on using good old xsd.exe, which happens to NOT support collections as List<T>.

Generally you don't notice this because most service definitions are relatively simple and adhere to the rules. Dumping something like a .net DataSet object into the service contract however, and you're service definition becomes so heinously complex that VS is going to have to go with using xsd.exe to generate your code.

And therein lies the problem.

Some things you can try:

  1. Extract out the XSD from the service WSDL and try calling svcutil directly: svcutil /o:file.cs /ct:System.Collections.Generic.List schema.xsd (probably won't work but I would try just in case)

  2. Modify your generated code manually, replacing all arrays with Lists (ouch)

  3. Don't dump a .net dataset into your service definition (this is my favorite)

  4. Use a tool like XsdArrayToList - this will allow you to re-gen your gen'd code.

Upvotes: 1

Related Questions