Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65461

Convert sequences to Generic Lists with svcutil

I am trying to auto generate some code with svcutil. I have the following within an xsd:

<xs:complexType name="ForsikringstilfelleDetalj">
    <xs:sequence>
        <xs:element name="detaljID" type="xs:string" minOccurs="0" maxOccurs="1"/>

I then run the following command:

svcutil xx.wsdl yy.xsd  /ct:System.Collections.Generic.List`1

I then get the following C# code:

public ForsikringstilfelleDetalj[] forsikringstilfelle

What I wanted was:

public List<ForsikringstilfelleDetalj> forsikringstilfelle

According to the info on the net the /ct option should tell svcutil to use generic lists for collections. Can anyone see what I am doing wrong?

Upvotes: 4

Views: 7595

Answers (5)

Carlo Bos
Carlo Bos

Reputation: 3293

I understand this question is a little old, but I was encountering the same problem and solved it as follows:

First, there is a new svcutil available on NugGet here: dotnet-svcutil that works with the newer .net and core frameworks.

Second, you need to specify the collection type you want to use as previous posters have indicated. However, there are 2 important concerns to consider:

  1. Specify the library reference that contains the collection type you want to use. This is done with the --reference flag. In my case I used: --reference System.Collections
  2. Specify the collection correctly with the namespace. Remember you need to correctly escape special characters. In my case I used: --collectionType "System.Collections.Generic.List``1". Note the 2 back ticks between List and 1. If, however, you have this command line in a batch file, you only need a single back-tick. The `1 one denotes a generic type with one type parameter.

So the whole command line command for me looked like this:

dotnet-svcutil  https://<your service url>.svc?wsdl --noLogo --reference System.Collections --collectionType "System.Collections.Generic.List``1" --outputDir proxy --internal --namespace *,<your name space for the generated proxy class> --outputFile <your proxy class name>.cs

Upvotes: 1

Suraj Verma
Suraj Verma

Reputation: 21

svcutil xx.wsdl yy.xsd /ct:System.Collections.Generic.List will only work if you have a return type of List or a generic class. Check your methods' return types to see whether all are generic or not.

Upvotes: -1

Cyberpass
Cyberpass

Reputation: 1155

I may be a little late to answer this question, but I'll leave it here in case someone else has the same issue.

The reason SvcUtil is not generating Generics based classes is because it is only supported with the DataContract Serializer and not the XML Serializer. The WSDL/XSD you have is probably not supported by the DataContract Serializer. Something as simple as having an attribute(xsd/xml not "DataContractAttribute") will default to using the XMLSerializer since DataContracts do not support attributes.

Read the following:

"In addition, the /r and /ct switches for referencing types are for generating data contracts. These switches do not work when using XmlSerializer."

https://msdn.microsoft.com/en-us/library/aa347733(v=vs.110).aspx

Upvotes: 3

J W
J W

Reputation: 135

I could not figure out why the /ct switch does not work properly in my case. Hopefully, they fix it soon; meanwhile, I added the following powershell command to the batch file to replace the generated array with a List:

powershell -Command "(gc ServiceRef.cs) -replace 'ForsikringstilfelleDetalj\[\]', 'System.Collections.Generic.List<ForsikringstilfelleDetalj>' | Out-File ServiceRef.cs"

Upvotes: 0

  • To download metadata (incluing xsd schemes) to the current directory use:

    svcutil /t:metadata http://.../.../service?WSDL
    
  • To generate the client code use:

    svcutil *.wsdl *.xsd /language:C# /ct:System.Collections.Generic.List`1
    

    Note: *.wsdl and *.xsd because there could be a lot of service definitions and schemes.

  • To generate the client code in one step use:

    svcutil http://.../.../service?WSDL /ct:System.Collections.Generic.List`1
    

Upvotes: 5

Related Questions