【问题标题】:Invoke a SOAP method with namespace prefixes使用命名空间前缀调用 SOAP 方法
【发布时间】:2010-12-20 15:01:32
【问题描述】:

我的 C# Web 服务客户端向基于 Java 的 Web 服务发送以下肥皂消息:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getData>
<request>
<requestParameters xmlns="http://b...">
<equals>
...
</equals>
</requestParameters>
</request>
</getData>
</soap:Body>
</soap:Envelope> 

并且基于 Java 的 Web 服务返回错误:

500 Internal Server Error
...
Cannot find dispatch method for {}getData
...

使用 Java 编写的客户端发送以下消息:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ns2:getData xmlns:ns2="http://a...">
<ns2:request>
<ns3:requestParameters xmlns:ns3="http://b...">
<ns3:equals>
...
</ns3:equals>
</ns3:requestParameters>
</ns2:request>
</ns2:getData>
</soap:Body>
</soap:Envelope> 

在 C# 中是否有一种简单的方法可以像 Java 客户端一样发送 SOAP 消息:使用命名空间前缀?

以下是发送消息的 C# 代码:

// class MyService is auto-generated using wsdl.exe tool
MyService service = new MyService();

RequestMessage request = new RequestMessage();
...

ResponseMessage response = service.getData(request);
...

更新:

这里是 RequestMessage 类:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.2152")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://uri.etsi.org/02657/v1.5.1#/RetainedData")]
public partial class RequestMessage
{

    private byte[] requestPriorityField;

    private RequestConstraints requestParametersField;

    private string deliveryPointHIBField;

    private string maxHitsField;

    private NationalRequestParameters nationalRequestParametersField;

    private System.Xml.XmlElement anyField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType="hexBinary", Order=0)]
    public byte[] requestPriority
    {
        get
        {
            return this.requestPriorityField;
        }
        set
        {
            this.requestPriorityField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    public RequestConstraints requestParameters
    {
        get
        {
            return this.requestParametersField;
        }
        set
        {
            this.requestParametersField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=2)]
    public string deliveryPointHIB
    {
        get
        {
            return this.deliveryPointHIBField;
        }
        set
        {
            this.deliveryPointHIBField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order=3)]
    public string maxHits
    {
        get
        {
            return this.maxHitsField;
        }
        set
        {
            this.maxHitsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=4)]
    public NationalRequestParameters nationalRequestParameters
    {
        get
        {
            return this.nationalRequestParametersField;
        }
        set
        {
            this.nationalRequestParametersField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyElementAttribute(Order=5)]
    public System.Xml.XmlElement Any
    {
        get
        {
            return this.anyField;
        }
        set
        {
            this.anyField = value;
        }
    }
}

更新 #2:

基于 Java 的 Web 服务不喜欢我的 C# 客户端生成 SOAP 消息的原因不是省略了命名空间前缀,而是因为省略了 getData 元素中的 xmlns,所以如果我的消息如下所示:

...
<getData xmlns="http://a...">
...
</getData>
...

有效!

通过在 wsdl.exe 生成的源代码中手动编辑 SoapRpcMethodAttribute,我设法将 xmlns 放入 getData。摘录如下:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(
    Name="AxxxPortTypeBinding", Namespace="http://a...")]
public partial class AxxxService 
    : System.Web.Services.Protocols.SoapHttpClientProtocol {

    ...

    /// <remarks/>
    [System.Web.Services.Protocols.SoapRpcMethodAttribute(
        "http://a.../getData", 
        RequestNamespace = "http://a...", 
        ResponseNamespace = "http://a...",
        Use = System.Web.Services.Description.SoapBindingUse.Literal)]
    [return: System.Xml.Serialization.XmlElementAttribute("response")]
    public ResponseMessage getData(RequestMessage request) {
        object[] results = this.Invoke("getData", new object[] {
                    request});
        return ((ResponseMessage)(results[0]));
    }

    ...
}

在我更改之前,SoapRpcMethodAttribute 有以下构造函数:

[System.Web.Services.Protocols.SoapRpcMethodAttribute(
    "", RequestNamespace = "", ResponseNamespace = "",
    Use = System.Web.Services.Description.SoapBindingUse.Literal)]

现在的问题是:首先要在 WSDL 文件中放入什么,以便 SoapRpcMethodAttribute 将这些字符串放在构造函数中(由 wsdl.exe 工具填充)?

【问题讨论】:

  • 您是否使用 wsdl.exe 生成了您的服务代理?
  • 是(请参阅问题底部代码中的注释)。

标签: c# xml web-services asmx xml-namespaces


【解决方案1】:

在生成服务代码时 WSDL.exe 工具没有正确拉入命名空间时,我已经看到过这个问题。检查生成的代码中的 request 对象定义。我的猜测是request 对象的类定义上没有定义XmlRootAttribute 属性。

将属性[XmlRootAttribute(Namespace "http://a...")] 添加到request 对象的类定义中应该可以解决此问题。

作为旁注,我建议使用部分类定义在单独的代码文件中添加这个附加属性。在单独的文件中定义属性将允许您在必要时使用 WSDL.exe 重新生成 Web 服务代码,而无需覆盖修复以正确设置根元素的命名空间。

【讨论】:

  • 是的,你是对的,RequestMessage 类定义(或同一自动生成文件中的任何其他类)中没有 XmlRootAttribute。
  • 我已将 XmlRootAttribute 添加到类定义中,但没有帮助。
  • @mvladic 您在上面发布的示例显示您添加了XmlTypeAttribute 属性,而不是XmlRootAttribute 属性。鉴于您的示例 SOAP 请求,RequestMessage 类应该被标识为正在发送的 XML 消息的根元素。我还从您的示例中注意到该服务返回一个响应(可能在 ResponseMessage 类中?)。您还需要使用 XmlRootAttribute 属性装饰此响应类,以确保使用正确的命名空间返回响应。
  • 我贴的例子是原创的,由wsdl工具生成。按照您的建议,我尝试添加 XmlRootAttribute,但没有帮助。
【解决方案2】:

生成的代理类应该有正确的命名空间。我推荐两件事:

1) 从命令行运行 WSDL.EXE 并注意是否有任何错误或警告。如果是这样,请编辑您的问题以包含它们。

2) 除非您在使用 .NET 2.0 时遇到问题,否则您应该尝试使用“添加服务引用”或等效的“SVCUTIL.EXE”创建代理类,这些将在客户端使用现代的 WCF 基础架构,解决这个问题的可能性更大。

【讨论】:

  • “添加服务引用”只适用于asmx,而我只有一个wsdl和一个xsd文件。
  • 我使用“svcutil my.wsdl my.xsd /language:C#”创建了客户端,但它仍然无法工作 - 没有命名空间前缀。
  • 我在我的问题中添加了 RequestMessage 类的定义。
  • @mvladic:您应该通过 connect.microsoft.com/visualstudio 向 Microsoft 报告此问题。确保包含该 WSDL 和任何包含的 XSD 文件。与 ASMX 不同,WCF 正在积极开发中,实际上应该修复错误。
  • @mvladic:您对“添加服务引用”有误 - 它也适用于 WSDL。它查看 .asmx 或 .svc 的唯一原因是查找 WSDL。
【解决方案3】:

我遇到了同样的问题并解决了它改变属性 UseSystem.Web.Services.Protocols.SoapDocumentMethodAttribute 属性对应于每个 Web 服务方法的值。 System.Web.Services.Description.SoapBindingUse.Literal 默认值已替换为 System.Web.Services.Description.SoapBindingUse.Encoded

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多