剛開始學習wcf,根據官方網站的說明寫下的代碼
第一步:
建立一個類庫項目GettingStartedLib,首先添加wcf引用System.ServiceModel; 添加接口ICalculator,添加類CalculatorService實現接口ICalculator
代碼:ICalculator
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace GettingStartedLib { [ServiceContract] //契約聲明 public interface ICalculator { [OperationContract] //操作契約 double Add(double n1, double n2); [OperationContract] double Subtract(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2); } }
代碼:CalculatorService
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GettingStartedLib { public class CalculatorService : ICalculator { public double Add(double n1, double n2) { double result = n1 + n2; Console.WriteLine("相加數:({0},{1})", n1, n2); // 輸出結果到控制台 Console.WriteLine("結果: {0}", result); return result; } public double Subtract(double n1, double n2) { double result = n1 - n2; Console.WriteLine("相減數:({0},{1})", n1, n2); Console.WriteLine("結果: {0}", result); return result; } public double Multiply(double n1, double n2) { double result = n1 * n2; Console.WriteLine("相乘數:({0},{1})", n1, n2); Console.WriteLine("結果: {0}", result); return result; } public double Divide(double n1, double n2) { double result = n1 / n2; Console.WriteLine("相除數:({0},{1})", n1, n2); Console.WriteLine("結果: {0}", result); return result; } } }
然后在這個類庫中添加app.config文件。剛開始做不知道文件格式,可以另建立一個wcf類庫服務,找到自動生成的配置文件當做模板,復制到當前的項目中,少做修改就行了。
代碼:app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" /> </system.web> <!-- 部署服務庫項目時,必須將配置文件的內容添加到 主機的 app.config 文件中。System.Configuration 不支持庫的配置文件。--> <system.serviceModel> <services> <service name="GettingStartedLib.CalculatorService"> <host> <baseAddresses> <add baseAddress = "http://localhost:8100" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否則地址將與上面提供的基址相關 --> <endpoint address="" binding="basicHttpBinding" contract="GettingStartedLib.ICalculator"> <!-- 部署時,應刪除或替換下列標識元素,以反映 用來運行所部署服務的標識。刪除之后,WCF 將 自動推斷相應標識。 --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元數據交換終結點供相應的服務用於向客戶端做自我介紹。 --> <!-- 此終結點不使用安全綁定,應在部署前確保其安全或將其刪除--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- 為避免泄漏元數據信息, 請在部署前將以下值設置為 false --> <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/> <!-- 要接收故障異常詳細信息以進行調試, 請將以下值設置為 true。在部署前設置為 false 以避免泄漏異常信息--> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
第二步:
建立服務啟動程序,這個創建一個控制台應用程序GettingStartedHost,首先添加wcf引用System.ServiceModel,添加GettingStartedLib類庫的引用。
代碼:GettingStartedHost
using GettingStartedLib; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Threading.Tasks; namespace GettingStartedHost { class Program { static void Main(string[] args) { // Step 1 創建一個基地址 Uri baseAddress = new Uri("http://localhost:8100/GettingStarted"); // Step 2 創建 ServiceHost 實例 ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); try { // Step 3 增加服務終結點類型 selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService"); // Step 4 允許媒體類型轉換. ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb); // Step 5 啟動服務. selfHost.Open(); Console.WriteLine("服務已經啟動."); Console.WriteLine("按Enter鍵終止服務."); Console.WriteLine(); Console.ReadLine(); // 關閉服務. selfHost.Close(); } catch (CommunicationException ce) { Console.WriteLine("異常出現: {0}", ce.Message); selfHost.Abort(); } } } }
第三步:
建立控制台應用程序GettingStartedClient,首先添加引用System.ServiceModel,和GettingStartedLib類庫
代碼:GettingStartedClient.Program
using GettingStartedClient.ServiceReference1; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GettingStartedClient { class Program { static void Main(string[] args) { //Step 1: 創建一個Wcf代理實例. CalculatorClient client = new CalculatorClient(); // Step 2: 請求服務操作. // 請求加法操作. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); //請求減法操作. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // 請求乘法操作. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // 請求除法操作. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); //Step 3:關閉服務 client.Close(); } } }
這里建立好客戶端程序還是不可以使用,需要與wcf服務添加到客戶端,我是通過vs的命令行工具 切換到客戶端的項目目錄輸入:
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service
,最后會自動生成引用文件。
界面:
第四步:
啟動應用測試:首先啟動Host程序,找到bin目錄打開生成的.exe程序,啟動服務。然后打開客戶端程序。
代碼下載:http://files.cnblogs.com/files/dearbeans/GettingStartedLib.rar