Authentication failed because the remote party has closed the transport stream.
在添加服務引用(HTTPS + mex)時發生,導致無法獲取元數據。
核心原因:憑證配置不正確,具體表現為:
使用 makecert(需 Visual Studio 命令提示字元):
rem 伺服器憑證
makecert -sr localmachine -ss My -n "CN=WCFServerPK" -sky exchange -pe -r
rem 客戶端憑證
makecert -sr localmachine -ss My -n "CN=WCFClientPK" -sky exchange -pe -r
-sky exchange 關鍵:標記私鑰可用於金鑰交換(加密/解密),否則會報「must have a private key capable of key exchange」。
執行 mmc → 加入「憑證」嵌入式管理單元(電腦帳戶)→ 個人 → 所有任務 → 匯出 → 勾選「匯出私鑰」 → 設定密碼 → 儲存為 .pfx。
Windows Server 2003 / XP (HttpCfg.exe):
httpcfg set ssl -i 0.0.0.0:9001 -h 9174185b2860b6d5ec3de133d5fcc4e1419b09e5
Vista / Server 2008+ (netsh):
netsh http add sslcert ipport=0.0.0.0:9001 certhash=9174185b2860b6d5ec3de133d5fcc4e1419b09e5 appid={11111111-2222-3333-4444-qqqqqqqqqqqq}
certhash 為憑證指紋(Thumbprint),可用 httpcfg query ssl 或 netsh http show sslcert 查看。
<service behaviorConfiguration="WCFService.WCFServiceBehavior" name="WCFService.WCFService">
<endpoint address="WCFService" binding="wsHttpBinding" bindingConfiguration="BindingConfiguration" contract="WCFService.IWCFService"/>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses><add baseAddress="https://computer:9001/"/></baseAddresses>
</host>
</service>
<wsHttpBinding>
<binding name="BindingConfiguration">
<security mode="Transport">
<transport clientCredentialType="Certificate"/>
</security>
</binding>
</wsHttpBinding>
<serviceBehaviors>
<behavior name="WCFService.WCFServiceBehavior">
<serviceMetadata httpsGetEnabled="true"/>
<serviceCredentials>
<serviceCertificate storeName="My" x509FindType="FindBySubjectName" findValue="WCFServerPK" storeLocation="LocalMachine"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
mexHttpsBinding 且 httpsGetEnabled="true"。