2013-09-26 44 views
2

我有一個方法後一個問題..WCF POST請求的內容類型text/xml的

這裏是我的接口

public interface Iinterface 
    { 
    [OperationContract] 
    [WebInvoke(Method = "POST", UriTemplate = "inventory?")] 
    System.IO.Stream inventory(Stream data); 
    } 

而且功能..

public System.IO.Stream inventory(System.IO.Stream data) 
    { 
    //Do something 
    } 

好,如果從客戶端發送帶有內容類型的text/plain或application/octet-stream的作品完美,但客戶端無法更改內容類型,並且他是text/xml,並且我獲取了錯誤信息。

The exception message is 'Incoming message for operation 'inventory' (contract  
'Iinterface' with namespace 'http://xxxx.com/provider/2012/10') contains an 
unrecognized http body format value 'Xml'. The expected body format value is 'Raw'. 
This can be because a WebContentTypeMapper has not been configured on the binding. 

有人能幫助我嗎?

謝謝。

+0

爲什麼客戶端不能更改它的內容類型?這只是一個HTTP請求頭。 – EkoostikMartin

+0

我問自己同樣的問題,但他說我不能做任何理由。 – bombai

+1

你不能期望發送和接收內容類型爲「text/xml」的原始數據流。我相信在這種情況下,您將不得不在服務上編寫另一種方法來接受xml,然後轉換爲流。 – EkoostikMartin

回答

4

正如錯誤所述 - 您需要一個WebContentTypeMapper來「告訴」WCF將傳入的XML消息作爲原始消息讀取。你會在你的綁定中設置映射器。例如,下面的代碼顯示瞭如何定義這種綁定。

public class MyMapper : WebContentTypeMapper 
{ 
    public override WebContentFormat GetMessageFormatForContentType(string contentType) 
    { 
     return WebContentFormat.Raw; // always 
    } 
} 
static Binding GetBinding() 
{ 
    CustomBinding result = new CustomBinding(new WebHttpBinding()); 
    WebMessageEncodingBindingElement webMEBE = result.Elements.Find<WebMessageEncodingBindingElement>(); 
    webMEBE.ContentTypeMapper = new MyMapper(); 
    return result; 
} 

http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx的帖子中有關於使用內容類型映射器的更多信息。

相關問題