整理 WCF 跨機器部署時常見的「無法連線」、「拒絕存取」、「安全性協商失敗」等問題,提供逐項排查清單與設定範本。
WCF 預設啟用訊息安全性,跨機器若無憑證、無網域環境,最簡單可行的方式是**雙邊關閉安全模式** (僅限可信內網)。
<system.serviceModel>
<services>
<service name="Services.CalculatorService" behaviorConfiguration="metadataBehavior">
<endpoint address="https://192.168.1.108:9999/calculatorservice"
binding="wsHttpBinding"
bindingConfiguration="NoSecurityBinding"
contract="Contracts.ICalculator" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="https://192.168.1.108:9999/" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="NoSecurityBinding">
<security mode="None" /> <!-- 關鍵:關閉安全性 -->
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculator">
<security mode="None"> <!-- 關鍵:關閉安全性 -->
<transport clientCredentialType="Windows" proxyCredentialType="None" />
<message clientCredentialType="Windows" negotiateServiceCredential="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://192.168.1.108:9999/calculatorservice"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ICalculator"
contract="Contracts.ICalculator"
name="WSHttpBinding_ICalculator" />
</client>
</system.serviceModel>
| # | 項目 | 檢查方式 | 常見錯誤 |
|---|---|---|---|
| 1 | 防火牆 | 服務端開放埠口 (例 9999) 入站規則;或暫時關閉防火牆測試 | 只開啟輸出規則、未針對特定埠口 |
| 2 | URL ACL (HTTP 命名空間預約) | 管理員執行 `netsh http add urlacl url=https://+:9999/ user=Everyone` | 未預約、帳號權限不足 (需 SYSTEM/Administrators) |
| 3 | Binding 位址 | 服務端 `baseAddress`/`endpoint address` 使用實際 IP (非 localhost/127.0.0.1) | 用 `localhost` 導致客戶端解析到自己機器 |
「跨機器不通」90% 是防火牆、URL ACL、Binding 位址、安全模式不匹配四大項其中之一。依清單逐項排除,通常 10 分鐘內解決。