這是專案最近遇到的一個問題,還是將它記錄下來以免忘記.

若您是使用純 .NET Solution 的朋友,可以忽略它,因為 .NET 程式本身呼叫是不會有這個問題的.

 

在一個大型的專案中,Web Services 都很容易會有兩台以上的機器的部署.

架構是 Client => L4 Switch => Web Services => Database (Cluster)

https 到 L4 switch 後轉為 http 到 web services,所以 web services 收到的為 http 的 request

所以它在回給前端的 WSDL 中的 soap:address location=”http://…”

Client 收到的 URL 為 http,再拿這 URL 來呼叫即會被 L4 Switch 擋下來,因為它只允許 https 來連接

可以透過三步驟將其替換為 https

1. 新增一個 class,繼承自 SoapExtensionReflector並 override ReflectMethod

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Description;

namespace ChangeSoapAddressDemo
{
    public class SoapAddressReflector : SoapExtensionReflector
    {
        public override void ReflectMethod()
        {
            ServiceDescription sd = ReflectionContext.ServiceDescription;

            foreach (Service service in sd.Services)
            {
                foreach (Port port in service.Ports)
                {
                    foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
                    {
                        SoapAddressBinding address = (SoapAddressBinding)extension;
                        address.Location = RemapHttpReferencesToHttps(address.Location);
                    }
                }
            }
        }

        private string RemapHttpReferencesToHttps(string location)
        {
            return location.Replace("http:", "https:");
        }
    }
}

 

2. 在 web.config 中加入下列的設定

<webServices>
        <soapExtensionReflectorTypes>
          <add type="ChangeSoapAddressDemo.SoapAddressReflector, ChangeSoapAddressDemo"/>
        </soapExtensionReflectorTypes>
      </webServices>

 

3. 瀏覽 WSDL,即可看到 soap:address location=”https://…”

image

Enjoy

anISV 發表在 痞客邦 PIXNET 留言(0) 人氣()