WCF发布请求内容类型text / xml

时间:2013-09-26 14:44:11

标签: c# wcf post content-type

我的帖子方法有问题..

这是我的界面

    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,并且我获得了错误.. < / p>

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.

有人可以帮助我吗?

感谢。

1 个答案:

答案 0 :(得分: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上的帖子提供了有关使用内容类型映射器的更多信息。