0

我有一个简单的 WCF POST,它向服务发布一个简单的 XML。

这是服务合同。

[ServiceContract]
public interface ITest
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "PostTest")]
   Stream PostTest(Stream testInfo);  
}

和配置,没什么花哨的。

<system.serviceModel>
<services>
  <service name="T.Test" behaviorConfiguration="ServiceBehaviour">
    <!-- Service Endpoints -->
    <!-- Unless fully qualified, address is relative to base address supplied above -->
    <endpoint address ="" binding="webHttpBinding" contract="T.ITest" behaviorConfiguration="web">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
    </endpoint>
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

只要我不提及内容类型,当我从一个简单的客户端对其进行测试时,一切正常。

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";

req.ContentType = "文本/xml; charset=utf-8";

var xmlDoc = new XmlDocument { XmlResolver = null };
xmlDoc.Load("../../PostData.xml");
string sXml = xmlDoc.InnerXml;
Console.Write(sXml + Environment.NewLine + Environment.NewLine);

req.ContentLength = sXml.Length;
var sw = new StreamWriter(req.GetRequestStream());
sw.Write(sXml);
sw.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();

经过一些研究,我了解到这是soap 1.1 与soap 1.2 之间的兼容性问题。但是如何在服务器端解决这个问题?

4

2 回答 2

0

我认为这与 SOAP 版本问题无关,因为webHttpBinding它是 REST 绑定,而不是 SOAP 绑定

在请求上设置ContentType标头会告诉服务客户端必须以请求的内容类型进行响应。您实际响应的内容类型是什么?如果不是text/xml; charset=utf-8,那么这可以解释错误。

根据文档,默认响应ContentType是或webHttpBinding流响应。application/xmlapplication/octet-stream

如果您没有在 上指定任何contentType内容HttpWebRequest,fiddler 会告诉我ContentType标头设置为text/html, application/xhtml+xml, */*

通配符意味着任何响应类型都可以 - 包括application/octet-stream您的服务器正在响应的响应类型。这就是为什么如果您不设置内容类型它会起作用的原因。

一旦您将客户端ContentType标头设置为text/xml,它就不再与服务响应的内容匹配,因此您会收到错误消息。

如果您无法在客户端设置请求 ContentType,那么您必须在服务器上进行。这篇文章提供了一个关于如何做到这一点的链接

WCF REST WebService 内容类型错误

这提供了更多信息

http://msdn.microsoft.com/en-us/library/ee476510.aspx

于 2013-06-17T19:11:32.510 回答
0

这具有接受 WCF 的任何内容类型的确切解决方案 http://vivekcek.wordpress.com/2012/06/14/wcf-rest-service-accepting-raw-xml-webcontenttypemapper/

于 2013-06-17T21:38:56.833 回答