我在我的软件项目中使用 WCF 接口包装现有的 ASMX Web 服务作为过渡阶段,以节省时间。除了一个返回 System.String 的函数外,这很好用。
原始 ASMX 服务根据给定的参数返回文本或 XML。这在 ASMX 中不是问题。在 WCF 中,返回的值(如果是 XML)被转义为:<gml>
应该在哪里<gml>
。请参阅下面的 SOAP。
要求
POST http://someuri.org/WebServices/Utils.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: http://www.someuri.org/IUtils/Function
Content-Length: 283
Accept: */*
User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
Host: foo.bar.org
Connection: Keep-Alive
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Function xmlns="http://www.someri.org/">
<type>...</type>
<input1>...</input1>
<input2>...</input2>
<input3>true</input3>
</Function >
</s:Body>
</s:Envelope>
回复
HTTP/1.1 200 OK
Date: Fri, 04 May 2012 11:40:01 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 2070
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<FunctionResponse xmlns="http://www.crotec.nl/">
<FunctionResult><gml>data</gml></FunctionResult>
</FunctionResponse>
</s:Body>
</s:Envelope>
一些谷歌搜索让我返回一个 System.IO.Stream 对象。
string result = DoStuff(arg1, arg2, arg3);
byte[] bin = Encoding.UTF8.GetBytes(result);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new System.IO.MemoryStream(bin);
这在一定程度上有效。
string result = "02010415DBD800D7E17577787A626978";
byte[] bin = {48,50,48,49,48,52,49,53,68,66,68,56,48,48,68,55,69,49,55,53,55,55,55,56,55,65,54,50,54,57,55,56};
然而,SOAP 消息中的返回结果是:
MDIwMTA0MTVEQkQ4MDBEN0UxNzU3Nzc4N0E2MjY5Nzg=
所以结果输出是乱码(我认为,再次,由消息编码(?)引起)
该方法是带有 OperationContract 的 attr,并且该服务托管在 IIS6 中,具有以下 ABC:
<service name="WebServices.BeheerUtils" behaviorConfiguration="Services.ServiceBehavior">
<!-- Service Endpoints -->
<endpoint address="" binding="basicHttpBinding" contract="WebServices.IUtils"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
任何想法为什么输出是乱码或如何防止 HTML 编码?
界面
[OperationContract]
System.IO.Stream Function(string type, string input1, string input2, string input3);
执行
public new System.IO.Stream Function(string type, string input1, string input2, string input3)
{
// Call the old ASMX method
string result = DoStuff(type, input1, input2, input3, true);
byte[] bin = Encoding.UTF8.GetBytes(result);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new System.IO.MemoryStream(bin);
}