【问题标题】:SOAP WCF Wrapped Object reference not set to an instance of an objectSOAP WCF Wrapped Object 引用未设置为对象的实例
【发布时间】:2019-04-11 13:40:39
【问题描述】:

我有以下 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>

我得到错误

对象引用未设置为对象的实例

如果我在消息中添加一个“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 很陌生,所以欢迎任何帮助。谢谢!

【问题讨论】:

    标签: .net web-services wcf soap


    【解决方案1】:

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

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

    下面是小提琴的结果。

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

      string MT_InboundDelivery(string HeaderDetails);
    

    【讨论】:

      猜你喜欢
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多