My C#.NET project has to talk to an external Java-platform web service (Sonic ESB). For development and testing, I have a mock service running in SOAP UI. The webservice shares an object called “ShipmentInformationMessage” that my code has to instantiate and fill with data and then pass it onto the webservice.
When I, after some time, got it to work together, I noticed that the request messages logged by SOAP UI had the following format:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<ShipmentInformationMessage
xmlns="http://www.noneofyour.biz/message/Transportation/2011/01">
<SenderId>NOSOTROS</SenderId>
<RecipientId>PARTNER</RecipientId>
<CreationTimeStamp>2011-08-03T11:53:36.6505521+02:00</CreationTimeStamp>
<Version>2.0</Version>
<TestIndicator>true</TestIndicator>
<ControlParty>
<Name xmlns="http://www.noneofyour.biz/schema/Common/2011/01">PrimaryContact</Name>
<Contact xsi:nil="true" xmlns="http://www.noneofyour.biz/schema/Common/2011/01"/>
</ControlParty>
<Action>new</Action>
<Shipments>
<Shipment>
<MasterSystemId xmlns="http://www.noneofyour.biz/schema/Transportation/2011/01">FargoGateInbound</MasterSystemId>
<OwnerId xmlns="http://www.noneofyour.biz/schema/Transportation/2011/01">DKPARCELS</OwnerId>
<TrackingCode xmlns="http://www.noneofyour.biz/schema/Transportation/2011/01">ConsignmentNo</TrackingCode>
<DatesAndTimes xmlns="http://www.noneofyour.biz/schema/Transportation/2011/01">
<ShipmentDateTime>2011-01-23T12:34:00</ShipmentDateTime>
</DatesAndTimes>
etcetera...
etcetera…
As you can see, the xml namespaces are added to multiple nodes, instead of being declared at the top and then prefixed to the element names. This will cause problems in the actual web service with which it will have to work (don’t ask me why).
What we would like is this instead:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<ShipmentInformationMessage
xmlns:ns1="http://www.noneofyour.biz/message/Transportation/2011/01"
xmlns:ns2="http://www.noneofyour.biz/schema/Transportation/2011/01"
xmlns:ns3="http://www.noneofyour.biz/schema/Common/2011/01">
<ns1:SenderId>NOSOTROS</ns1:SenderId>
<ns1:RecipientId>PARTNER</ns1:RecipientId>
<ns1:CreationTimeStamp>2011-07-01T13:31:14.7164012+02:00</ns1:CreationTimeStamp>
<ns1:Version>2.0</ns1:Version>
<ns1:TestIndicator>true</ns1:TestIndicator>
<ns1:ControlParty>
<ns3:Name>PrimaryContact</ns3:Name>
<ns3:Contact d6p1:nil="true" />
</ns1:ControlParty>
<ns1:Action>new</ns1:Action>
<ns1:Shipments>
<ns1:Shipment>
<ns2:MasterSystemId>FargoGateInbound</ns2:MasterSystemId>
<ns2:OwnerId>DKPARCELS</ns2:OwnerId>
<ns2:TrackingCode>ConsignmentNo</ns2:TrackingCode>
<ns2:DatesAndTimes>
<ns2:ShipmentDateTime>2011-01-23T12:34:00</ns2:ShipmentDateTime>
</ns2:DatesAndTimes>
etcetera...
etcetera…
After some investigation, I set about developing my custom request formatter, by extending IClientMessageFormatter, and then hooking it up by adding it to the operation behaviours. This at least went fine. However, I wasn’t really sure how to implement the SerializeRequest method and couldn’t find any useful examples on the internet, so fumbled ahead a bit and ended up with this:
public class SonicMessageFormatter : IClientMessageFormatter
{
private IClientMessageFormatter _InnerFormatter;
public SonicMessageFormatter(IClientMessageFormatter innerFormatter)
{
_InnerFormatter = innerFormatter;
}
public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
PutShipmentInformationMessage operation = (PutShipmentInformationMessage)parameters[0];
ShipmentInformationMessage sim = operation.ShipmentInformationMessage;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("ns1", "http://www.noneofyour.biz/message/Transportation/2011/01");
ns.Add("ns2", "http://www.noneofyour.biz/schema/Transportation/2011/01");
ns.Add("ns3", "http://www.noneofyour.biz/schema/Common/2011/01");
XmlSerializer xs = new XmlSerializer(sim.GetType());
MemoryStream ms = new MemoryStream();
StreamWriter writer = new StreamWriter(ms, Encoding.UTF8);
xs.Serialize(writer, sim);
Message requestMessage = Message.CreateMessage(messageVersion, sim.Action.ToString(), writer);
writer.Flush();
return requestMessage;
}
public object DeserializeReply(Message message, object[] parameters)
{
return _InnerFormatter.DeserializeReply(message, parameters);
}
}
When testing with this, I got the following error:
System.Runtime.Serialization.SerializationException: Type 'System.Text.UTF8Encoding+UTF8Encoder' with data contract name 'UTF8Encoding.UTF8Encoder:http://schemas.datacontract.org/2004/07/System.Text' is not expected. Add any types not known statically to the list of known types
So I changed the code by adding(modifying the following lines:
Type[] knownTypes = new Type[1];
knownTypes[0] = Encoding.UTF8.GetEncoder().GetType();
XmlSerializer xs = new XmlSerializer(sim.GetType(), knownTypes);
But now I get the following error:
System.InvalidOperationException: System.Text.UTF8Encoding.UTF8Encoder cannot be serialized because it does not have a parameterless constructor.
Well, heck! What do I do now!?
EDIT I am adding the wsdl of the mock service to help solve further issues:
<wsdl:definitions xmlns:ns="http://www.noneofyour.biz/message/Transportation/2011/01" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:auto1="http://www.noneofyour.biz/message/Transportation/2011/01" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://www.noneofyour.biz/message/Transportation/2011/01">
<wsdl:types>
<xsd:schema>
<xsd:import namespace="http://www.noneofyour.biz/message/Transportation/2011/01" schemaLocation="/mockShipmentInformationService_SOAPBinding?WSDL&interface=ShipmentInformationService_SOAPBinding&part=ShipmentInformationMessage.xsd"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="ShipmentInformationMessage">
<wsdl:part name="ShipmentInformationMessage" element="ns:ShipmentInformationMessage"></wsdl:part>
</wsdl:message>
<wsdl:portType name="ShipmentInformationService">
<wsdl:operation name="PutShipmentInformationMessage">
<wsdl:input message="ns:ShipmentInformationMessage"></wsdl:input>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ShipmentInformationService_SOAPBinding" type="ns:ShipmentInformationService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="PutShipmentInformationMessage">
<soap:operation soapAction="http://www.noneofyour.biz/ShipmentInformationService/PutShipmentInformationMessage" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ShipmentInformationService_Service">
<wsdl:port name="ShipmentInformationServicePort" binding="ns:ShipmentInformationService_SOAPBinding">
<soap:address location="http://localhost:8088/mockShipmentInformationService_SOAPBinding"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
EDIT Here follow the top parts for messages generated by the default formatter and custom formatter, respectively:
Default ClientMessageFormatter (InnerFormatter), which works:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://www.noneofyour.biz/ShipmentInformationService/PutShipmentInformationMessage</Action>
</s:Header>
<s:Body
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ShipmentInformationMessage
xmlns="http://www.noneofyour.biz/message/Transportation/2011/01">
<SenderId>NOSOTROS</SenderId>
<RecipientId>PARTNER</RecipientId>
<CreationTimeStamp>2011-08-05T10:42:38.9344907+02:00</CreationTimeStamp>
<Version>2.0</Version>
<TestIndicator>true</TestIndicator>
<ControlParty>
<Name xmlns="http://www.noneofyour.biz/schema/Common/2011/01">PrimaryContact</Name>
<Contact xsi:nil="true" xmlns="http://www.noneofyour.biz/schema/Common/2011/01" />
</ControlParty>
<Action>new</Action>
etcetera...
Custom ClientMessageFormatter (SonicMessageFormatter), which doesn’t work:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://www.noneofyour.biz/ShipmentInformationService/PutShipmentInformationMessage</Action>
</s:Header>
<s:Body>
<ShipmentInformationMessage
xmlns:ns1="http://www.noneofyour.biz/message/Transportation/2011/01"
xmlns:ns2="http://www.noneofyour.biz/schema/Transportation/2011/01"
xmlns:ns3="http://www.noneofyour.biz/schema/Common/2011/01">
<ns1:SenderId>NOSOTROS</ns1:SenderId>
<ns1:RecipientId>PARTNER</ns1:RecipientId>
<ns1:CreationTimeStamp>2011-08-05T13:45:36.9134685+02:00</ns1:CreationTimeStamp>
<ns1:Version>2.0</ns1:Version>
<ns1:TestIndicator>true</ns1:TestIndicator>
<ns1:ControlParty>
<ns3:Name>PrimaryContact</ns3:Name>
<ns3:Contact d6p1:nil="true">
</ns3:Contact>
</ns1:ControlParty>
<ns1:Action>new</ns1:Action>
etcetera...
As you can see, the custom formatter’s message has no namespaces declared in the Body-node in contrast with the default formatter’s message. I have also tried without adding namespaces to the serializer, but that didn’t make it work either.
I finally got it to work!!! The problem was that the ShipmentInformationMessage did not receive a prefix and thus was not recognized by the mock service (obviously). After A LOT of searching on the internet, I finally tried out the code from this excellent MSDN blog by Andrew Arnott!
I modified the code so xml namespaces could be added, plugged it in, and now it works!!!
This is what the top of the working message looks like:
Please note the prefix of the ShipmentInformationMessage-element that does the trick!
I now understand that to provide an alternative to the default formatter, which is based on WCF’s DataContractSerializer, just a bare XmlSerialzer doesn’t cut it. You need something at least as good as DataContractSerializer.
EDIT
The piece of code that does the trick here is the following:
Here, the root element is defined and the necessary namespace is added and the xml-serializer is initialized with it. I am passing “ShipmentInformationMessage” as wrapperName as well as its namespace.
Then, with all three namespaces and their prefixes added to the xml-serializer afterwards as well (like in the code example of my original post), I am getting exactly what I need and the SOAP UI mock service totally accepts it!