WCF Rest XML 终结点。(HTTPPOST)。反序列化外部对象

WCF Rest XML EndPoint.(HTTPPOST). Deserialise an external object

我有一个服务合同,它应该接受一个小部件作为输入参数。 小部件规范是由将使用合同的第三方提供给我的。 我制定了适当的数据契约并可以使用单元测试(使用 WCF 作为服务参考)来序列化小部件并执行 HTTP POST.

它是嵌入在 HTTPPOST 正文中的基本 XML 序列化。它不是格式正确的 xml 文档。 例如

 StringWriter sw = new StringWriter();
        XmlTextWriter xtw = new XmlTextWriter(sw);
        xtw.WriteStartElement("Widget");
        xtw.WriteElementString("DateTime", widget.DateTime);
        xtw.WriteStartElement("Status");
        foreach (MyServRef.Status status in widget.Status)
        {
            xtw.WriteStartElement("Status");
            xtw.WriteElementString("Bay", status.Bay.ToString());
            xtw.WriteElementString("CurrentState", status.CurrentState.ToString());
            xtw.WriteElementString("Sector", status.Sector.ToString());
            xtw.WriteElementString("SpaceId", status.SpaceId.ToString());
            xtw.WriteElementString("StatusId", status.StatusId.ToString());
            xtw.WriteElementString("TransitionDateTime", status.TransitionDateTime);
            xtw.WriteEndElement();
        }
        xtw.WriteEndElement();
        xtw.WriteElementString("UniqueId", message.UniqueId.ToString());
        xtw.WriteEndElement();
        xtw.Close();

        string xml = sw.ToString();

当第三方尝试使用合同时出现错误 400。他们使用的是完全格式化的 xml 文档,其中包含名称空间声明。 例如

    <Widget xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    UniqueId="8cf4df0f-e765-4afc-b6fa-cd90b07b67b2"
    DateTime="2015-08-02T01:00:29.2213364Z" xmlns="http://www.thesite.com/">
      <Status>
        <SpaceId>00010003</SpaceId>
        <StatusId>1658038</StatusId>     
      </Status>
    </Widget>

合同是:

 [OperationContract]//Don't decorate XML REST Contracts use   web.config       
    Response MyContract(Widget widget);

数据合同是;

[DataContract(Namespace = "")]
public class Widget
{


    private string _uniqueIdField;

    private string _dateTimeField;



    [DataMember]

    public Status[] Status
    {
        get;
        set;

    }

    [DataMember]
    public string UniqueId
    {
        get { return _uniqueIdField; }
        set { _uniqueIdField = value; }
    }

    [DataMember]
    public string DateTime
    {
        get { return _dateTimeField; }
        set { _dateTimeField = value; }
    }
}

我尝试了很多选项,包括采用字符串参数的备用合同,然后使用 DataContractSerializer 反序列化小部件。

基本上我不知道如何做到这一点,并且正在四处寻找正确的方法。 将不胜感激任何可以帮助我解决这个问题的东西。 谢谢

如果您可以制作一个 .net winform 应用程序并引用您的服务,那行得通吗?如果可行,您应该能够对请求进行 wireshark,以查看您的 winform 应用程序与第三方请求之间的区别。