一、開發環境
作業系統:Windows 10
開發環境:VS2015
程式設計語言:C#
IIS版本:10.0.0.0
二、添加WCF服務、Internet Information Services(IIS)
1、進入“控制台”, 打開“程式和功能”, 點擊左上角的“啟用或關閉Windows功能”後, 在“.NET Framework 4.6 高級服務”中的子節點選中“WCF 服務”, 如下圖所示:
2、再找到“Internet Information Services”, 同樣選中該節點, 如下圖所示:
3、點擊“確定”按鈕以便安裝這些服務與元件, 等待完成安裝即可。
三、新建一個WCF服務庫
1、使用VS2015新建一個WCF服務庫, 並將專案名稱改為“MyWCFService”, 如下圖所示:
2、在解決方案資源管理器裡將“IService1”介面、“Service1”類分別重命名為“IMyWCFService”與“MyWCFService”, 如下圖所示:
VS2015將會分別創建“IService1”介面、“Service1”類, “IService1”介面含GetData、GetDataUsingDataContract方法, 並使用“Service1”類實現了該介面, 代碼如下:
“IService1”介面:
// 注意: 使用“重構”功能表上的“重命名”命令, 可以同時更改代碼和設定檔中的介面名“IMyWCFService”。 [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // TODO: 在此添加您的服務操作 } // 使用下面示例中說明的資料約定將複合類型添加到服務操作。 [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } }“Service1”類:
// 注意: 使用“重構”功能表上的“重命名”命令, 可以同時更改代碼和設定檔中的類名“Service1”。 public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } }3、將滑鼠移到解決方案資源管理器中專案“MyWCFService”上並右擊滑鼠, 彈出上下文功能表, 在功能表中選中“發佈”後, 彈出下圖所示的“發佈 WCF服務”對話方塊,
在目標位置選擇“D:\WCF”, 其他按默認, 點擊“發佈”按鈕, 即可在“D:\WCF”資料夾裡生成如下圖所示的文件:
四、新建一個WCF服務網站
1、點擊打開IIS, 新建一個網站, 網站名稱設置為“MyWCFService”, 物理位址選擇“D:\WCF”, 埠從默認的80改為81, 如下圖所示:
2、點擊確定後, 即新建一個WCF服務網站, 我們可以在流覽器輸入“http://localhost:81/MyWCFService.MyWCFService.svc”進行驗證, 如下圖所示:
五、新建一個控制台用戶端測試WCF服務
1、在原來的解決方案裡新建一個專案名為“WCFTestClient”的控制台程式,如下圖所示:
2、滑鼠右鍵點擊專案“WCFTestClient”,在彈出的上下文功能表中選擇“設為啟動項目”,如下圖所示:
3、滑鼠右鍵點擊專案“WCFTestClient”子節點“引用”,在彈出的上下文功能表中選擇“添加服務引用”,如下圖所示:
4、將彈出“添加服務引用”對話方塊,在“地址”輸入剛剛建的WCF服務網站位址(http://localhost:81/MyWCFService.MyWCFService.svc),點擊“轉到”按鈕之後,將會在“服務”清單裡列出剛才建的服務,選擇“IMyWCFService”後,將會在“操作”列表裡列出GetData、GetDataUsingDataContract方法,如下圖所示:
5、將命名空間改為“MyWCFReference”後點擊“確定”按鈕之後,將會在項目“WCFTestClient”子節點增加“Service References”等,如下圖所示:
6、在Program類上方添加對WCF服務引用,並在Main主函數裡輸入以下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using WCFTestClient.MyWCFReference; namespace WCFTestClient { class Program { static void Main(string[] args) { MyWCFServiceClient client = new MyWCFServiceClient; Console.WriteLine(client.GetData(123456)); CompositeType cType = new CompositeType { StringValue = "Hello World!", BoolValue = true }; Console.WriteLine(client.GetDataUsingDataContract(cType).StringValue); } } }六、運行用戶端控制台程式
運行“WCFTestClient”用戶端控制台程式,如下圖所示:
五、新建一個控制台用戶端測試WCF服務
1、在原來的解決方案裡新建一個專案名為“WCFTestClient”的控制台程式,如下圖所示:
2、滑鼠右鍵點擊專案“WCFTestClient”,在彈出的上下文功能表中選擇“設為啟動項目”,如下圖所示:
3、滑鼠右鍵點擊專案“WCFTestClient”子節點“引用”,在彈出的上下文功能表中選擇“添加服務引用”,如下圖所示:
4、將彈出“添加服務引用”對話方塊,在“地址”輸入剛剛建的WCF服務網站位址(http://localhost:81/MyWCFService.MyWCFService.svc),點擊“轉到”按鈕之後,將會在“服務”清單裡列出剛才建的服務,選擇“IMyWCFService”後,將會在“操作”列表裡列出GetData、GetDataUsingDataContract方法,如下圖所示:
5、將命名空間改為“MyWCFReference”後點擊“確定”按鈕之後,將會在項目“WCFTestClient”子節點增加“Service References”等,如下圖所示:
6、在Program類上方添加對WCF服務引用,並在Main主函數裡輸入以下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using WCFTestClient.MyWCFReference; namespace WCFTestClient { class Program { static void Main(string[] args) { MyWCFServiceClient client = new MyWCFServiceClient; Console.WriteLine(client.GetData(123456)); CompositeType cType = new CompositeType { StringValue = "Hello World!", BoolValue = true }; Console.WriteLine(client.GetDataUsingDataContract(cType).StringValue); } } }六、運行用戶端控制台程式
運行“WCFTestClient”用戶端控制台程式,如下圖所示: