2

我的post方法有问题..

这是我的界面

    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.

有人可以帮助我吗?

谢谢。

4

1 回答 1

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上的帖子包含有关使用内容类型映射器的更多信息。

于 2013-09-26T17:49:45.803 回答