2013-09-26 38 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的帖子中有关于使用内容类型映射器的更多信息。

相关问题