一、 WCF服務的創建
有兩種創建方式:
1.WCF服務庫
2.WCF服務應用程序
如下圖所示:
這里選擇WCF服務庫。注意事項:
1.WCF服務庫是一個類庫項目,這里選擇.net 3.5版本(版本高低可以會有一些沖突)。
2.因為是類庫,所以配置文件是App.config,要發布到IIS,就必須將其改名為web.config。用原來的IService接口和實現Service范例。然后在添加Web.config配置:
文件名:Web.config :
<?xml version="1.0"?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> <services> <service behaviorConfiguration="ServiceBehavior" name="WcfServiceLibrary1.Service1"> <endpoint binding="wsHttpBinding" bindingConfiguration="" contract="WcfServiceLibrary1.IService1"/> </service> </services> </system.serviceModel> </configuration>
特別主要要添加:
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
否則可能在
三、從IIS宿主引用WCF服務添加引用WCF服務的時候無法找到服務,出現如下錯誤信息:
下載“http://127.0.0.1:7293/WCFServerRefWCFLib.svc”時出錯。
請求因 HTTP 狀態 404 失敗: Not Found。
元數據包含無法解析的引用:“http://127.0.0.1:7293/WCFServerRefWCFLib.svc”。
沒有終結點在偵聽可以接受消息的 http://127.0.0.1:7293/WCFServerRefWCFLib.svc。這通常是由於不正確的地址或者 SOAP 操作導致的。如果存在此情況,請參見 InnerException 以了解詳細信息。
遠程服務器返回錯誤: (404) 未找到。
如果該服務已在當前解決方案中定義,請嘗試生成該解決方案,然后再次添加服務引用。
3.添加svc文件。
選擇添加類,找不到svc類型的文件模板。可以直接輸入”文件名+.svc“,例如WCFServerUseWCF.svc,點“確定”創建一個svc格式的文件。清除自動生成的WCFServerUseWCF.svc的內容。添加如下代碼:
文件名:WCFServerUseWCF.svc
<%@ ServiceHost Language="C#" Service="WcfServiceLibrary1.Service1"%>
還有一種方式不是直接讓IIS訪問WCF類庫項目,而是新建一個網站,方法如下:
在解決方案節點--右鍵-新建網站--在彈出框中選擇,WCF服務。如下圖:
然后添加對WCF類庫的應用。引用完成之后會在Bin中看到引用了WCF類庫的dll,如下圖所示:
然后是添加web.config和.svc文件和內容,與上面完全一樣的做法。
二、IIS作為宿主,發布WCF服務
確保已經安裝IIS!
步驟1:
"我的電腦"右鍵--“管理”--“服務和應用程序”--“Interner信息服務(IIS)管理器”--“網站”右鍵,添加一個網站。如下圖:
步驟2:
啟動網站和
瀏覽http://127.0.0.1:7293/WCFServerUseWCFLib.svc
如果發布成功,會在瀏覽器中有如下畫面:
點擊:http://127.0.0.1:7293/WCFServerUseWCFLib.svc?wsdl,會在瀏覽器中有如下畫面:
三、從IIS宿主引用WCF服務
步驟1:
創建一個項目,這里選擇WPF應用程序。
步驟2:
右鍵“WPF應用程序”-添加服務引用 --地址,輸入:http://127.0.0.1:7293/WCFServerUseWCFLib.svc--點擊“前往”按鈕,發現WCF服務.重命名服務命名空間(這里命名為:WCFServerUseWCFLib)。
如下圖所示配置:
點擊“確定”按鈕;
引用成功后,App.config 文件自動添加了WCF Client端的配置,如下所示:
文件名:App.config

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" /> </security> </binding> <binding name="WSHttpBinding_IService11" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="http://127.0.0.1:7293/WCFServerUseWCFLib.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" contract="WCFServerUseWCFLib.IService1" name="WCFServerUseWCFLibIService"> <identity> <servicePrincipalName value="host/weiMe-PC" /> </identity> </endpoint> <endpoint address="http://127.0.0.1:7294/WCFServerRefWCFLib.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService11" contract="WCFServerRefWCFLib.IService1" name="WCFServerRefWCFLibIService"> <identity> <servicePrincipalName value="host/weiMe-PC" /> </identity> </endpoint> </client> </system.serviceModel> </configuration>
步驟3:
導入命名空間:
using WpfApplication1.WCFServerUseWCFLib;
調用WCF服務(這里調用方法 :
string GetData(int)
private void BtnWCFLibToIIS_OnClick(object sender, RoutedEventArgs e) { using (WCFServerUseWCFLib.Service1Client proxy = new WCFServerUseWCFLib.Service1Client()) { MessageBox.Show(this.BtnWCFWebRefWCFLibTOIIS.Content.ToString() + proxy.GetData(2)); } }
如果要客戶端要使用異步編程,可以讓客戶端代理支持異步編程,做法是:
1.右鍵WCF服務的引用---選擇“配置服務引用”。
2.在彈出的對話框中,勾選“生成異步操作”--點擊“確定”按鈕。
四、解決方案
【The End】