0x00 前言Windows Communication Foundation (WCF)是用於在.NET Framework中構建面向服務的應用程序的框架。本文將要介紹WCF開發的相關內容,為後續介紹的內容作鋪墊。
0x01 簡介本文將要介紹以下內容:
◼使用basicHttpBinding實現WCF
◼使用NetTcpBinding實現WCF
◼通過命令行實現WCF
◼通過IIS實現WCF
◼通過服務實現WCF
0x02 基礎知識參考資料:
https://docs.microsoft.com/en-us/dotnet/framework/wcf/whats-wcf
常用的傳輸協議:
◼HTTP,http://localhost:8080/
◼TCP,net.tcp://localhost:8080/
◼IPC,net.pipe://localhost/
常用的Binding:
◼BasicHttpBinding
◼WSHttpBinding
◼NetTcpBinding
◼NetNamedPipeBinding
元數據發布(metadata exchange),簡稱MEX
WCF默認禁用MEX,這樣能夠避免數據洩露
本著逐步深入的原則,本系列文章選擇先介紹開啟MEX的用法,這樣能夠提高客戶端開發的效率,禁用MEX的用法將放在下篇文章進行介紹。
0x03 使用basicHttpBinding實現WCF本節採用命令行實現WCF的方式作為示例
開發工具:Visual Studio 2015
1.服務端編寫(1)新建項目
選擇Visual C#-Console Application,名稱為basicHttpBindingWCFServer
(2)新建WCF服務
選擇Add-New Item.選擇WCF Service,名稱為Service1.cs
(3)修改service1.cs
添加DoWork的實現代碼,代碼示例:
(4)修改Program.cs
添加引用System.ServiceModel
添加啟動代碼,代碼示例:
(5)編譯運行
命令行輸出服務地址:http://localhost:8733/Design_Time_Addresses/basicHttpBindingWCFServer/Service1/
服務地址也可以在工程中的App.config查看
(6)測試
此時開啟了MEX,可選擇以下方法進行測試:
通過瀏覽器訪問服務地址:http://localhost:8733/Design_Time_Addresses/basicHttpBindingWCFServer/Service1/,能夠返回服務信息
使用WcfTestClient進行測試,默認路徑:C:\Program Files(x86)\Microsoft Visual Studio 14\Common7\IDE\WcfTestClient.exe,連接http://localhost:8733/Design_Time_Addresses/basicHttpBindingWCFServer/Service1/,調用DoWork(),此時服務端命令行輸出Run Server.DoWork(),方法調用成功
使用Svcutil生成客戶端配置代碼,命令示例:svcutil.exe http://localhost:8733/Design_Time_Addresses/basicHttpBindingWCFServer/Service1//out:1.cs,相關代碼可參考:https://github.com/dotnet/samples/tree/main/framework/wcf/Basic/Binding/Net/Tcp/Default/CS
注:
App.config由Visual Studio自動生成,服務地址由App.config隨機指定,這裡也可以通過代碼的方式指定服務地址,不需要依賴App.config,方法如下:
Program.cs示例:
App.config示例:
2.客戶端編寫(1)新建項目
選擇Visual C#-Console Application,名稱為basicHttpBindingWCFClient
(2)引用服務
選擇Add-Service Reference.
填入URL:http://localhost:8733/Design_Time_Addresses/basicHttpBindingWCFServer/Service1/
(3)修改Program.cs
代碼示例:
(4)編譯運行
此時服務端命令行輸出Run Server.DoWork(),方法調用成功
0x04 使用NetTcpBinding實現WCF本節採用命令行實現WCF的方式作為示例
1.服務端編寫(1)新建項目
選擇Visual C#-Console Application,名稱為NetTcpBindingWCFServer
(2)新建WCF服務
選擇Add-New Item.選擇WCF Service,名稱為Service1.cs
(3)修改service1.cs
添加DoWork的實現代碼,代碼示例:
(4)修改Program.cs
添加引用System.ServiceModel
添加啟動代碼,代碼示例:
(5)修改App.config
Line10: serviceMetadata httpGetEnabled='true' httpsGetEnabled='true' /
修改為: serviceMetadata httpGetEnabled='false' httpsGetEnabled='false' /
Line17: endpoint address='' binding='basicHttpBinding' contract='NetTcpBindingWCFServer.IService1'
修改為: endpoint address='' binding='netTcpBinding' contract='NetTcpBindingWCFServer.IService1'
Line22: endpoint address='mex' binding='mexHttpBinding' contract='IMetadataExchange' /
修改為: endpoint address='mex' binding='mexTcpBinding' contract='IMetadataExchange' /
Line25: add baseAddress='http://localhost:8733/Design_Time_Addresses/NetTcpBindingWCFServer/Service1/' /
修改為: add baseAddress='net.tcp://localhost:1111/Design_Time_Addresses/NetTcpBindingWCFServer/Service1/' /
完整代碼示例:
(6)編譯運行
命令行輸出服務地址:net.tcp://localhost:1111/Design_Time_Addresses/NetTcpBindingWCFServer/Service1/
(7)測試
此時開啟了MEX,可選擇以下方法進行測試:
使用WcfTestClient進行測試,默認路徑:C:\Program Files(x86)\Microsoft Visual Studio 14\Common7\IDE\WcfTestClient.exe,連接net.tcp://localhost:1111/Design_Time_Addresses/NetTcpBindingWCFServer/Service1/,調用DoWork(),此時服務端命令行輸出Run Server.DoWork(),方法調用成功
使用Svcutil生成客戶端配置代碼,命令示例:svcutil.exe net.tcp://localhost:1111/Design_Time_Addresses/NetTcpBindingWCFServer/Service1//out:1.cs,相關代碼可參考:https://github.com/dotnet/samples/tree/main/framework/wcf/Basic/Binding/Net/Tcp/Default/CS
2.客戶端編寫(1)新建項目
選擇Visual C#-Console Application,名稱為NetTcpBindingWCFClient
方法同0x03中的2.客戶端編寫
0x05 通過IIS實現WCF本節僅以服務端編寫作為示例,客戶端編寫同命令行實現的方法一致
1.服務端編寫(1)新建項目
選擇Visual C#-WCF-WCF Service Library,名稱為WcfServiceLibrary1
(2)發布
選中項目,右鍵-Publish.設置Target location為c:\wcfdemo
(3)在IIS管理頁面下新建網站
設置以下參數:
◼Site name:wcfdemo
◼Physical path:c:\wcfdemo
◼IP address: All unassigned
◼Port:81
選中網站wcfdemo,進入Content View
選中WcfServiceLibrary1.Service1.svc,右鍵-Browse,得到URL:http://localhost:81/WcfServiceLibrary2.Service1.svc
(4)測試
此時開啟了MEX,可選擇以下方法進行測試:
通過瀏覽器訪問服務地址:http://localhost:81/WcfServiceLibrary2.Service1.svc,能夠返回服務信息
使用WcfTestClient進行測試,默認路徑:C:\Program Files(x86)\Microsoft Visual Studio 14\Common7\IDE\WcfTestClient.exe,連接http://localhost:81/WcfServiceLibrary2.Service1.svc,調用GetData(),獲得返回值,方法調用成功
使用Svcutil生成客戶端配置代碼,命令示例:svcutil.exe http://localhost:81/WcfServiceLibrary2.Service1.svc,相關代碼可參考:https://github.com/dotnet/samples/tree/main/framework/wcf/Basic/Binding/Net/Tcp/Default/CS
0x06 通過服務實現WCF本節僅以服務端編寫作為示例,客戶端編寫同命令行實現的方法一致
1.使用basicHttpBinding實現服務端
(1)新建項目
選擇Visual C#-Console Application,名稱為WCFService
(2)新建Windows Service
選擇Add-New Item.選擇Windows Service,名稱為Service1.cs
(3)設置服務信息
選中Service1.cs,右鍵-Add Installer
項目中自動創建ProjectInstaller.cs文件,該文件會添加倆個組件serviceProcessInstaller1和serviceInstaller1
選中serviceProcessInstaller1組件,查看屬性,設置account為LocalSystem
選中serviceInstaller1組件,查看屬性,設置ServiceName為VulServiceTest1
(4)編輯Program.cs
(5)啟動服務
編譯生成WCFService.exe
安裝服務:
啟動服務:
補充:卸載服務
(6)測試
此時開啟了MEX,可選擇以下方法進行測試:
通過瀏覽器訪問服務地址:http://localhost:1112/TestService,能夠返回服務信息
使用WcfTestClient進行測試,默認路徑:C:\Program Files(x86)\Microsoft Visual Studio 14\Common7\IDE\WcfTestClient.exe,連接http://localhost:1112/TestService,調用RunMe(),在str對應的Value輸入calc,執行後啟動system權限的calc,方法調用成功
使用Svcutil生成客戶端配置代碼,命令示例:svcutil.exe http://localhost:1112/TestService /out:1.cs,相關代碼可參考:https://github.com/dotnet/samples/tree/main/framework/wcf/Basic/Binding/Net/Tcp/Default/CS
2.使用NetTcpBinding實現服務端
方法同上,區別在於Program.cs,示例代碼如下:
注:
服務端設置了HttpGetUrl:http://localhost:1114/TestService
0x07 小結本文介紹了在啟用元數據發布(MEX)時WCF開發的相關內容,下一篇將要介紹關閉元數據發布(MEX)時WCF開發的相關內容。
Recommended Comments