上接 WCF學習之旅—WCF寄宿前的准備(八)
WCF學習之旅—WCF服務部署到IIS7.5(九)
五、控制台應用程序宿主
(1) 在解決方案下新建控制台輸出項目 ConsoleHosting。如下圖。

(2)添加 System.ServiceModel.dll 的引用。
(3)添加 WCF 服務類庫(WcfServiceLib)的項目引用。
(4)創建宿主程序,代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Threading.Tasks; using WcfServiceLib; namespace ConsoleHosting { class Program { static void Main(string[] args) { //創建宿主的基地址 Uri baseAddress = new Uri("http://localhost:8080/BookService"); //創建宿主 using (ServiceHost host = new ServiceHost(typeof(BookService), baseAddress)) { //向宿主中添加終結點 host.AddServiceEndpoint(typeof(IBookService), new WSHttpBinding(), baseAddress); if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) { //將HttpGetEnabled屬性設置為true ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; behavior.HttpGetUrl = baseAddress; //將行為添加到Behaviors中 host.Description.Behaviors.Add(behavior); //打開宿主 host.Opened += delegate { Console.WriteLine("BookService控制台程序寄宿已經啟動,HTTP監聽已啟動....,按任意鍵終止服務!"); }; host.Open(); Console.Read(); } } } } }
(5)運行宿主程序,在運行客戶端進行調用之前,需要先運行宿主程序。如下圖所示,則說明宿主建立成功。

建立客戶端
(1) 重新建立解決方案-->Windows窗體應用程序項目。如下圖。

(2)添加對服務的引用(在引用上右鍵-->輸入我們定義的服務宿主的基地址(此處為:http://localhost:8080/BookService)-->轉到-->確定),具體請看第一節。如下圖1,圖2。

圖1

圖2
(2) 在From1中添加一個文本框與一個按鈕。如下圖。

(3) 在From1.cs中寫如下代碼。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WinClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnGetBook_Click(object sender, EventArgs e) { BookServiceReference.BookServiceClient client = new BookServiceReference.BookServiceClient(); string book = client.GetBook("5"); textBox1.Text = book; } } }
(4) 測試程序如下圖所示說明成功(注意:一定要先運行我們的宿主程序才行,如果宿主沒有打開的話會報錯:由於目標計算機積極拒絕,無法連接。)。如下圖。

在這個示例中我們把Endpoint中的ABC,基地址,Behaviors等都直接寫在了代碼里,但實際應用過程中都是去依賴配置文件,為了對比說明我們下面的例子中會使用配置文件。
六、Windows應用程序宿主
(1) 在解決方案下新建Windows窗體應用程序項目 WinHosting。如下圖。

(2)添加 System.ServiceModel.dll 的引用。
(3)添加 WCF 服務類庫(WcfServiceLib)的項目引用。
(4)添加應用程序配置文件App.config,這次我們使用配置的方式進行WCF服務的公布,WCF服務配置代碼如下。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8080/BookService/metadata" /> <serviceDebug includeExceptionDetailInFaults="True" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="WcfServiceLib.BookService"> <endpoint address="http://127.0.0.1:8080/BookService" binding="wsHttpBinding" contract="WcfServiceLib.IBookService" /> </service> </services> </system.serviceModel> </configuration>
(5)創建宿主程序Form1窗體,並修改App.config,代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WcfServiceLib; namespace WinHosting { public partial class Form1 : Form { public Form1() { InitializeComponent(); } ServiceHost host = null; private void Form1_Load(object sender, EventArgs e) { try { host = new ServiceHost(typeof(BookService)); host.Opened += delegate { label1.Text="窗體宿主程序,BookService,使用配置文件!"; }; host.Open(); } catch (Exception ex) { label1.Text=ex.Message; } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { host.Close(); } } }
(6) 運行程序如下圖所示。

建立客戶端
使用我們在Console寄宿程序編寫的客戶端,去訪問Windows窗體宿主程序的WCF服務。
