在上一篇WCF服務配置中,文章講解了WCF的配置所須要的基本節點和屬性構造,可是對於初學者的咱們在編寫程序的時候,每每對這些節點的位置和屬性不是特別清楚,因此就致使咱們的因配置文件錯誤而不能運行服務程序。Visual studio 針對服務配置提供了一個可視化的配置界面(Microsoft Service Configuration Editor),極大的方便開發者進行服務配置,接下來將演示如何對一個WCF服務程序進行配置:html
1.新建解決方案WcfConfigTool,添加一個類庫類型的Service程序以及一個控制檯應用的Host程序,各文件的代碼以下圖所示:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
2.經過WCF服務配置編輯器(Microsoft Service Configuration Editor)來配置服務程序,選擇visual studio 菜單中的工具選項下的WCF服務配置編輯器,點擊便可打開。
![](http://static.javashuo.com/static/loading.gif)
3.打開編輯器後,選擇文件->新建配置,打開配置界面,咱們就能夠配置服務了。
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
4.配置服務的基地址,點擊左邊服務菜單項的主機選項,而後點擊右下角的新建按鈕添加基地址。
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
5.修改默認終結點的綁定類型爲wsHttpBinding,把標識中的DNS設置爲Localhost.
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
6.添加元數據終結點配置,選擇左側終結點菜單選項,右鍵選擇新建服務終結點。設置Address爲mex,Binding 設置爲mexHttpBinding,Contract設置爲IMetadataExchange
![](http://static.javashuo.com/static/loading.gif)
7.添加綁定配置,選擇左側的綁定菜單項,新建綁定配置
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
8.配置終結點行爲,選擇左側的高級選項的終結點行爲配置新建終結點行爲配置,將名稱設置爲endpointBehavior,點擊添加按鈕添加終結點行爲
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
9.添加服務行爲配置,選擇左側服務行爲菜單項新建服務行爲配置。設置名稱爲serviceBehavior,點擊添加按添加服務行爲。
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
10.爲服務選擇BehaviorConfiguration的選項爲serviceBehavior。點擊左側的Service.Service1選擇,將右側的BehaviorConfiguration選擇設置爲serviceBehavior
![](http://static.javashuo.com/static/loading.gif)
11.爲終結點選擇綁定配置和行爲配置,點擊左側的第一個終結點,將右側的BehaviorConfiguration設置爲endpointBehavior、BindingConfiguration設置爲binding1.
![](http://static.javashuo.com/static/loading.gif)
12.點擊文件菜單下的另存爲選項保存文件,將配置文件內容複製到先前建好的Host工程下的App.config文件中,編譯程序後,就能夠運行寄宿服務了,到此咱們就完成了一個服務程序配置任務。
![](http://static.javashuo.com/static/loading.gif)
13.能夠利用WCF客戶端驗證此服務是否發佈成功。在開始菜單的visual studio Tools下的命令提示Visual Studio命令提示(2010),輸入wcftestclient命令打開WCF測試客戶端
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)