要将元数据(包括 xsd 方案)下载到当前目录,请使用:
svcutil /t:metadata http://.../.../service?WSDL
要生成客户端代码,请使用:
svcutil *.wsdl *.xsd /language:C# /ct:System.Collections.Generic.List`1
注意:*.wsdl 和 *.xsd,因为可能有很多服务定义和方案。
要一步生成客户端代码,请使用:
svcutil http://.../.../service?WSDL /ct:System.Collections.Generic.List`1
我回答这个问题可能有点晚了,但我会把它留在这里,以防其他人遇到同样的问题。
SvcUtil 不生成基于泛型的类的原因是因为它仅受 DataContract Serializer 支持,而不受 XML Serializer 支持。 DataContract Serializer 可能不支持您拥有的 WSDL/XSD。像拥有属性(xsd/xml 而不是“DataContractAttribute”)这样简单的事情将默认使用 XMLSerializer,因为 DataContracts 不支持属性。
请阅读以下内容:
“此外,用于引用类型的 /r 和 /ct 开关用于 生成数据合同。使用时这些开关不起作用 XmlSerializer。”
https://msdn.microsoft.com/en-us/library/aa347733(v=vs.110).aspx
我无法弄清楚为什么 /ct 开关在我的情况下无法正常工作。希望他们尽快修复它;同时,我将以下 powershell 命令添加到批处理文件中,以用列表替换生成的数组:
powershell -Command "(gc ServiceRef.cs) -replace 'ForsikringstilfelleDetalj\[\]', 'System.Collections.Generic.List<ForsikringstilfelleDetalj>' | Out-File ServiceRef.cs"
我知道这个问题有点老了,但我遇到了同样的问题并解决如下:
首先,NugGet 上提供了一个新的 svcutil:dotnet-svcutil,它可与较新的 .net 和核心框架配合使用。
其次,您需要指定要使用的集合类型,如之前的海报所示。但是,有两个重要问题需要考虑:
--reference
标志完成的。就我而言,我使用:--reference System.Collections
--collectionType "System.Collections.Generic.List``1"
。请注意 List
和 1
之间的 2 个反勾号。所以对我来说整个命令行命令看起来像这样:
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
svcutil xx.wsdl yy.xsd /ct:System.Collections.Generic.List
才有效。检查方法的返回类型以查看所有方法是否都是通用的。