如何将Visual Studio 2017中ASMX Web服务的WSDL命名空间s改为xs?
解决ASMX Web服务WSDL中命名空间前缀从
s改为xs的方法 在Visual Studio 2017搭建的ASMX Web服务里,要把WSDL默认生成的XML Schema前缀s替换成xs,可以通过自定义ServiceDescriptionReflector来实现,具体操作步骤如下:
1. 创建自定义WSDL反射器类
在你的Web服务项目中新增一个类文件(比如命名为CustomWsdlReflector.cs),写入以下代码:
using System.Web.Services.Description; public class CustomWsdlReflector : ServiceDescriptionReflector { public override void ReflectMethod() { base.ReflectMethod(); // 遍历所有服务描述的命名空间映射 foreach (ServiceDescription sd in ServiceDescriptions) { // 移除默认的"s"前缀映射 if (sd.Namespaces.ContainsKey("s")) { sd.Namespaces.Remove("s"); } // 添加"xs"前缀到XML Schema命名空间的映射 sd.Namespaces.Add("xs", "http://www.w3.org/2001/XMLSchema"); } } }
2. 在Web服务类中关联自定义反射器
修改你的ReceivePayment Web服务类,添加[WebServiceBinding]特性,指定我们刚创建的自定义反射器:
using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Serialization; [WebService(Namespace = "http://smp.ws.uz/", Name = "SMP034")] // 关联自定义反射器以修改WSDL前缀 [WebServiceBinding(ReflectorType = typeof(CustomWsdlReflector))] public class ReceivePayment : WebService { [WebMethod] [return: XmlElement("accept")] [SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Wrapped)] public accept receivePayment(payment payment) { accept accept = new accept(); // .... 为accept对象设置所有属性值 .... return accept; } }
3. 重新发布并验证结果
重新编译并发布你的Web服务,访问WSDL地址(例如http://你的域名/ReceivePayment.asmx?wsdl),就能看到WSDL中的命名空间前缀已经替换为xs,示例如下:
<wsdl:definitions xmlns:xs="http://www.w3.org/2001/XMLSchema" ...>
如果你的Web服务使用了SoapDocumentService特性,也可以尝试在特性中直接配置命名空间前缀,但上面的自定义反射器方法更直接,能全局替换WSDL里的Schema前缀。
内容的提问来源于stack exchange,提问作者Kholdarbekov
相关产品推荐
相关产品推荐

