8

I'm getting an Object reference not set to an instance of object error in my WCF web service which uses webHttpBinding (soap 1.1) I have noticed that if you have the input parameters in a certain order the error does not get raised.

i.e.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:not="http://schemas.globalfoundries.com/NotificationService">
   <soapenv:Header/>
   <soapenv:Body>
      <not:NotifyWorkflowItemUpdate>
         <not:userIDs>testUserID</not:userIDs>
         <not:taskID>testTaskID</not:taskID>
         <not:taskType>testTaskType</not:taskType>
         <not:status>testStatus</not:status>
         <not:appID>testAppID</not:appID>
         <not:message>testMessage</not:message>
      </not:NotifyWorkflowItemUpdate>
   </soapenv:Body>
</soapenv:Envelope>

However if I change the order of the input parameters in the request template I get the aforementioned error. i.e. (note message and userIDs parameters are switched)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:not="http://schemas.globalfoundries.com/NotificationService">
   <soapenv:Header/>
   <soapenv:Body>
      <not:NotifyWorkflowItemUpdate>
     <not:message>testMessage</not:message>
         <not:taskID>testTaskID</not:taskID>
         <not:taskType>testTaskType</not:taskType>
         <not:status>testStatus</not:status>
         <not:appID>testAppID</not:appID>
         <not:userIDs>testUserID</not:userIDs>
      </not:NotifyWorkflowItemUpdate>
   </soapenv:Body>
</soapenv:Envelope>

Why is this happening? Are request parameters mapped to the .Net method parameters via the order and not by the names? Is there an attribute that I have to specify on the service contract to make named parameter mapping possible?

Harindaka
  • 4,658
  • 8
  • 43
  • 62

2 Answers2

11

You need to use XmlSerializerFormat class in your WCF service interface.

[ServiceContract, XmlSerializerFormat]
public interface IGoodMessageService
{
    ...
}

Problem and solution is explained in this link: http://neimke.blogspot.com.tr/2012/03/serialization-ordering-causes-problems.html

  • Yes please add some explanation as well if you can, I will accept this as the answer. Waited two years for the answer though :D – Harindaka Jul 10 '14 at 12:30
5

The XML schema of your SOAP message specifies the order. In XML order of element matters and WCF is validating the XML against the schema.

softveda
  • 10,858
  • 6
  • 42
  • 50
  • Is there any way to use something like named parameters so that the consuming client can specify the parameters in any order as long as the correct names are specified in the tags? – Harindaka Jul 26 '12 at 11:51
  • I don't think so. The SOAP and XSD are well defined standards designed for interoperability. Don't think SOAP as methods and parameters because your language generates proxies. Think it as message passing and the contract of that message is validated strictly. – softveda Jul 26 '12 at 12:24
  • I just posted the same question, only I added that in ASMX, the ordering does NOT matter. I would think there would be a setting in WCF that would allow you to mimic the unordered feature in ASMX. – Huy T Sep 20 '13 at 20:42