Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume you're using SOAP as the message format. In this case, the object you're serializing is not the root of the XML, the soap envelope is. So it makes sense that the XmlRoot would be ignored. By default WCF will create a message contract for you and name the response and it has the namespace of the service. What you can do is <a href="http://msdn.microsoft.com/en-us/library/ms730255.aspx" rel="nofollow noreferrer">create your own message contract</a> to have full control over SOAP.</p> <p>Create the following two classes:</p> <pre><code>[MessageContract] public class MyTestMethodRequest { [MessageBodyMember( Namespace = "http://datacontract" )] public MyType MyType; } [MessageContract] public class MyTestMethodResponse { [MessageBodyMember( Namespace = "http://datacontract" )] public MyOtherType MyOtherType; } </code></pre> <p>Then change the signature of your service operation to the following.</p> <pre><code>[OperationContract] public MyTestMethodResponse MyTestMethod( MyTestMethodRequest request ) { return new MyTestMethodResponse { MyOtherType = new MyOtherType { OtherStringValue = "bar" } }; } </code></pre> <p>Now if you example the SOAP messages you should see the following:</p> <p><strong>Request</strong></p> <pre><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;s:Header&gt; &lt;Action xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" s:mustUnderstand="1"&gt;http://servicecontract/TestService/MyTestMethod&lt;/Action&gt; &lt;/s:Header&gt; &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;MyTestMethodRequest xmlns="http://servicecontract"&gt; &lt;MyType StringValue="foo" xmlns="http://datacontract" /&gt; &lt;/MyTestMethodRequest&gt; &lt;/s:Body&gt; &lt;/s:Envelope&gt; </code></pre> <p><strong>Response</strong></p> <pre><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;s:Header /&gt; &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;MyTestMethodResponse xmlns="http://servicecontract"&gt; &lt;MyOtherType OtherStringValue="bar" xmlns="http://datacontract" /&gt; &lt;/MyTestMethodResponse&gt; &lt;/s:Body&gt; &lt;/s:Envelope&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload