Reputation: 331
We are trying to develop a web service which will replace an existing web service consumed by a client of ours. This meant that the SOAP response from the new web service in WCF has to match exactly with the SOAP response from the current web service which was a legacy service written in Java. We are having trouble in making the new service return the same SOAP response as the old one.
Initially, we started with the DataContract as structure for the response but since it was generating some additional tags we switched to MessageContract.
Here is the sample request and response with DataContAract as the attribute for request and response. We are facing two of issues with using DataContract –
The response is having two additional tags GetDataUsingDataContractReponse
, GetDataUsingDataContractResult
which we are not able to remove programmatically.
We were not able to get the BoolValue
property as a XML attribute, instead it shows up as a XML element.
We would greatly appreciate if you can share any ideas you have to work around these two issues. Below are the sample request and response using DataContract
.
Request with DataContract-
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:mem="http://schemas.datacontract.org/2004/07/MemoPayment">
<soapenv:Header/>
<soapenv:Body>
<tem:GetDataUsingDataContract>
<!--Optional:-->
<tem:composite>
<mem:BoolValue>true</mem:BoolValue> <!--Not able to make this value as XML Attribute. Instead, always shows up as XML Element -->
<mem:StringValue>Sam</mem:StringValue>
</tem:composite>
</tem:GetDataUsingDataContract>
</soapenv:Body>
</soapenv:Envelope>
Response with DataContract:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetDataUsingDataContractResponse xmlns="http://tempuri.org/">
<GetDataUsingDataContractResult xmlns:a="http://schemas.datacontract.org/2004/07/MemoPayment" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> à Extra tags showing up. Any way to remove them?
<a:BoolValue>true</a:BoolValue>
<a:StringValue>SamSuffix</a:StringValue>
</GetDataUsingDataContractResult>
</GetDataUsingDataContractResponse>
</s:Body>
</s:Envelope>
To have more control on the SOAP message, we have switched from DataContract
to MessageContract
and now the response is being rendered as expected with XML attributes and XML elements.
But, the issue with using MessageContract
is that all the XML elements are showing up as optional. Example – StringValue
in the below request. Again, we would greatly appreciate if you can suggest any work around from your experience.
Request with MessageContract:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:MsgRequest boolVal="true">
<!--Optional:-->
<tem:StringValue>Sam</tem:StringValue> à Always showing up as Optional. Not able to control from the code. Any way to remove the Optional attribute?
</tem:MsgRequest>
</soapenv:Body>
</soapenv:Envelope>
Response with MessageContract:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MsgResponse boolVal="true" xmlns="http://tempuri.org/">
<StringValue>SamSuffix</StringValue>
</MsgResponse>
</s:Body>
</s:Envelope>
Upvotes: 1
Views: 1147
Reputation: 7067
In the past, when our team needed complete control over WCF Service output, we used the XMLSerializer rather than data and/or message contracts.
WCF supports the XmlSerializer class, which is not unique to WCF. The XmlSerializer class supports a much narrower set of types than the DataContractSerializer class, but allows much more control over the resulting XML and supports much more of the XML Schema definition language (XSD) standard. It also does not require any declarative attributes on the serializable types. For more information, see the XML Serialization topic in the .NET Framework documentation.
Reference:
http://msdn.microsoft.com/en-us/library/ms733901(v=vs.110).aspx
Upvotes: 1