首先是WCF合同:complexType已经被声明

时间:2015-02-09 12:08:29

标签: c# wcf wsdl svcutil.exe design-by-contract

我正在使用svcutil.exe根据来自外部源的合同生成服务端点。

它似乎按预期工作,但服务无法将端点暴露给其他人。

具体来说,当我尝试为服务获取WSDL时,会抛出异常并出现以下错误:

  

System.InvalidOperationException:调用时抛出异常   WSDL导出扩展:   System.ServiceModel.Description.XmlSerializerOperationBehavior   合同:   http://tempuri.org/xml/wsdl/soap11/DistributionService/1/port:DistributionReceiverWebServicePort   ----> System.Xml.Schema.XmlSchemaException:complexType'http://tempuri.org/xml/wsdl/soap11/DistributionService/1/types:FejlType'   已经宣布。

但是 - 类型FejlType类型只声明一次,是一个非常简单的类:

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/xml/wsdl/soap11/DistributionService/1/types")]
[System.Runtime.Serialization.DataContractAttribute(Name = "FejlType", Namespace = "http://tempuri.org/xml/wsdl/soap11/DistributionService/1/types")]
public class FejlType : object, System.Runtime.Serialization.IExtensibleDataObject
{
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
    private string FejlKodeField;
    private string FejlTekstField;

    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get {return this.extensionDataField;}
        set {this.extensionDataField = value;}
    }

    [System.Runtime.Serialization.DataMemberAttribute(IsRequired = true, EmitDefaultValue = false)]
    public string FejlKode
    {
        get {return this.FejlKodeField;}
        set {this.FejlKodeField = value;}
    }

    [System.Runtime.Serialization.DataMemberAttribute(IsRequired = true, EmitDefaultValue = false)]
    public string FejlTekst
    {
        get {return this.FejlTekstField;}
        set {this.FejlTekstField = value;}
    }
}

我在这里完全处于黑暗中 - 为什么WCF在导出WSDL时会抱怨这个?

1 个答案:

答案 0 :(得分:3)

我找到了问题的原因。

仅供参考:WSDL起源于KOMBIT(丹麦政府IT-thingy),WSDL是其数据中心(Serviceplatformen)的一部分

我发现svcutil.exe生成的服务类的类型FejlType实现了两次 - 都是部分类,顺便说一句。其中一个具有特定的.net名称空间前缀,另一个没有自己的前缀。导致问题的原因是类型上的XmlTypeAttribute - 每个类型都暴露了相同的xml命名空间。因此,即使服务类确实编译,它们也暴露了相同的xml类型规范 - 导致我在此处报告的异常。