0

I created a WCF Service that I'm passing a stream to on a request. The client side code looks like this:

  FileInfo fo = new FileInfo("c:/Downloads/test.xml");
        StreamWriter wo = fo.CreateText();

        XmlDocument MyXmlDocument = new XmlDocument();
        MyXmlDocument.Load("C:/DataFiles/Integrations/RequestXML.xml");
        byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(MyXmlDocument.OuterXml);

        Uri uri = new Uri("http://localhost:63899/MyRESTServiceImpl.svc/Receive");

        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);

        Request.ContentLength = RequestBytes.Length;

        Request.Method = "POST";

        Request.ContentType = "text/xml";

        Stream RequestStream = Request.GetRequestStream();
        RequestStream.Write(RequestBytes, 0, RequestBytes.Length);
        RequestStream.Close();

        HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string r = reader.ReadToEnd();
        //XmlDocument ReturnXml = new XmlDocument();
        //ReturnXml.LoadXml(reader.ReadToEnd());
        response.Close();

        wo.Write(r);

Right now, all I want to do is process the request and then return the XML right back to the client for testing purposes. Here are my IMyRESTServiceImpl.cs and MyRESTServiceImpl.svc.cs code respectively:

[ServiceContract]
public interface IMyRESTServiceImpl
{
    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
    Stream Receive(Stream text);
}


public class MyRESTServiceImpl : IMyRESTServiceImpl
{

    public Stream Receive(Stream text)  
        {  
            string stringText = new StreamReader(text).ReadToEnd();  

            return text;
        }   

}

Basically what's happening is that the API is returning my XML to me in string tags and using HTML encoding for the < and > signs (& gt; & lt;). I need it to just return the XML to me exactly as it was sent. I've debugged it and the XML remains intact on the server side so this is occurring as it's sent back. Any ideas on how to handle this? Thanks.

1 Answer 1

2

The implementation you have doesn't compile - the method is declared to return a Stream, but it's returning a String. If you return as a string, it will encode the XML characters; if you don't want the encoding, return it either as a Stream or as a XmlElement (or XElement).

Update with example

This is an example of a method returning a Stream for an arbitrary XML response:

[WebGet]
public Stream GetXML()
{
    string theXml = @"<products>
  <product name=""bread"" price=""1.33">
    <nutritionalFacts>
      <servings>2</servings>
      <calories>150</calories>
      <totalFat>2</totalFat>
    </nutritionalFacts>
  </product>
  <product name=""milk"" price=""2.99">
    <nutritionalFacts>
      <servings>8</servings>
      <calories>120</calories>
      <totalFat>5</totalFat>
    </nutritionalFacts>
  </product>
</products>";
    WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
    MemoryStream result = new MemoryStream(Encoding.UTF8.GetBytes(theXml);
    return result;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but how do you send it back as a stream? This is where I'm stuck. I've scoured the internet for a working example but have found nothing. Using string was the only way I could get the XML to send back. When attempting to send it back as a Stream nothing is received on the client side.
I added an example in the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.