WCF合同首先基于xsd数据类型

时间:2012-08-16 11:26:12

标签: c# .net wcf xsd contract-first

我们正在将数据类型创建为XSD定义。然后将这些xsd文件导入WebSphere Application Server。

我们希望WebSphere Application Server能够使用这些数据类型调用WCF Web服务。

原始xsd如下:

    <xs:simpleType name="Stillingsprosent">
         <xs:restriction base="xs:double"/>
    </xs:simpleType>

    <xs:element name="gjennomsnittStillingsprosent" type="Stillingsprosent" minOccurs="0" maxOccurs="1"/>

通过xsd.exe运行此代码会生成以下C#代码:

public partial class GjennomsnittStillingsprosent {

private double gjennomsnittField;

private bool gjennomsnittFieldSpecified;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public double gjennomsnitt {
    get {
        return this.gjennomsnittField;
    }
    set {
        this.gjennomsnittField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool gjennomsnittSpecified {
    get {
        return this.gjennomsnittFieldSpecified;
    }
    set {
        this.gjennomsnittFieldSpecified = value;
    }
}
}

当我们在WCF合约中使用此数据类型时,它会生成以下xsd:

<xs:complexType name="GjennomsnittStillingsprosent">
 <xs:sequence>
  <xs:element name="gjennomsnittField" type="xs:double"/>
  <xs:element name="gjennomsnittFieldSpecified" type="xs:boolean"/>
 </xs:sequence>
</xs:complexType>
<xs:element name="GjennomsnittStillingsprosent" nillable="true" type="tns:GjennomsnittStillingsprosent"/>
  • 我们希望数据合约与​​原始xsd相同。
  • 我们也不想在生成后编辑文件。 (数据模型很大)

问题与minoccurs = 0的可选字段有关,这些字段实际上是可空的,但是在.net具有可空类型之前创建了xsd.exe。

我们如何确保WCF合同中使用的数据类型与XSD中指定的数据类型完全相同?

1 个答案:

答案 0 :(得分:0)

声明如下:

[DataContract]
public class Stillingsprosent{
[DataMember]
public double stillingsprosent;
}

然后在某个地方使用它:

[DataMember(EmitDefault=false)]
public Stillingsprosent? gjennomsnittStillingsprosent;

EmitDefault = false表示默认情况下不会发出它(即此处为null)

相关问题