Web服务请求返回null

时间:2012-10-25 21:33:33

标签: c# .net web-services

我正在尝试调用托管在java服务器上的Web服务。我通过Visual Studio提供的Web服务创建工具创建了类。当我调用该方法时,我可以在Fiddler中看到它正在返回有效数据。但是,在我的C#代码中,我的结果为null。

任何帮助都将不胜感激。

C#呼叫代码:

RuleValidationResponseRule[] ruleResponse = rulesWebService.RuleValidation(ruleData, "LO");

Web Service生成的代码:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.example.org/LathamInt/RuleValidation", 
    RequestNamespace="http://www.example.org/LathamInt/", 
    ResponseNamespace="http://www.example.org/LathamInt/", 
    Use=System.Web.Services.Description.SoapBindingUse.Literal, 
    ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlArrayAttribute("ruleValidationResp", 
    Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[return: System.Xml.Serialization.XmlArrayItemAttribute("rule", 
    Form=System.Xml.Schema.XmlSchemaForm.Unqualified, 
    IsNullable=false)]
public RuleValidationResponseRule[] RuleValidation([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    RuleValidationRuleData ruleData, 
    [System.Xml.Serialization.XmlAttributeAttribute()] string company) {
    object[] results = this.Invoke("RuleValidation", new object[] {
                ruleData,
                company});
    return ((RuleValidationResponseRule[])(results[0]));
}

XML传递给webservice(来自Fiddler):

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <RuleValidation company="LO" xmlns="http://www.example.org/LathamInt/">
            <ruleData xmlns="">
                <rule ruleID="GDSP" />
                <varData varName="GuideSpace" varValue="3" />
            </ruleData>
        </RuleValidation>
    </soap:Body>
</soap:Envelope>

从服务返回的XML(来自Fiddler):

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lat="http://www.example.org/LathamInt/">
    <soapenv:Header/>
    <soapenv:Body>
        <RuleValidationResponse>
            <ruleValidationResp>
                <rule result="0" ruleId="GDSP" status="success">
                    <expression pre="@GuideSpace > 3" post="3 > 3"/>
                </rule>
            </ruleValidationResp>
        </RuleValidationResponse>
    </soapenv:Body>
</soapenv:Envelope>

C#生成的代码中的响应定义:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.example.org/LathamInt/")]
public partial class RuleValidationResponseRule {

    private RuleValidationResponseRuleExpression expressionField;

    private string resultField;

    private string ruleIdField;

    private string statusField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public RuleValidationResponseRuleExpression expression {
        get {
            return this.expressionField;
        }
        set {
            this.expressionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string result {
        get {
            return this.resultField;
        }
        set {
            this.resultField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ruleId {
        get {
            return this.ruleIdField;
        }
        set {
            this.ruleIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string status {
        get {
            return this.statusField;
        }
        set {
            this.statusField = value;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

我相信我已经解决了它。

在返回XML中,这一行

<RuleValidationResponse>

表示返回标记没有命名空间。需要将其更改为

<lat:RuleValidationResponse>

要么是,要么修改

[System.Web.Services.Protocols.SoapDocumentMethodAttribute ("http://www.example.org/LathamInt/RuleValidation", 
    RequestNamespace="http://www.example.org/LathamInt/", 
    ResponseNamespace="http://www.example.org/LathamInt/",  //<-- ** Change this line
    Use=System.Web.Services.Description.SoapBindingUse.Literal, 
    ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

[System.Web.Services.Protocols.SoapDocumentMethodAttribute ("http://www.example.org/LathamInt/RuleValidation", 
    RequestNamespace="http://www.example.org/LathamInt/", 
    ResponseNamespace="",  //<-- ** Change this line
    Use=System.Web.Services.Description.SoapBindingUse.Literal, 
    ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]