我有一个简单的 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 之间的兼容性问题。但是如何在服务器端解决这个问题?