I’m wrapping an existing ASMX webservice with a WCF interface as a transition phase in my software project to save time. This works well except for one function which returns a System.String.
The original ASMX service returned either text or XML depending on the parameters given. This was not a problem in ASMX. In WCF the returned value, if XML, is escaped like: <gml> where it should be <gml>. Please see the underneath SOAP.
Request
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>
Response
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>
Some googling brought me to returning a System.IO.Stream object.
string result = DoStuff(arg1, arg2, arg3);
byte[] bin = Encoding.UTF8.GetBytes(result);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new System.IO.MemoryStream(bin);
This works to a point.
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};
The returned result in the SOAP message is however:
MDIwMTA0MTVEQkQ4MDBEN0UxNzU3Nzc4N0E2MjY5Nzg=
So the resulting output is garbled (again, I think, caused by the message encoding(?))
The method is attr’s with an OperationContract and the service is hosted in IIS6 with the following 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>
Any ideas why the output is garbled or how to prevent the HTML encoding?
Interface
[OperationContract]
System.IO.Stream Function(string type, string input1, string input2, string input3);
Implementation
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);
}
So I fiddled around with some return types and this is what I can up with:
I basically wrap all responses in a new Root element. This gives me literal XML in the SOAP enveloped instead of ‘html encoded’. Though it does what I need, I find it awkward. However I found no other solution fitting my need so I present this as the final solution.