近期的開發要求使用 IIS 承載的 WCF 提供資料和實現操作,用戶端使用 Ajax Json 和 WinForm,因此要在統一服務上同時綁定用於 Ajax 的 webHttpBinding 和用於 WinForm 的 wsHttpBinding。
下面假定服務名為 Itoro.WebCore.Web。
在建立服務時,選擇“啟用了 AJAX 的 WCF 服務”,然後在 web.config 會自動在 system.serviceModel 下產生相關配置,預設的用於 Ajax 的配置是行為中的名為 Itoro.WebCore.WebAspNetAjaxBehavior 的終點行為,並且使用 <enableWebScript /> 啟用 Ajax Javascript 支援。在服務配置中是一個名為 Itoro.WebCore.Web 的服務,其中的終點配置為使用 Itoro.WebCore.WebAspNetAjaxBehavior 行為並綁定到 webHttpBanding。
接下來是手動修改配置以支援 WinForm 的用戶端訪問,即 wsHttpBinding。
在 system.serviceModel/behaviors/serviceBehaviors 下添加如下內容(如不存在 serviceBehaviors 則在相應位置建立該元素):
<behavior name="Itoro.WebCore.WebBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
該配置指定名為 Itoro.WebCore.WebBehavior 的服務行為啟用 Http 下的服務中繼資料。
接下來修改名為 Itoro.WebCore.Web 的服務(即 <service name="Itoro.WebCore.Web" /> 元素)為以下內容:
<service behaviorConfiguration="Itoro.WebCore.WebBehavior" name="Itoro.WebCore.Web">
<endpoint address="web" behaviorConfiguration="Itoro.WebCore.WebAspNetAjaxBehavior"
binding="webHttpBinding" contract="Itoro.WebCore.Web" />
<endpoint address="ws" binding="wsHttpBinding" contract="Itoro.WebCore.Web">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
其中添加 service 元素中的 behaviorConfiguration,添加了2個終點配置,分別為用於 WinForm 的 wsHttpBinding 和用於中繼資料的 mexHttpBinding。另外,修改 webHttpBinding 和 wsHttpBinding 的 address 屬性分別的 web 和 ws(也可以自訂,但是一定要設定,而且不能相同,否則會因為不同類型的綁定監聽同一 Uri 而發生錯誤)。
完整的配置如下:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="Itoro.WebCore.WebAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Itoro.WebCore.WebBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="Itoro.WebCore.WebBehavior" name="Itoro.WebCore.Web">
<endpoint address="web" behaviorConfiguration="Itoro.WebCore.WebAspNetAjaxBehavior"
binding="webHttpBinding" contract="Itoro.WebCore.Web" />
<endpoint address="ws" binding="wsHttpBinding" contract="Itoro.WebCore.Web">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
最後在服務使用上也要有些變動,假設我們的服務為 http://localhost/Web.svc,那麼在 Ajax 網站上添加服務時應該使用 Web.svc/web,而產生的指令碼引用地址為 Web.svc/web/js,其中的 web 即為 webHttpBinding 中的 address。在 WinForm 中可以直接通過 http://localhost/Web.svc 加入服務參考,在產生的 app.config 中 /configuration/system.serviceModel/client 下會有2個終點配置,其中一個是 WebHttpBinding_Web,另外一個是 WSHttpBinding_Web,因此在使用時必須指定終點為 WSHttpBinding_Web,即 WebClient client = new WebClient("WSHttpBinding_Web") 否則運行時會出現錯誤。
最後,因為 Web 是無狀態的,而 WinForm 是有狀態的,所以在 Web 下需要使用 Cookie(包括 Session),而在 WinForm 下只要 WebClient client 沒有銷毀就會一直保留同一狀態,因為在編寫服務的時候也要考慮相容這兩種模式。