Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6321477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:09:22+00:00 2026-05-24T16:09:22+00:00

My C#.NET project has to talk to an external Java-platform web service (Sonic ESB).

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:09:22+00:00Added an answer on May 24, 2026 at 4:09 pm

    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:

     <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>
         <ns1:ShipmentInformationMessage xmlns:ns2="http://www.noneofyour.biz/schema/Transportation/2011/01" xmlns:ns3="http://www.noneofyour.biz/schema/Common/2011/01" xmlns:ns1="http://www.noneofyour.biz/message/Transportation/2011/01">
           <ns1:SenderId>NOSOTROS</ns1:SenderId>
           <ns1:RecipientId>PARTNER</ns1:RecipientId>
           <ns1:CreationTimeStamp>2011-08-10T07:44:52.9423961+02:00</ns1:CreationTimeStamp>
           <ns1:Version>2.0</ns1:Version>
           <ns1:TestIndicator>true</ns1:TestIndicator>
           <ns1:ControlParty>
             <ns3:Name>PrimaryContact</ns3:Name>
             <ns3:Contact d5p1:nil="true" xmlns:d5p1="http://www.w3.org/2001/XMLSchema-instance" />
           </ns1:ControlParty>
           <ns1:Action>new</ns1:Action>
    

    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:

                XmlRootAttribute root = new XmlRootAttribute(wrapperName);
                root.Namespace = wrapperNamespace;
                serializer = new XmlSerializer(this.objectType, root);
    

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .net project that has a web reference to a service. I
I have an ASP.NET project which has already some custom fields in it's web.config
I've got a basic asp.net MVC project that has a Web project, a Service
Once an asp.net MVC project has a web deployment project associated with it, there
I have a UserControl in my Asp.net project that has a public property. I
I have inherited a project that has class libraries written in VB.NET, some of
We have a large ASP.NET project which has 100s of BLL classes. In our
My current project has a development web server and live web server. Developers make
I have a .Net project which has the following dlls: AxInterop.Crystal.dll ( comctl32.ocx )
I have a C#/ASP.net project has included a database that I have developed that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.