如何更改ASMX Web服务的WSDL端口的地址?

时间:2019-05-14 04:31:45

标签: c# .net asmx

我们有一些旧的ASMX Web服务已移至额外的安全层之后。 WSDL是自动生成的运行时,现在正在使用其运行服务器的IP,而不是公共DNS。您如何通过代码或web.config通过公共DNS更改IP?

该Web服务既通过HTTP POST / GET提供带有查询字符串的SOAP,又提供更常见的方案,即通过HTTP POST带有SOAP-request-body的SOAP。我想用运行时生成的WSDL的公共DNS名称切换本地IP。在常见的情况下,我已经使用SoapExtensionReflector成功地做到了这一点,但是它不适用于使用查询字符串的WSDL端口。

SoapExtensionReflector的简化版本如下:

public class MySoapExtensionReflector : SoapExtensionReflector
{
    public override void ReflectDescription()
    {
        var description = ReflectionContext.ServiceDescription;

        foreach (Service service in description.Services)
        {
            foreach (Port port in service.Ports)
            {
                foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
                {
                    SoapAddressBinding binding = extension as SoapAddressBinding;
                    if (null != binding)
                    {
                        binding.Location = Regex.Replace(binding.Location,
                                           "http://localhost:2017", 
                                           "https://example.com");
                    }
                }
            }
        }
    }

    public override void ReflectMethod()
    {
        // No implementation
    }
}

web.config的相关部分:

<system.web>
    <webServices>
      <soapExtensionReflectorTypes>
        <add type="MyService.MySoapExtensionReflector, MyService"/>
      </soapExtensionReflectorTypes>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
</system.web>

这将在WSDL中产生以下结果:

<wsdl:service name="MyService">
    <wsdl:port name="MyServiceSoap" binding="tns:MyServiceSoap">
        <soap:address location="https://example.com/Myservice.asmx"/>
    </wsdl:port>
    <wsdl:port name="MyServiceSoap12" binding="tns:MyServiceSoap12">
        <soap12:address location="https://example.com/Myservice.asmx"/>
    </wsdl:port>
    <wsdl:port name="MyServiceHttpGet" binding="tns:MyServiceHttpGet">
        <http:address location="http://localhost:2017/Myservice.asmx"/>
    </wsdl:port>
    <wsdl:port name="MyServiceHttpPost" binding="tns:MyServiceHttpPost">
        <http:address location="http://localhost:2017/Myservice.asmx"/>
    </wsdl:port>
</wsdl:service>

哪个不是理想的结果。最后两个端口是由协议生成的-web.config中的HttpPost和HttpGet定义。

您如何更改此设置,以便WSDL端口具有所需的主机名?

结果应如下所示:

<wsdl:service name="MyService">
    <wsdl:port name="MyServiceSoap" binding="tns:MyServiceSoap">
        <soap:address location="https://example.com/Myservice.asmx"/>
    </wsdl:port>
    <wsdl:port name="MyServiceSoap12" binding="tns:MyServiceSoap12">
        <soap12:address location="https://example.com/Myservice.asmx"/>
    </wsdl:port>
    <wsdl:port name="MyServiceHttpGet" binding="tns:MyServiceHttpGet">
        <http:address location="http://example.com/Myservice.asmx"/>
    </wsdl:port>
    <wsdl:port name="MyServiceHttpPost" binding="tns:MyServiceHttpPost">
        <http:address location="http://example.com/Myservice.asmx"/>
    </wsdl:port>
</wsdl:service>

0 个答案:

没有答案