1

在我的 Web 服务中,我想摆脱通过调用 ...service.svc?singleWsdl 生成的 WSDL 中生成的 ArrayOf... 定义。

目前定义看起来像(我尝试了使用 XmlArray 等的所有品种):

[DataContract]
public class Incident {...}

[CollectionDataContract(Name = "IncidentList", ItemName = "IncidentItem")]
public class IncidentList : List<Incident>
{
    public IncidentList()
        : base()
    {      }

    public IncidentList(IEnumerable<Incident> list)
        : base(list)
    {        }
}

[MessageContract]
public class IncidentsResponse
{
    [MessageBodyMember]
    public Incident[] Incidents { get; set; }

    [MessageBodyMember]
    public IncidentList IncidentList { get; set; }
}

当我获得 WSDL 时,我总是会收到(或多或少 - 取决于 Name 属性等):

<xs:element name="IncidentsResponse"> <xs:complexType><xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="IncidentList" type="tns:ArrayOfIncident"/><xs:element minOccurs="0" maxOccurs="1" name="Incidents" type="tns:ArrayOfIncident"/> </xs:sequence> </xs:complexType></xs:element>

我真正想要的是直接在元素内列出的类型,例如

<xs:element name="IncidentsResponse"> <xs:complexType> <Incidents><IncidentItem>...</IncidentItem><IncidentItem>...</IncidentItem> </Incidents> <IncidentList><IncidentItem>...</IncidentItem><IncidentItem>...</IncidentItem></IncidentList> </xs:complexType></xs:element>

因此,对实际数据的引用,而不是列表类型 (ArrayOf)。

有什么办法可以做到这一点?如果我得到正确的信息,CollectionDataContract 属性应该可以解决问题,但不知何故它不会......

尤其是当消费客户端是 Java 时,这种额外的间接方式会有点伤害,因为它会使代码膨胀

欢迎任何想法:-)

4

1 回答 1

0

this is intended since wcf can be used across language so it should support common types rather than only supporting .net specific type. still if u want to generate a proxy using svcutil with list then you can create it by using the sample svcutil wsdl /ct:System.Collections.Generic.List`1 or if you are using adding service reference then there is an advanced button , Click that button and select the System.Generic.List as your Collection. This will resolve the problem.. and having the array information will not harm anything so it can be left as it is.

于 2013-06-14T17:46:02.547 回答