我有一個簡單的基於Windows服務的WCF REST客戶端(基於WebServiceHost())。我可以從GET操作接收數據,並在PUT操作中調用正確的方法,但傳遞的json不會被反序列化爲.NET對象。我總是將傳入的對象變爲空。WFC Rest服務PUT與JSON - 收到的對象始終爲空
接收方法有兩個參數,一個來自uri,另一個來自接收的json數據。關鍵正確地進入我的方法。
我打開日誌記錄,但沒有任何反序列化錯誤。提琴手很高興,它收到了200響應。我沒有看到有什麼有趣的東西來看看電線上發生了什麼。
我一直在看着圍繞這個問題的其他帖子,以及我能看到的東西,我已經正確地設置了一些東西。顯然這不是真的,但我看不出有什麼缺失/錯誤。如果只有更多的診斷信息出現,這將會容易得多。我將配置文件調試設置爲冗長,但我沒有在實際的消息反序列化過程中看到太多內容。
有沒有辦法進入實際的反序列化過程進一步調試?
或者我的設置在下面有詳細說明嗎?
服務合同,Send()
方法具有兩個參數,一個key
和message
對象:
[ServiceContract]
public interface ILoader
{
[OperationContract]
[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Send/{key}")]
string Send(string key, MessageBodyWrapper message);
}
我對消息包裝對象數據契約設置:
[DataContract(Name = "MessageWrapper")]
public class MessageBodyWrapper
{
private string _messageBody;
private string _state;
public MessageBodyWrapper()
{
_messageBody = "un-initialized";
}
[DataMember(Name="Message")]
public string Message
{
get { return _messageBody; }
set { _messageBody = value; }
}
public override string ToString()
{
JavaScriptSerializer js = new JavaScriptSerializer(); // Available in System.Web.Script.Serialization;
return js.Serialize(this);
}
}
接口
public class Loader : ILoader
{
private string _state;
public Loader()
{
_state = "initialzied";
}
public string Send(string key, MessageBodyWrapper Message)
{
string ret = "Send here -- Message";
return (ret);
}
}
我使用招送出樣品PUT
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:8080
Content-Length: 48
{"Message":"sdsdfsdf"}
我向我的MessageBodyWrapper()類添加了ToString()方法。然後在我的主體中創建一個新對象並序列化它。然後我用Fidder作爲另一個測試,但這並沒有幫助。我真的認爲我的配置存在一些問題,我無法發現。 –