title: WCF service, response in SOAP or plain XML, how?
body: <p>I am struggling few hours with this and can't find a solution to the communication problem.
  My service need to communicate with clients via SOAP or plain XML</p>
  
  <p>My service will be written based on WCF framework, however my clients not.</p>
  
  <p>Could you show me step by step how to change my service code and configuration in a way that it will return SOAP or XML message? I am interested in both solutions.</p>
  
  <p>I've tried to achieve this basing on this answers:</p>
  
  <p><a href="https://stackoverflow.com/questions/186631/rest-soap-endpoints-for-a-wcf-service">REST / SOAP endpoints for a WCF service</a>
  <a href="https://stackoverflow.com/questions/1477344/call-wcf-using-soap-call">Call WCF Using SOAP call</a></p>
  
  <p>But none of this solution worked properly for me. Either I got nothing in my browser or error that resource could not be found.</p>
  
  <p>So I've started a new WCF Service Project. It runs under <a href="http://localhost:3151/" rel="nofollow noreferrer">http://localhost:3151/</a> and have code like this:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.ServiceModel;
  using System.Text;
  
  namespace WcfService1
  {
      // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
      public class Service1 : IService1
      {
          public string GetData(int value)
          {
              return string.Format("You entered: {0}", value);
          }
  
          public CompositeType GetDataUsingDataContract(CompositeType composite)
          {
              if (composite.BoolValue)
              {
                  composite.StringValue += "Suffix";
              }
              return composite;
          }
      }
  }
  </code></pre>
  
  <p>Probably I'will need to create two endpoints? Both of them should contain <code>basicHttpBinding</code> as a binding parameter. But what else?</p>
  
  <p>Also, how to produce an XML like this (based on data contract):</p>
  
  <pre><code>&lt;CompositeType BoolValue = 0 StringValue = ""&gt;
  </code></pre>
  
  <p>rather than this:</p>
  
  <pre><code>&lt;CompositeType&gt;
    &lt;BoolValue&gt;0&lt;/BoolValue&gt;
    &lt;StringValue&gt;&lt;/StringValue&gt;
  &lt;/CompositeType&gt;
  </code></pre>
  
  <p>Please note that I need to as a two way communication so my service need to receive SOAP or XML and response in SOAP or XML.</p>
  
  <p>Also if this is possible I would like to see description of the service methods in my browser just like it was in ASP .NET. </p>
  
  <p>Thanks in advance for your time.</p>
  
  <p><strong>Very big update down here</strong></p>
  
  <p>I've tried to find some solution and here it is all what I gathered so far. I've focused only on plain XML.</p>
  
  <p>Lets say that the protocol looks like this:</p>
  
  <p>Message from a client:</p>
  
  <pre><code>&lt;MESSAGE MessageParam1 = 0&gt;
    &lt;AdditionalInfo&gt;Some message info &lt;/AdditionalInfo&gt;
  &lt;/MESSAGE&gt;
  </code></pre>
  
  <p>Response:</p>
  
  <pre><code>&lt;RESPONSE ResponseParam1 = 0&gt;
    &lt;AdditionalInfo&gt;Some response info&lt;/AdditionalInfo&gt;
  &lt;/MESSAGE&gt;
  </code></pre>
  
  <p>First of all, I would like to see description of current service methods and it params in the exact format as they will be send / received. What I mean is that when I was experimented with ASP .NET 2.0 WebServices when I invoked Service in my browser (just started the service) I've got such response:</p>
  
  <pre><code>POST /Service1.asmx HTTP/1.1
  Host: localhost
  Content-Type: text/xml; charset=utf-8
  Content-Length: length
  SOAPAction: "http://tempuri.org/ShowUser"
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soap:Body&gt;
      &lt;ShowUser xmlns="http://tempuri.org/"&gt;
        &lt;MyLogin&gt;
          &lt;User&gt;string&lt;/User&gt;
          &lt;Password&gt;string&lt;/Password&gt;
          &lt;Database&gt;string&lt;/Database&gt;
        &lt;/MyLogin&gt;
      &lt;/ShowUser&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  
  HTTP/1.1 200 OK
  Content-Type: text/xml; charset=utf-8
  Content-Length: length
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soap:Body&gt;
      &lt;ShowUserResponse xmlns="http://tempuri.org/"&gt;
        &lt;ShowUserResult&gt;string&lt;/ShowUserResult&gt;
      &lt;/ShowUserResponse&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>And this is GREAT. Apart from that this has SOAP envelope (underlying data is also different) but the point is that I can show it to the client and tell him: "you are doing HTTP POST method with such XML structure and you are receiving such answer". I want similar result with WCF but even simpler - without SOAP. So far, when I click on the Service1.svc I am getting a bunch of WSDL code which I don't want to. I know that this is great for some cases but I don't know what platform is using my client. And if he won't be using .NET than it will probably not import correctly the WSDL.</p>
  
  <p>So you know what I would like to get.</p>
  
  <p>This is what I did so far after two long days...:</p>
  
  <pre><code>namespace RESTService
  {
      // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
  
      [ServiceContract]
      [XmlSerializerFormat]
      public interface IService
      {
          [OperationContract]
          [WebGet]
          Response EchoWithGet(string s);
  
          [OperationContract]
          [WebInvoke]
          Response EchoWithPost(string s);
      }
  
      public class Response
      {
          [XmlAttribute]
          public int ResponseParam1;
          [XmlElement]
          public string AdditionalInfo;
      }
  }
  </code></pre>
  
  <p>As you see I did not provide Message class because I don't know how to pass it to the method via browser. I only know how to pass a string.</p>
  
  <p>And here is the implementation and config:</p>
  
  <pre><code>namespace RESTService
  {
      // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
      public class Service1 : IService
      {
          public Response EchoWithGet(string s)
          {
              Response Transaction;
              Transaction = new Response();
              Transaction.AdditionalInfo = "I've responded: " + s;
              return Transaction;
          }
          public Response EchoWithPost(string s)
          {
              Response Transaction;
              Transaction = new Response();
              Transaction.AdditionalInfo = "I've responded: " + s;
              return Transaction;
          }
      }
  }
  </code></pre>
  
  <p>The config is also a bit of mystery for me. I've tried to create binding as it was described in one of the topics from MSDN: <a href="http://msdn.microsoft.com/en-us/library/aa395208.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa395208.aspx</a></p>
  
  <p>but then after invoking a method I had returned a SOAP-like fault structure but without SOAP envelope.</p>
  
  <p>Anyway here is the config:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
        &lt;services&gt;
           &lt;service name="RESTService.Service1"&gt;
             &lt;host&gt;
             &lt;/host&gt;
             &lt;endpoint 
                 address="rest" behaviorConfiguration="webBehavior"
                 bindingConfiguration="" binding="webHttpBinding" 
                 contract="RESTService.IService"/&gt;
           &lt;/service&gt;
         &lt;/services&gt;
         &lt;behaviors&gt;
           &lt;endpointBehaviors&gt;
              &lt;behavior name="webBehavior"&gt;
                 &lt;webHttp /&gt;
              &lt;/behavior&gt;
           &lt;/endpointBehaviors&gt;
         &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>When I invoke a service method in my browser like this:</p>
  
  <p><a href="http://localhost:2443/Service1.svc/rest/EchoWithGet?s=test" rel="nofollow noreferrer">http://localhost:2443/Service1.svc/rest/EchoWithGet?s=test</a></p>
  
  <p>I got in my browser an XML file:</p>
  
  <pre><code>&lt;Response ResponseParam1="0"&gt;
  &lt;AdditionalInfo&gt;I've responded: test&lt;/AdditionalInfo&gt;
  &lt;/Response&gt;
  </code></pre>
  
  <p>So it looks like I am on the right track? However still I don't know how to receive a full information (like the one in ASP .NET 2.0 ) about what has been send and what has been received. I would like to rather not listen the service port with sniffer to look what is going through HTTP. I would like to see it. Also, when I look at the source file of the received XML I see this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;Response ResponseParam1="0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" &lt;br/&gt;
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;&lt;br/&gt;
  &lt;AdditionalInfo&gt;I've responded: test&lt;/AdditionalInfo&gt;&lt;/Response&gt;
  </code></pre>
  
  <p>I don't want to have this additional info in the returned XML, how to cut this?</p>
  
  <p>Another problem is with the second method:</p>
  
  <p><a href="http://localhost:2443/Service1.svc/rest/EchoWithPost?s=test" rel="nofollow noreferrer">http://localhost:2443/Service1.svc/rest/EchoWithPost?s=test</a></p>
  
  <p>This doesn't work as expected. I get the error message that "method is not allowed". How to resolve this?</p>
  
  <p>So, lets sum up this long question.</p>
  
  <p>I have some half-working solution which might be the correct way or is the wrong way. I would like to ask you to either finish it that it also could:</p>
  
  <ol>
  <li>Accept in method an XML file as a parameter.</li>
  <li>Show me how to call the method in web browser with XML as a parameter. Yes, I don't want to a separate client in .NET. Let the browser will be the client.</li>
  <li>Show me how to cut unnecessary bloated info in returned XML.</li>
  <li>Show me how to obtain .NET 2.0-like description of my service with bearing on mind that I don't want to have SOAP.</li>
  <li>Explain me what is wrong with EchoWithPost method. Why it doesn't work?</li>
  </ol>
  
  <p>If my example is bad, please provide me some basic example how it should be achieved.</p>
  
  <p>I am just learning .NET and WCF and I am very confused. All that problems only with setting up correct data exchange protocol... and I even haven't statred to write a real service :|</p>
  
  <p>Thanks a lot for your time and any future help.</p>