gpt4 book ai didi

c# - 调用带有 namespace 前缀的 SOAP 方法

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:39 24 4
gpt4 key购买 nike

我的 C# 网络服务客户端向基于 Java 的网络服务发送以下 soap 消息:

<?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 工具在生成服务代码时没有正确地引入命名空间。检查生成的代码中的 request 对象定义。我的猜测是 request 对象的类定义中没有定义 XmlRootAttribute 属性。

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

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

关于c# - 调用带有 namespace 前缀的 SOAP 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4490810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com