1.簡介
WCF(Windows Communication Foundatio:Windows通信框架)是基於微軟.NET平台編寫的分布式應用的統一編程模型。
在WCF出現之前,常見的有以下三種分布式技術
webservice 基於http協議的soap模式
remoting 常用於tcp模式的二進制傳輸
MSMQ 這是一種分布式離線技術,用於業務解耦
因為分布式技術太多,使用不方便,需要整合,所以WCF應運而生。
WCF本質就是對上面技術的再次封裝。
2.開始我們的WCF之旅
新建一個控制台應用程序MyFirstWCF
添加一個WCF服務,命名為TeachService,這樣程序會自動添加一下內容,而且App.config也會新增<system.serviceModel>
ITeachService
using System.ServiceModel; namespace MyFirstWCF { [ServiceContract]//表示這個接口類遵循WCF協定 public interface ITeachService { [OperationContract]//表示這個方法是服務協定的一部分,只有添加了以上特性,才能保證接口被WCF運行時獲取到 string GetCourseName(int courseId);//這個方法是我們自己添加的,不使用自動生成的方法,自動生成的已刪除 } }
TeachService
namespace MyFirstWCF { public class TeachService : ITeachService { public string GetCourseName(int courseId) { return "WCT 通信技術";//做測試而已,不管什么參數,都打印這句話 } } }
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="MyFirstWCF.TeachService"> <endpoint address="" binding="basicHttpBinding" contract="MyFirstWCF.ITeachService"> <!--abc的概念--> <!--address【地址】,如果不寫,則使用下面的baseAddress【基礎地址】,基礎地址可以修改--> <!--binding【綁定】,表示通過哪一種通道--> <!--contract【契約】,表示具體使用的接口--> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8733/MyFirstWCF/TeachService/" /> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>
abc概念可以參考一下圖片
Program:
using System; using System.ServiceModel; namespace MyFirstWCF { class Program { static void Main(string[] args) { //使用ServiceHost啟動 ServiceHost host=new ServiceHost (typeof(TeachService)); host.Open(); Console.WriteLine("WCF start..."); Console.Read(); } } }
運行起來,會發生錯誤
HTTP 無法注冊 URL http://+:8733/MyFirstWCF/TeachService/。進程不具有此命名空間的訪問權限(有關詳細信息,請參見 http://go.microsoft.com/fwlink/?LinkId=70353)。”
解決方法:
1)以管理員身份運行vs
2)以管理員身份運行
結果:
成功運行
3.新建一個客戶端
只是為了演示,我們添加在同一個解決方案里面就可以了
還是一樣,添加一個控制台應用程序TestClientApp
在引用那里右鍵添加一個【服務引用】,地址為WCF的地址,因為上面地址為空,所以使用基礎地址
http://localhost:8733/MyFirstWCF/TeachService/
轉到的時候,WCF程序必須有在運行,不然就會報錯。
成功!命名空間可以自己定義。
App.config也會添加相應的內容,可以自己看一下,不用修改,就不展示了
Program如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestClientApp { class Program { static void Main(string[] args) { MyFirstMCF.TeachServiceClient client = new MyFirstMCF.TeachServiceClient();//TeachService映射到客戶端會自動加上Client后綴 string courseName = client.GetCourseName(1); Console.WriteLine("我們正在學習"+courseName); Console.Read(); } } }
結果:
成功獲取返回值!
4.FaultException錯誤傳遞
添加一個可能會報錯的函數,這里我們定義一個除法,我們知道,當除數為0,則函數報錯
ITeachService添加:
[OperationContract] int Division(int x, int y);
TeachService定義這個方法:
public int Division(int x, int y) { try { return x / y; } catch(Exception ex)//發送錯誤,用FaultException返回 { throw new FaultException(ex.Message); } }
記住,寫完后要重新生成一下,然后啟動服務
客戶端Program:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestClientApp { class Program { static void Main(string[] args) { MyFirstMCF.TeachServiceClient client = new MyFirstMCF.TeachServiceClient();try { int resu = client.Division(10, 0); Console.WriteLine("結果:" + resu); } catch (Exception ex) { Console.WriteLine("錯誤:"+ex.Message); } Console.Read(); } } }
這里,服務端會把錯誤返回給客戶端,客戶端直接打印即可。
5.三種操作
1)請求響應操作
默認情況下,服務契約中的操作都屬於“請求響應”操作,也就是上述的所有方法都是一個請求響應操作
2)單程操作(One-Way)
不需要服務端給出響應,即客戶端發送消息,並且得到服務端確認后,就理解結束本次操作調用的模式
如:
客戶端調用服務端的Void方法(方法名字上一般得加上IsOneWay=true)
[OperationContract(IsOneWay = true)] void SayHello();
3)雙程操作
6.binding
7.在IIS上部署wcf
首先,我們新建一個web程序,在程序里面添加wcf服務
web程序對應的wcf服務是一個網站,生成的配置文件也和控制台應用程序不一樣
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel>
在這里我們可以不管他,直接運行網站
執行結果:
然后,在別的程序添加服務引用的時候使用的是上面的鏈接
===》進入主題:如何把WCF部署到IIS
在IIS新建一個站點,然后把這個網站程序發布過去,運行
找到對應的WCF程序入口(就是上面的鏈接換上站點對應的端口號)
我們會發現程序報錯,訪問不了WCF服務
解決方法:
1)添加MIME類型
擴展名:.svc
MIME類型 :application/octet-stream
2)找到處理程序映射,添加托管處理程序
請求路經:*.svc
類型:System.ServiceModel.Activation.HttpHandler
名稱:svc-Integrated
好了,我們可以正常訪問了!!!