.NET Core 參考 WCF,SVC 按環境調整網址

  • 258
  • 0

坐和放寬

原來在 Framework 時代
加入 svc,asmx 參考後,網址是存放在 Web.config
我們可以藉由 web.config transform
在 Web.Debug.config,Web.Release.config 存放不同網址以區分開發/正式環境

但 Core 改成放到 Reference.cs 的 GetEndpointAddress() 裡面

一開始小專案我沒研究怎麼處理,只有在每一個Controller的建構子裡面設定svc的網址
大專案就行不通了
比較好的方法是加入一個 partial 類別
從這裡餵網址進去
最簡單用 #if DEBUG
複雜一點存在appsettings.json,appsettings.Develope.json
在 Startup.cs 讀進全域變數 

namespace wsMyService {
    public partial class MyServiceClient {
        static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials) {
            var uriString = ExampleWeb.clsDefine.wsMyService;
            serviceEndpoint.Address =
                new System.ServiceModel.EndpointAddress(new Uri(uriString),
                new System.ServiceModel.DnsEndpointIdentity(""));
        }
    }
}

參考:load WCF service by environment in .net core project