首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WCF 分散式開發常見錯誤 (21):IChannelListener 開啟失敗 — Message 安全模式缺少服務憑證

     編輯:關於.NET

錯誤現象

使用 NetTcpBinding、Message 安全模式與 UserName 認證進行自我宿主時,啟動 ServiceHost 即拋出:

The ChannelDispatcher at 'net.tcp://localhost:9001/WCFService' with contract(s) '"IWCFService"' is unable to open its IChannelListener.

移除配置文件中的 <security mode="Message"> 區塊後錯誤消失。

原因分析

WCF 規定:Message 安全模式強制要求服務端提供 X.509 憑證,用於加密 SOAP 訊息標頭與主體。配置文件中雖指定了 clientCredentialType="UserName",但缺少 <serviceCertificate> 設定,導致通道建構階段驗證失敗,無法開啟監聽器。

解決步驟

1. 建立憑證

makecert -r -pe -n "CN=FrankWCFServer" -ss My -sky exchange

憑證將存放於「目前使用者 → 個人」存儲區。

瀏覽器查看憑證

2. 服務配置文件添加 serviceCertificate

<serviceBehaviors>
  <behavior name="WCFService.WCFServiceBehavior">
    <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="false" />
    <serviceCredentials>
      <serviceCertificate x509FindType="FindBySubjectName" findValue="FrankWCFServer" storeLocation="CurrentUser"/>
      <clientCertificate>
        <authentication certificateValidationMode="None"/>
      </clientCertificate>
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFService.MyUserNamePasswordValidator,WCFService"/>
    </serviceCredentials>
  </behavior>
</serviceBehaviors>

3. 完整繫結配置參考

<wsHttpBinding>
  <binding name="MessageAndUserName">
    <security mode="Message">
      <message clientCredentialType="UserName"/>
    </security>
  </binding>
</wsHttpBinding>

驗證結果

加入服務憑證配置後,ServiceHost.Open() 成功,客戶端可正常呼叫驗證邏輯。

總結與心得

  • WCF Message 安全模式 強制要求服務憑證,這與舊版 ASMX/WSE 直接在 SOAP Header 傳遞明文帳密截然不同。
  • XP 環境下憑證建立、部署流程繁瑣,易成初學者絆腳石。
  • 建議開發階段將 certificateValidationMode="None" 以簡化信任鏈驗證,專注於業務邏輯驗證。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved