【问题标题】:WCF Post Request content type text/xmlWCF 发布请求内容类型 text/xml
【发布时间】:2013-09-26 14:44:10
【问题描述】:

我的 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.

有人可以帮助我吗?

谢谢。

【问题讨论】:

  • 为什么客户端不能改变它的内容类型?它只是一个 HTTP 请求头。
  • 我问自己同样的问题,但他说我无论如何都做不到..
  • 您不能期望发送和接收内容类型为text/xml 的原始数据流。我相信在这种情况下,您将不得不在您的服务上编写另一个方法来接受 xml,然后转换为流。
  • @EkoostikMartin,您可以text/xml(以及application/json)发送到接收原始数据流的操作。您只需要设置适当的内容类型映射器。

标签: c# wcf post content-type


【解决方案1】:

正如错误所说 - 您需要 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 的帖子包含有关使用内容类型映射器的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-08
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多