发送前修改SOAP XML请求:WSDL生成类的命名空间适配问题
解决SOAP请求命名空间前缀不匹配的问题
看起来你遇到的核心问题是wsdl.exe生成的代理类默认将操作节点放在默认命名空间中,而服务器期望使用指定的neon前缀来标识命名空间。以下是具体的代码修改步骤,帮你解决这个问题:
1. 修正SoapDocumentMethodAttribute属性
首先,更新你的emitirWS方法的特性配置,补充正确的SOAP Action,并确保命名空间参数正确:
[System.Web.Services.Protocols.SoapDocumentMethodAttribute( "http://neon.stoconsulting.com/NeonEmisionWS/NeonEmisionWS?wsdl/emitirWS", // 补充正确的SOAP Action RequestNamespace = "http://neon.stoconsulting.com/NeonEmisionWS/NeonEmisionWS?wsdl", ResponseNamespace = "http://neon.stoconsulting.com/NeonEmisionWS/NeonEmisionWS?wsdl", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] [return: System.Xml.Serialization.XmlElementAttribute("RespuestaWS", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] [WebServiceSOAPExtension] public respuestaWS emitirWS(comprobante comprobante) { object[] results = this.Invoke("emitirWS", new object[] { comprobante}); return ((respuestaWS)(results[0])); }
2. 配置命名空间前缀映射
在你的客户端代理类(继承自SoapHttpClientProtocol的类)的构造函数中,添加XmlSerializerNamespaces配置,将命名空间映射到neon前缀:
public class NeonEmisionWS : SoapHttpClientProtocol { public NeonEmisionWS() { // 设置你的服务URL this.Url = "https://your-service-url-here/NeonEmisionWS"; // 添加命名空间前缀映射 XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("neon", "http://neon.stoconsulting.com/NeonEmisionWS/NeonEmisionWS?wsdl"); this.XmlSerializerNamespaces = ns; } // 你的emitirWS方法放在这里 }
3. 确保实体类不绑定到命名空间
检查你的comprobante及其子实体类(如envioCfdi、emisor等)的特性,确保它们的XmlTypeAttribute没有指定Namespace属性,这样序列化时这些节点会使用默认命名空间,不会带上neon前缀:
// 正确的实体类配置示例 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public class comprobante { // 你的类成员... public envioCfdi envioCfdi { get; set; } public emisor emisor { get; set; } // ...其他属性 }
4. 验证数组类型的正确性
你之前将var[][]改为var[]才编译通过,这里要确认这个修改符合WSDL的定义:
- 如果WSDL中对应的元素是
maxOccurs="unbounded"(可重复),那么一维数组是正确的 - 如果后续发现序列化的数组节点不符合要求,建议重新使用
wsdl.exe生成代码,可尝试添加/wrapped参数:wsdl.exe /wrapped /language:C# your-wsdl-url.wsdl
完成以上修改后,你的客户端生成的SOAP请求应该会自动添加xmlns:neon的命名空间声明,并且emitirWS节点会变为<neon:emitirWS>,完全匹配服务器要求的格式。
内容的提问来源于stack exchange,提问作者thepanch
相关产品推荐
相关产品推荐

