2011-12-28 31 views
0

我所有的GET端點都像冠軍一樣工作,但我試圖實現webinvoke方法=「POST」。來自WCF網絡調用的錯誤請求

我認爲我的格式有問題,但我不能告訴它是什麼,有人可以幫忙嗎?

[ServiceContract] 
interface iFlowRate 
{ 
    [OperationContract] 
    [WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}",RequestFormat= WebMessageFormat.Xml)] 
    string AddFlowRate(string apikey,FlowRate flowrate); 
} 

當我調試它時,它甚至不會進入此方法。 我打電話給這樣的服務。

string postData = "<FlowRate ><wellname>wellname</wellname></FlowRate>"; 
//Setup the http request. 
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
request.Method = "POST"; 
request.ContentLength = postData.Length; 
request.ContentType = "application/xml"; 
request.KeepAlive = true; 

StreamWriter streamwriter = new 
StreamWriter(request.GetRequestStream()); 
streamwriter.Write(postData); 
streamwriter.Close(); 

// Get the response. 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
// Read the response 
StreamReader responsereader = new StreamReader(response.GetResponseStream()); 
string strResponseData = responsereader.ReadToEnd(); 

任何想法?順便說一句,使用WCF 4.0,非常感謝任何幫助。

+1

什麼是您嘗試POST的URL? – 2011-12-28 13:54:10

+0

另外,如何定義FlowRate類? – carlosfigueira 2011-12-28 17:36:20

+0

url = localhost:4369/FlowRate/Add?apikey = 32q13e4-c78a-ce9d-e011-15eacd8e8958「; {[DataContract] public class FlowRate {[DataMember] public string wellname {get; set;}}} – Matt 2011-12-28 20:02:13

回答

0

直到不​​久之前,我終於徹底殺死了我,我終於偶然發現了答案。

這裏是我的發現來源:Wrapped BodyStyle in WCF Rest

但我會剪到好東西。

首先,設置ServiceContract的名稱空間。

[ServiceContract(Namespace = "http://mytagservice")] 

現在,我敢肯定還有另一種方法可以使這個工作,但這是我如何入侵它。將BodyStyle設置爲Wrapped。默認的請求格式是XML,所以如果你不想在這裏設置它的話。

[WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}", BodyStyle = WebMessageBodyStyle.Wrapped)] 

然後改變你的XML,包括包裝和命名空間。請特別注意標籤名稱,因爲它們區分大小寫。

string postData = "<AddFlowRate xmlns='http://mytagservice'><flowrate><wellname>wellname</wellname></flowrate></AddFlowRate>"; 

因爲它使用的包裝的消息類型此解決方案能夠儘可能多的參數工作,你可以想。