Nebula
Nebula

Reputation: 1057

WCF returned System.IO.Stream garbled

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: &lt;gml&gt; 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>&lt;gml&gt;data&lt;/gml&gt;</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);
}

Upvotes: 0

Views: 1080

Answers (4)

Nebula
Nebula

Reputation: 1057

So I fiddled around with some return types and this is what I can up with:

string result = DoStuff(type, input1, input2, input3, true);

XDocument d = new XDocument();
XDocument basis = new XDocument(new XElement("result"));

// Load the result into a XDocument, add the result as a value of the wrapper element if the format is invalid    
try
{
    d = XDocument.Parse(result);
    basis.Root.Add(d.Root);
}
catch (Exception)
{
    basis.Root.Value = result;
}    

// Return the XElement
return basis.Root;

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.

Upvotes: 0

empi
empi

Reputation: 15881

I cannot agree with this statement:

This was not a problem in ASMX. In WCF the returned value, if XML, is escaped like: &lt;gml&gt; where it should be <gml>.

I created a service with method:

[OperationContract]
string GetXml(string str);

public string GetXml(string str)
{
    return "<gml>" + str + "</gml>";
}

And then I call the service with generated WCF Client:

var client = new MyWcfServiceClient();
var result = client.GetXml("test");

The result is:

<gml>test</gml>

UPDATE

Just to ensure you that it's a standard way for xml to behave please perform the following test and check the value of respone.FunctionResult:

public class FunctionResponse
{
    public string FunctionResult { get; set; }
}

public void Test()
{
    var serialized = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<FunctionResponse>   
    <FunctionResult>&lt;gml&gt;data&lt;/gml&gt;</FunctionResult>
</FunctionResponse>";
    var ser = new XmlSerializer(typeof(FunctionResponse));
    using (var stringReader = new StringReader(serialized))
    {
        using (var xmlReader = new XmlTextReader(stringReader))
        {
            var response = ser.Deserialize(xmlReader);                    
        }
    }            
}

Upvotes: 1

to StackOverflow
to StackOverflow

Reputation: 124696

If your function returns a string, then it's normal that it should be encoded.

You could try declaring your function to return an XmlNode instead of a string.

Upvotes: 1

Chris
Chris

Reputation: 2481

Use basicHttpBinding instead of wsHttpBinding.

Upvotes: 0

Related Questions