如何强制.NET ASMX的WSDL生成将soap:address地址从http改为https?
解决VB.NET ASMX项目SSL卸载后端WSDL地址修改问题
优先代码外实现方案(构建流程适配)
- IIS出站URL重写规则:直接在站点
web.config中配置重写规则,拦截WSDL请求并替换协议。配置示例:
<system.webServer> <rewrite> <outboundRules> <rule name="Replace WSDL HTTP with HTTPS"> <match filterByTags="None" pattern="<soap:address location="http://(.*?)"/>" /> <action type="Rewrite" value="<soap:address location="https://{R:1}"/>" /> <conditions> <add input="{REQUEST_URI}" pattern="\.asmx\?wsdl$" /> </conditions> </rule> </outboundRules> </rewrite> </system.webServer>
该规则会在WSDL内容返回客户端前,自动将soap:address中的http://替换为https://,无需修改项目代码,可在构建时同步此配置到部署环境。
- 构建后脚本替换:在项目构建流程中加入自动替换脚本(以PowerShell为例),针对预先生成的WSDL文件修改地址:
# 替换WSDL中的HTTP地址为HTTPS $wsdlFilePath = "$(OutputPath)\example.asmx.wsdl" (Get-Content $wsdlFilePath -Raw) -replace '<soap:address location="http://(.*?)"/>', '<soap:address location="https://$1"/>' | Set-Content $wsdlFilePath
将此脚本添加到项目的构建后事件中,每次构建完成后自动执行替换操作。
备选代码内实现方案
如果代码外方案无法落地,可通过重写ASMX服务的GetWsdl方法实现地址替换:
Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.Xml Public Class ExampleService Inherits WebService Protected Overrides Function GetWsdl() As XmlDocument Dim wsdlDoc = MyBase.GetWsdl() Dim addressNodes = wsdlDoc.GetElementsByTagName("soap:address") For Each node As XmlNode In addressNodes Dim locationAttr = node.Attributes("location") If locationAttr IsNot Nothing Then locationAttr.Value = locationAttr.Value.Replace("http://", "https://") End If Next Return wsdlDoc End Function End Class
内容的提问来源于stack exchange,提问作者Jim Hankins
相关产品推荐
相关产品推荐

