SOAP WCF 包装对象引用未设置为对象的实例

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

我有以下 WCF 服务

[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
        }
    }
}

每当我使用以下 SOAP 消息发出请求时

<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>

我收到错误

Object reference not set to an instance of an object

如果我在消息中添加一个 "InboundDelivery" 节点,它就会起作用。

 <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>

但是我无法更改消息,因为这是由第三方应用程序发送的。我尝试将 [MessageContract(IsWrapped=true)] 属性添加到我的服务中,但没有成功。我对 SOAP 很陌生,所以欢迎任何帮助。谢谢!

MessageContarct 可以控制 soap 消息的结构。

下面是我的测试代码。但是如果要使用messageContract,return类型也应该是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; }
}

下面是 fiddle 结果。

如果你不想使用messageContract并且无法控制客户端。 我认为您应该更改方法的签名。 例如,

  string MT_InboundDelivery(string HeaderDetails);