Search code examples
.netweb-serviceswcfsoap

SOAP WCF Wrapped Object reference not set to an instance of an object


I have following WCF Service

[ContractType(ContractKnownType.CORE)]
[ServiceContract(Namespace = WcfConstants.WcfNamespace), ServiceBehavior(Namespace = WcfConstants.WcfNamespace)]
[HostAsWebService]
[XmlSerializerFormat]
public class DeliveryWebService : IFactoryService
{
    [OperationContract, Sessional]
    public string InboundDelivery(MT_InboundDelivery MT_InboundDelivery)
    {
        var error = "";
        try
        {
            ... some code
        }
    }
}

Whenever I do a request with following SOAP Message

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.MEScontrol.net/WebServices">
  <soapenv:Header/>
     <soapenv:Body>
         <web:MT_InboundDelivery>
             <web:HeaderDetails/>
         </web:MT_InboundDelivery>
     </soapenv:Body>
</soapenv:Envelope>

I get error

Object reference not set to an instance of an object

If I add a "InboundDelivery" node to the message it works.

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.MEScontrol.net/WebServices">
  <soapenv:Header/>
     <soapenv:Body>
       <web:InboundDelivery>
           <web:MT_InboundDelivery>
             <web:HeaderDetails/>
           </web:MT_InboundDelivery>
         <web:InboundDelivery>
     </soapenv:Body>
</soapenv:Envelope>

However I cannot change the message since this is send by a third-party application. I tried adding properties as [MessageContract(IsWrapped=true)] to my service without any success. I'm very new to SOAP so any help is welcome. Thanks!


Solution

  • MessageContarct could control the structure of soap message.

    Below is my test code. But if you want to use messageContract, the return type should also be of type MT_InboundDelivery

    public class DeliveryWebService : IFactoryService
    {
        public MT_InboundDelivery InboundDelivery(MT_InboundDelivery MT_InboundDelivery)
        {
    
            return MT_InboundDelivery;
        }
    }
     [ServiceContract(Namespace = "http://www.MEScontrol.net/WebServices")]
    
    public interface IFactoryService
    {
        [OperationContract]
        MT_InboundDelivery InboundDelivery(MT_InboundDelivery MT_InboundDelivery);
    }
    
    
    [MessageContract(IsWrapped = true)]
    public class MT_InboundDelivery
    {
        [MessageBodyMember]
        public string HeaderDetails { get; set; }
    }
    

    Below is the fiddle result. enter image description here

    If you don't want to use messageContract and couldn't control the client side. I think you should change the signature of your method. For example ,

      string MT_InboundDelivery(string HeaderDetails);