買了《WCF技術剖析》,按著書本的例子進行操作,寫下我的操作過程。
參考部落格:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html
步驟一 構建整個解決方案
步驟二 建立服務契約:ICalculator.cs
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />using System.ServiceModel;</p><p>namespace xuwei.WcfServices.Contracts//服務契約<br />{<br /> [ServiceContract(Name="CalculatorService",Namespace="http://blog.csdn.net/xw13106209")]//將介面定義成服務契約<br /> public interface ICalculator<br /> {<br /> [OperationContract]<br /> double Add(double x,double y);</p><p> [OperationContract]<br /> double Subtract(double x,double y);</p><p> [OperationContract]<br /> double Multiply(double x,double y);</p><p> [OperationContract]<br /> double Divide(double x, double y);</p><p> }<br />}
步驟三 建立服務:CalculatorService.cs
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />using xuwei.WcfServices.Contracts;</p><p>namespace xuwei.WcfServices.Services//實現服務契約<br />{<br /> public class CalculatorService:ICalculator<br /> {<br /> public double Add(double x,double y)<br /> {<br /> return x + y;<br /> }</p><p> public double Subtract(double x, double y)<br /> {<br /> return x - y;<br /> }</p><p> public double Multiply(double x, double y)<br /> {<br /> return x * y;<br /> }</p><p> public double Divide(double x, double y)<br /> {<br /> return x / y;<br /> }<br /> }<br />}<br />
步驟四 通過自我寄宿的方式寄宿服務:Hosting控制台中的program.cs
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />using xuwei.WcfServices.Contracts;<br />using xuwei.WcfServices.Services;<br />using System.ServiceModel.Description;<br />using System.ServiceModel;</p><p>namespace xuwei.WcfServices.Hosting//自主服務寄宿<br />{<br /> class Program<br /> {<br /> static void Main(string[] args)<br /> {<br /> using (ServiceHost host=new ServiceHost(typeof(CalculatorService)))<br /> {<br /> host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");<br /> if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)<br /> {<br /> ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();<br /> behavior.HttpGetEnabled = true;<br /> behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");<br /> host.Description.Behaviors.Add(behavior);<br /> }</p><p> host.Opened += delegate<br /> {<br /> Console.WriteLine("CalculatorService已經啟動,按任意鍵終止服務!");<br /> };</p><p> host.Open();<br /> Console.Read();<br /> }<br /> }<br /> }<br />}<br />
注意1:
完成以後需要編譯Hosting下的program.cs。但是在通過Ctrl+F5執行(其實可以通過右鍵解決方案->產生解決方案完成,不需要通過Ctrl+F5執行)的時候可能報錯:
無法直接啟動帶有“類庫輸出類型”的項目,如所示。
這時我們需要右鍵Hosting,然後選擇“設為啟動項目”,再次執行就不會報錯了。
注意2
在進行真正的WCF應用開發時,一般不會直接通過編碼的方式進行終結點的添加和服務行為的定義,而是通過配置的方式進行。上面添加終結點和定義服務行為的代碼可以通過如下方法進行。首先在Hosting項目中建立應用程式設定檔App.config,在App.config中添加如下配置:
<?xml version="1.0" encoding="utf-8" ?><br /><configuration><br /> <system.serviceModel><br /> <behaviors><br /> <serviceBehaviors><br /> <behavior name="metadataBehavior"><br /> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/calculatorservice/metadata" /><br /> </behavior><br /> </serviceBehaviors><br /> </behaviors><br /> <services><br /> <service behaviorConfiguration="metadataBehavior" name="xuwei.WcfServices.Services.CalculatorService"><br /> <endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" contract="xuwei.WcfServices.Contracts.ICalculator" /><br /> </service><br /> </services><br /> </system.serviceModel><br /></configuration>
如果採用了上訴的配置,服務寄宿代碼將會得到極大的精簡,只需包含下面幾行代碼:
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />using xuwei.WcfServices.Contracts;<br />using xuwei.WcfServices.Services;<br />using System.ServiceModel.Description;<br />using System.ServiceModel;</p><p>namespace xuwei.WcfServices.Hosting//自主服務寄宿<br />{<br /> class Program<br /> {<br /> static void Main(string[] args)<br /> {<br /> using (ServiceHost host=new ServiceHost(typeof(CalculatorService)))<br /> {<br /> //host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");<br /> //if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)<br /> //{<br /> // ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();<br /> // behavior.HttpGetEnabled = true;<br /> // behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");<br /> // host.Description.Behaviors.Add(behavior);<br /> //}</p><p> host.Opened += delegate<br /> {<br /> Console.WriteLine("CalculatorService已經啟動,按任意鍵終止服務!");<br /> };</p><p> host.Open();<br /> Console.Read();<br /> }<br /> }<br /> }<br />}<br />
步驟五 建立用戶端調用服務:Client中的program.cs
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />using xuwei.WcfService.Client.CalculatorServices;<br />namespace xuwei.WcfService.Client<br />{<br /> class Program<br /> {<br /> static void Main(string[] args)<br /> {<br /> using (CalculatorServiceClient proxy = new CalculatorServiceClient())<br /> {<br /> Console.WriteLine("x+y={2} when x={0} and y={1}",1,2,proxy.Add(1,2));<br /> Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, proxy.Subtract(1, 2));<br /> Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, proxy.Multiply(1, 2));<br /> Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, proxy.Divide(1, 2));<br /> Console.Read();<br /> }<br /> }<br /> }<br />}<br />
在執行步驟四以後E:/ms_workplace/WCF1/Hosting/bin/Debug目錄下會有一個“Hosting.exe”的應用程式,雙擊開啟該應用程式:
然後右鍵Client項目,選擇“加入服務參考”
點擊確定即可完成服務引用的添加,這時Client下就會多出一個Service Reference
雙擊CalculatorServices,在物件瀏覽器中能夠看到如下視圖
編譯Client,會在E:/ms_workplace/WCF1/Client/bin/Debug有Client.exe,雙擊開啟這個應用程式,會有如下結果: