从SOAP消息中获取MessageContract / XmlType

时间:2013-08-30 19:42:16

标签: c# wcf soap

我很难通过添加服务参考来添加服务。我可以毫无问题地调用该服务,然后我收到回复(我可以在Fiddler中看到它):

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SOAP-ENV:Body>
    <executeResponse>
      <request_number>REQ0048172</request_number>
    </executeResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

但是,我在那里得到executeResponse的空值。 Reference.cs的相关部分如下。

接口:

[System.ServiceModel.ServiceContractAttribute(Namespace="http://www.serviceprovider.com/service")]
public interface Soap {

    // CODEGEN: Generating message contract since the operation execute is neither RPC nor document wrapped.
    [System.ServiceModel.OperationContractAttribute(Action="http://www.serviceprovider.com/service/execute", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
    executeResponse1 execute(executeRequest request);

}

客户端:

public partial class SoapClient : System.ServiceModel.ClientBase<Soap>, Soap {

    executeResponse1 Soap.execute(executeRequest request) {
        return base.Channel.execute(request);
    }

    public executeResponse execute(execute execute1) {
        executeRequest inValue = new executeRequest();
        inValue.execute = execute1;
        executeResponse1 retVal = ((Soap)(this)).execute(inValue);
        return retVal.executeResponse;
    }

}

executeResponse1:

[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class executeResponse1 {

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://www.serviceprovider.com/service", Order=0)]
    public executeResponse executeResponse;

}

executeResponse:

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.serviceprovider.com/service")]
public partial class executeResponse : object, System.ComponentModel.INotifyPropertyChanged {

    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
    public string request_number { get; set; }

}

我不确定如何从SOAP响应中反向修复此问题。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

根据这个:

命名空间= “http://www.serviceprovider.com/service”

WCF期望正文下的SOAP元素具有上述命名空间。但是它们是空的。如果你可以更改结果soap(甚至只是通过模拟它来临时测试)尝试用这个替换body元素:

<SOAP-ENV:Body xmlns="http://www.serviceprovider.com/service">

否则尝试在代理代码中设置Namespace =“”(我不确定WCF是否允许它)。您还可以使用空字符串替换WSDL中的此命名空间,这可能在生成代理方面更加健壮。

最后,如果这没有帮助,请根据此WSDL设置WCF服务(或直接在代理中设置数据协定),并查看它返回的soap与实际服务返回的SOAP的不同。