標籤:wcf
在VS2010裡建立一個最簡單的WCF服務,基本流程如下:
一:建立WCF應用首先,建立一個WCF服務的應用,如所示
建立完成之後,VS將自動產生一個最簡單的WCF工程,在這個應用中,包含了最基本
Contract、Service。
工程如下:
不需要編輯任何檔案,直接編譯產生,得到一個WcfService1.dll檔案
二、WCF應用中的契約(Contract)在產生的WCF工程中,IService1.cs中為
Contract(本例中契約和服務放在同一個工程下了,實際上也可以分為兩個工程),代碼如下:
[ServiceContract]public interface IService1{ [OperationContract] string GetData(int value); [Operation Contract] CompositeType GetDataUsingDataContract(CompositeType composite);// TODO: Add your service operations here} // Use a data contract as illustrated in the sample below to add composite types to service operations.[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 {string Value = value; } }}
可以看到,聲明了服務契約IService1,以介面形式聲明,其中還包括兩個操作契約GetData以及GetDataUsingDataContract。還聲明了資料契約CompositeType,以類的形式聲明,包含兩個資料成員BoolValue和StringValue。
三、WCF應用中的服務功能實現(ServiceBehavior)在產生的WCF應用中,Service1.svc.cs中為
“ServiceBehavior”(本例中契約和服務放在同一個工程下了,實際上也可以分為兩個工程),代碼如下(Service1.svc還有其它作用,後面再說):
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; }}
可以看到,這個Service1類實現了在契約中聲明的IService1介面(服務契約),也使用到了CompositeType類(資料契約);實現了GetData以及GetDataUsingDataContract這兩個服務契約中功能,這些功能即為WCF服務允許外部程式進行調用的功能。
四、寄宿(Host)WCF服務有2種常見的寄宿方式: 1)一種是為一組WCF服務建立一個託管的應用程式,通過手工啟動程式的方式對服務進行寄宿,所有的託管的應用程式均可作為WCF服務的宿主,比如 Console應用、Windows Forms應用和ASP.NET應用等,我們把這種方式的服務寄宿方式稱為自我寄宿(Self Hosting)。 2)另一種則是通過作業系統現有的進程啟用方式為WCF服務提過宿主,Windows下的進程啟用手段包括IIS、Windows Service或者WAS(Windows Process Activation Service)等 無論採用哪種寄宿方式,在為某個服務建立 ServiceHost的過程中,WCF架構內部會執行一系列的操作,其中最重要的步驟就是為服務建立服務描述(Service Description) 本例以第一種為例,建立一個WinForm應用作為託管程式,在VS2010中,建立一個普通的WinForm程式(以frameword4.0為例),如:
在工程中,添加對System.ServiceModel的引用,並添加對剛才產生的WcfService1.dll的引用,然後在Form上添加一個Button和一個Label控制項,如:
在WinForm工程中添加對要託管的WCF服務的描述,既可以通過代碼實現,也可以通過工具產生。本例在VS2010下,通過Tools-WCF Service Configeration Editor工具產生。點開後,通過菜單的File-New Config建立一個描述檔案,然後點擊Service頁面的Create a New Service,彈出如下介面:
點擊Browse...按鈕,選中剛才產生的WcfService1.dll,並選中其中的Service1服務,點擊下一步,彈出如下介面:
這個介面中要選擇使用的契約,預設即可,點擊下一步,彈出如下介面:
這個介面中要選擇使用的網路通訊協議,選擇最通用的HTTP協議,並在下一個介面中選擇基本的Http服務(Basic WebService interoperablitity),
在接下來的介面中,輸入訪問地址,其它程式可以通過這個地址來訪問本WCF服務。
完成這一步之後,接下來還有一些設定,我也不清楚用途,略去,最後點擊File-Save,產生一個App.config檔案。這裡把該檔案貼上來:
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="NewBehavior0"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8585/wcf1/metadata" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="NewBehavior0" name="WcfService1.Service1"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="ep1" contract="WcfService1.IService1" /> <host> <baseAddresses> <add baseAddress="http://localhost:8585/wcf1" /> </baseAddresses> </host> </service> </services> </system.serviceModel></configuration>
把該App.config檔案放在與WinForm工程的根目錄下(與Form1.cs同一目錄),還需要在VS中將該檔案加入到工程中。
添加Button控制項的處理函數:
private void button1_Click(objectsender, EventArgs e){ System.ServiceModel.ServiceHosthost =new System.ServiceModel.ServiceHost(typeof(WcfService1.Service1)); host.Open(); this.label1.Text= "opened";}
編譯運行,點擊介面中的Button控制項,即可以將該WCF服務寄宿到該Frm中(關閉表單後WCF服務結束)。
註:如果運行時報這個錯誤(HTTP could not register URL http://+:8585/wcf1/. Your process does not have access rights to this namespace),有可能是許可權不足導致,需要使用管理員權限運行。
五、在用戶端訪問並調用WCF服務建立一個WinFrm工程(不要關閉剛才的寄宿介面),在VS菜單中點擊Project-Add Service Reference..項,輸入剛才在寄宿介面中定義的地址,就可以添加對該WCF服務的引用,如:
在程式中使用如下代碼,即可以調用WCF服務中的GetData方法:
ServiceReference1.Service1Clientaa=newServiceReference1.Service1Client();MessageBox.Show(aa.GetData(2));
六、總結WCF一個簡單的應用,剛剛接觸,以後還有更多機會去深入學習。
WCF--建立簡單的WCF服務