TCP 錯誤 10061: 無法連接目標主機,目標機積極拒絕連接
這是 **TCP 層面的連接被拒絕**(RST 包),非 HTTP 4xx/5xx,說明:
<add key="WebServiceUrl" value="https://121.15.129.225:8877/?wsdl/" /> <-- 包含 ?wsdl
<endpoint address="https://121.15.129.225:8877/" ... /> <-- 不含 ?wsdl
正確:Endpoint address 應指向 **服務實際端點**,而非 WSDL 文檔地址。若服務部署在 `
<binding name="LmsBLQService" /> <-- 空綁定配置
<endpoint bindingConfiguration="LmsBLQService" ... />
若服務需特定綁定(如 `basicHttpBinding` 安全模式、編碼、超時),必須完整配置:
<basicHttpBinding>
<binding name="LmsBLQService"
maxReceivedMessageSize="2147483647"
receiveTimeout="00:10:00"
sendTimeout="00:10:00">
<security mode="None" /> <-- 或 Transport / TransportCredentialOnly
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
contract="ServiceReference1.LmsBLQServicePortType"
```
需確認:
- 服務引用命名空間正確
- 介面定義與服務端 WSDL 同步(建議「更新服務引用」)
網路層排查清單
- 端口連通性測試:
telnet 121.15.129.225 8877
# 或 PowerShell
Test-NetConnection 121.15.129.225 -Port 8877
若失敗 → 防火牆/安全組/服務未啟動
- 服務端狀態確認:
- 服務進程是否運行
- 端口是否監聽:`netstat -an | findstr 8877`
- 綁定 IP:`0.0.0.0:8877` (全介面) vs `127.0.0.1:8877` (僅本地)
- 防火牆/安全組規則:
- 雲服務商控制台:入站規則允許 8877
- 服務器本地防火牆:`firewall-cmd --add-port=8877/tcp`
- WSDL 可訪問性驗證:
curl https://121.15.129.225:8877/?wsdl
# 瀏覽器直接訪問查看 XML 結構
正確配置模板
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="LmsBLQServiceBinding"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
receiveTimeout="00:10:00"
sendTimeout="00:10:00">
<security mode="None" /> <-- 根據服務端實際情況調整
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://121.15.129.225:8877/"
binding="basicHttpBinding"
bindingConfiguration="LmsBLQServiceBinding"
contract="ServiceReference1.LmsBLQServicePortType"
name="LmsBLQServiceEndpoint" />
</client>
</system.serviceModel>
代碼調用最佳實踐
using (var client = new LmsBLQServiceClient("LmsBLQServiceEndpoint"))
{
client.Open();
try
{
// 設置超時
client.Endpoint.Binding.SendTimeout = TimeSpan.FromMinutes(5);
var result = client.UploadData(data);
client.Close();
}
catch (Exception ex)
{
client.Abort();
// 記錄異常詳情:ex.Message, ex.StackTrace, 內層異常
throw;
}
}
總結
「目標機積極拒絕連接」90% 是 **網路層面問題**(端口未監聽、防火牆攔截、IP 綁定錯誤),10% 是 **Endpoint 地址錯誤** 導致連接錯誤地址。排查順序:`telnet` 驗證連通性 → 確認服務端監聽狀態 → 核對配置文件 Endpoint 地址與 Binding → 檢查防火牆/安全組規則。