【问题标题】:web service request is returning nullWeb 服务请求返回 null
【发布时间】:2012-10-25 21:33:32
【问题描述】:

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

任何帮助将不胜感激。

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 传递给 web 服务(来自 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;
        }
    }
}

【问题讨论】:

  • 这是很多代码。也许您可以通过对代码设置断点、单步执行并告诉我们数据在何处消失为 null 来帮助我们。我们无法设置机器来检查这一点,因此您可以更好地进行故障排除。
  • 我同意,但是,Visual Studio 似乎并没有破坏生成的 Web 服务代码。所以我一直试图理解生成的代码,看看命名空间是否不合适或类似的东西。
  • 然后放入一些日志语句。
  • 这一行在这里:object[] results = this.Invoke("RuleValidation", new object[] { ruleData, company});显示 results 有 1 个元素,并且该元素为 null。
  • 我不太明白那行代码...是在调用本身吗?

标签: c# .net web-services


【解决方案1】:

我相信我已经解决了。

在返回的 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)]

【讨论】:

  • 1.将 Reference.cs 中的 [][] 替换为 [] - 选中 2. 使用 Web 参考而不是服务参考 - 选中 3. 花了半天时间搜索解决方法 - 选中 4. 阅读 10 多篇提到 Axis / Java 和 .NET 集成的文章 -已检查 5. 找到此响应和可行的解决方案 - 已检查 :) 谢谢!
猜你喜欢
  • 2020-01-28
  • 2015-08-14
  • 1970-01-01
  • 2020-03-21
  • 1970-01-01
  • 2019-08-26
  • 2017-03-12
  • 2016-03-31
  • 2023-03-07
相关资源
最近更新 更多