WCF Rest XML EndPoint。(HTTPPOST)。反序列化外部對象

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


I have a Service contract which is supposed to accept a widget as an input parameter. The widget spec was given to me by a third party which will be using the contract. I made the appropriate data contracts and can use a unit test (which is using the WCF as a service reference) to serialize a widget and do the HTTP POST.

我有一個服務合同,它應該接受一個小部件作為輸入參數。小部件規范是由使用合同的第三方提供給我的。我制定了相應的數據協定,並且可以使用單元測試(使用WCF作為服務參考)來序列化窗口小部件並執行HTTP POST。

It is a bare bones XML serialization that is embedded in the body of the HTTPPOST. It is not a well formed xml document. e.g.

它是一個簡單的XML序列化,嵌入在HTTPPOST的主體中。它不是一個格式良好的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();

When the third party tries to use the contract there is an Error 400. They are using a fully formatted xml doc which has namespace declarations in it. e.g.

當第三方嘗試使用合同時,會出現錯誤400.它們使用的是完全格式化的xml doc,其中包含名稱空間聲明。例如

    <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>

The contract is:

合同是:

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

The Data Contract is;

數據合同是;

[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; }
    }
}

I have tried many options including an alternate contract that takes a string parameter and then used a DataContractSerializer to deserialize the widget.

我嘗試了很多選項,包括一個帶有字符串參數的備用契約,然后使用DataContractSerializer來反序列化窗口小部件。

Essentially I haven't a clue how to do this and am floundering around looking for the correct way. Would appreciate anything that can help me solve this. thanks

基本上我不知道如何做到這一點,我正在四處尋找正確的方法。我將不勝感激,可以幫助我解決這個問題。謝謝

1 个解决方案

#1


0  

If you can make a .net winform app and reference your service does that work? If that works you should be able to wireshark the requests to see what the difference is between your winform app and the third party's request.

如果您可以制作一個.net winform應用程序並參考您的服務是否可行?如果可行,您應該能夠查詢請求,以了解您的winform應用程序與第三方請求之間的區別。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/08/03/7209b1fe29f58a8932a0f70e48e7e358.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com