WCF restful xml testing failed using postman RRS feed

  • Question

  • I have a very simple WCF Rest Service 

    Interface as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using HtmlAgilityPack;
    using System.Web;


    namespace WcfServiceLibrary1

        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        public interface IService1

            //string  ExtData(object xml);
            string ExtData(string xml);



    Service Contract as follows:


     public class Service1 : IService1

            [WebInvoke(Method = "POST",
                        RequestFormat = WebMessageFormat.Xml,
                       ResponseFormat = WebMessageFormat.Xml,
                        UriTemplate = "ExtData")]
           // public string ExtData(string xml)
            public string ExtData(string xml)
                return "OK";


    Config File for self hosted WCF Rest Service is as follows:

    <?xml version="1.0"?>
        <compilation debug="true"/>
      <!-- When deploying the service library project, the content of the config file must be added to the host's 
      app.config file. System.Configuration does not support config files for libraries. -->
            <binding name="MyBinding1" maxBufferSize="65536" maxReceivedMessageSize="65536">
              <readerQuotas maxStringContentLength="65536" maxBytesPerRead="65536" />
          <remove scheme="http" />
          <add scheme="http" binding="webHttpBinding" />
          <remove scheme="net.tcp" />
          <remove scheme="net.pipe" />
          <remove scheme="net.msmq" />
          <service name="WcfServiceLibrary1.Service1">
            <endpoint address="http://localhost:8703" binding="webHttpBinding"
              bindingConfiguration="MyBinding1" name="TestEndpoint" contract="WcfServiceLibrary1.IService1">
                <dns value="localhost" />
                <add baseAddress="http://localhost:8703" />
              <!-- 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"/>
      <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

    When I test using postman or python script I have to wrap the xml in a <string ..... xml ..... </string> and use the &lt and &gt to get it to work ... so obviously the contract is expecting a string ... 

    How would I get this to read a simple <TestData>HELLO</TestData> xml sent from PostMan ... or any where without the string wrapper ... 

    When I test the service using WCF post as below .. everything is fine ... but external systems all fail because not encapsulating as string ... Help .. Please ... what data type or settings do I need to put into the Operations Contract to get it to read "non" encapsulated xml strings?

            public static string CallAgent(string msg, string agentServiceUrl)
                string resourceUrl = "/ExtData";
                string method = "POST";
                string reply = UseHttpWebApproach(agentServiceUrl, resourceUrl, method, msg);
                return reply;
            private static string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string requestBody)
                string responseMessage = null;
                        var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
                        request.Timeout = 2000;
                        if (request != null)
                            request.ContentType = "text/html";
                             request.Method = method;

                        if (method == "POST" && requestBody != null)

                            byte[] reqBytes = ToByteArrayUsingXMLContractSer(requestBody);
                            request.ContentLength = reqBytes.Length;

                            Stream newStream = request.GetRequestStream();
                                newStream.Write(reqBytes, 0, reqBytes.Length);


                        if (request != null)
                            var response = request.GetResponse() as HttpWebResponse;
                            if (response.StatusCode == HttpStatusCode.OK)
                                Stream responseStream = response.GetResponseStream();
                                if (responseStream != null)
                                        var reader = new StreamReader(responseStream);
                                        responseMessage = reader.ReadToEnd();
                                responseMessage = response.StatusDescription;
                catch (Exception ex)
                    Console.WriteLine("UseHttpWebApproach error: " + ex.Message);
                    responseMessage = ex.Message;
                return responseMessage;

            private static byte[] ToByteArrayUsingJsonContractSer(string requestBody)
                byte[] bytes = null;
                var serializer1 = new DataContractJsonSerializer(typeof(string));
                var ms1 = new MemoryStream();
                serializer1.WriteObject(ms1, requestBody);
                ms1.Position = 0;
                var reader = new StreamReader(ms1);
                bytes = ms1.ToArray();
                return bytes;
            private static byte[] ToByteArrayUsingXMLContractSer(string requestBody)
                byte[] bytes = null;
                var serializer1 = new DataContractSerializer(typeof(string));
                var ms1 = new MemoryStream();
                serializer1.WriteObject(ms1, requestBody);
                ms1.Position = 0;
                var reader = new StreamReader(ms1);
                bytes = ms1.ToArray();
                return bytes;

    Monday, September 22, 2014 10:38 PM