我得到了一些 XSD:
<xs:complexType name="myFaultType">
<xs:sequence>
<xs:element name="type" type="mns:enumServiceException" />
<xs:element name="code" type="xs:integer" />
<xs:element name="message" type="xs:string" />
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other" />
</xs:sequence>
</xs:complexType>
<xs:element name="myFault" type="mns:myFaultType" />
<xs:simpleType name="enumServiceException">
<xs:restriction base="xs:string">
<xs:enumeration value="constraint" />
<xs:enumeration value="objectNotFound" />
<xs:enumeration value="permissionDenied" />
<xs:enumeration value="runtime" />
<xs:enumeration value="updateConflict" />
<xs:enumeration value="versioning" />
</xs:restriction>
</xs:simpleType>
XSD.exe 生成了这个(我的修正代码):
[GeneratedCode("xsd", "2.0.50727.3038")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(Namespace = "http://docs.oasis-open.org/messaging/200908/")]
[XmlRoot("myFault", Namespace = "http://docs.oasis-open.org/messaging/200908/", IsNullable = false)]
public sealed class myFaultType
{
[XmlElement(Order = 1)]
public enumServiceException type;
[XmlElement(Order = 2, DataType = "integer")]
public string code;
[XmlElement(Order = 3)]
public string message;
[XmlElement(Order = 4)]
public XmlElement[] any;
}
[GeneratedCode("xsd", "2.0.50727.3038")]
[Serializable]
[XmlType(Namespace = "http://docs.oasis-open.org/messaging/200908/")]
public enum enumServiceException
{
constraint,
objectNotFound,
permissionDenied,
runtime,
updateConflict,
versioning,
}
合同:
[XmlSerializerFormat]
public interface IObjectService
{
[FaultContract(typeof(myFaultType))]
DoSomeResponseMessage DoSome(DoSomeRequestMessage message);
}
问题:
1 - 这是工作室生成的 WSDL 部分(错误排序):
<xs:complexType name="myFaultType"><xs:sequence>
<xs:element name="any" nillable="true" type="q1:ArrayOfXmlElement"/>
<xs:element name="code" nillable="true" type="xs:string"/>
<xs:element name="message" nillable="true" type="xs:string"/>
<xs:element name="type" type="tns:enumServiceException"/></xs:sequence></xs:complexType>
2 - 在我的 IErrorHandler 中的 ProvideFault 代码
var exception = new FaultException<myFaultType>(new myFaultType(), "fault reason");
var messageFault = exception.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, "http://URL/v1-0/Fault");
生成该错误消息(错误的排序,一些默认命名空间而不是预期的“http://docs.oasis-open.org/messaging/200908/”):
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring xml:lang="ru-RU">fault reason</faultstring>
<detail>
<myFaultType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/SomeMyNamespace">
<any xmlns:d6p1="http://schemas.datacontract.org/2004/07/System.Xml" i:nil="true" />
<code>0</code>
<message>repositoryId - invalid format</message>
<type>invalidArgument</type>
</myFaultType>
</detail>
</s:Fault></s:Body>
我做错了什么?
似乎 WCF 并不关心 XmlSerializer 属性。