使用WCF進行跨平臺開發之一(WCF的實現、控制檯託管與.net平臺的呼叫)1.建立專案結構2.契約的設計3.實現服務4.控制檯託管服務5.在.net平臺中呼叫WCF
WCF是Windows Communication Foundation的縮寫,是微軟發展的一組資料通訊的應用程式開發介面,它是.NET框架的一部分,是WinFx的三個重要開發類庫之一,其它兩個是WPF和WF。在本系列文章
(我現在計劃的應該是三篇,一篇WCF的開發和部署,另外是在.net平臺上呼叫它,第二篇是PHP呼叫,第三篇是JAVA呼叫)。
在本次的跨平臺整合通訊開發示例中,使用到的各種技術,咱且走且看,一邊開發一邊講解。
1.建立專案結構
使用VS2010一個名為IntergatedCommunication的空解決方案,在其下,新建Contracts、Implemention兩個類庫專案,分別為契約的設計與服務的實現,而後新建ConsoleHost、Client兩個控制檯應用程式,分別為在控制檯中實現服務託管使用,一個作為.net平臺上呼叫WCF的例項使用,如下圖
2.契約的設計
本例項我還是想讓它確實可以應用在實際專案中,所以我在設計的時候,將使用複雜型別(complex type),因為這並不同於普通型別,尤其在java和php在使用複雜型別引數是,呼叫方法是很不一樣的。
首先對Contracts、Implemention和ConsoleHost專案中新增對System.ServiceModel和System.Runtime.Serialization的引用。這兩個名稱空間中包含ServiceContractAttribute等WCF需要的契約特性類,和對複雜型別序列化的類DataContractSerializer。
本示例使用員工資訊(員工ID、員工姓名、所屬部門)查詢本員工上月的工資明細(員工ID、薪水、日期),所以首先建立兩個類Employee類和SalaryDetail類,在類中引用System.Runtime.Serialization名稱空間,並且,在類上新增DataContractAttribute並在每個類屬性上新增DataMemberAttribute:
Employee.cs
using System.Runtime.Serialization; namespace Contracts { [DataContract] public class Employee { [DataMember] public string Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string Department { get; set; } } }
SalaryDetail.cs
using System.Runtime.Serialization;
namespace Contracts
{
[DataContract]
public class SalaryDetail
{
[DataMember]
public string Id { get; set; }
[DataMember]
public decimal Salary { get; set; }
[DataMember]
public DateTime Date { get; set; }
}
}
以上所設計的是資料契約,在使用DataContract和DataMember修飾和類和屬性後,可將這些型別和屬性暴露在元資料中,而後設計服務契約
定義一個藉口名為IEmployeeManagement並新增一個方法簽名GetSalaryOfLastMonth,並新增ServiceContractAttribute和OperationContractAttribute。
IEmployeeManagement.cs
using System.ServiceModel;
namespace Contracts
{
[ServiceContract]
public interface IEmployeeManagement
{
[OperationContract]
SalaryDetail GetSalaryOfLastMonth(Employee emp);
}
}
3.實現服務
在Implemention中新增對Contracts專案的引用,新增EmployeeManagement類,實現IEmployeeManagement介面
EmployeeManagement.cs
using Contracts;
namespace Implemention
{
public class EmployeeManagement:IEmployeeManagement
{
public SalaryDetail GetSalaryOfLastMonth(Employee emp)
{
SalaryDetail salaryDetail = new SalaryDetail();
salaryDetail.Id = emp.Id;
salaryDetail.Date = DateTime.Now.AddMonths(-1);
salaryDetail.Salary = 20000;
return salaryDetail;
}
}
}
因為這裡實現的內容並不重要,所以沒有具體的去實現它,知識簡單的返回了一個SalaryDetail的例項,Id為傳入引數的員工ID,時間為當前時間的前一個月,薪水為固定的20000。
4.控制檯託管服務
在ConsoleHost中新增對以上兩個專案的引用,這時,生成整個解決方案,然後在ConsoleHost中新增應用程式配置檔案App.config。並使用WCF服務配置編輯器開啟它,並配置服務託管地址和繫結型別等資訊,最終配置結果為
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ExposeMetaDataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="EmployeeManagement/MEX" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ExposeMetaDataBehavior" name="Implemention.EmployeeManagement">
<endpoint address="EmployeeManagement"
binding="wsHttpBinding" bindingConfiguration="" contract="Contracts.IEmployeeManagement" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9876/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
開啟program.cs,在main方法中新增程式碼,託管服務
using System.ServiceModel;
using Implemention;
namespace ConsoleHost
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(Implemention.EmployeeManagement));
try
{
Console.WriteLine("EmployeeManagement Service Starting");
host.Open();
Console.WriteLine("EmployeeManagement Service Started");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (ex.InnerException != null)
{
Console.WriteLine("n" + ex.InnerException.Message);
}
}
finally
{
host.Close();
}
Console.ReadKey();
}
}
}
生成解決方案,並在VS外以管理員許可權啟動ConsoleHost.exe檔案,這樣就在控制檯中託管了服務
5.在.net平臺中呼叫WCF
在Client中,新增服務引用,名稱空間設定為ServiceReference
在program.cs中新增程式碼,呼叫控制檯中託管的服務
namespace Client
{
class Program
{
static void Main(string[] args)
{
ServiceReference.EmployeeManagementClient client = new ServiceReference.EmployeeManagementClient();
ServiceReference.Employee emp = new ServiceReference.Employee() { Id = "dev001", Name = "James White", Department = "Development" };
ServiceReference.SalaryDetail salaryDetail = client.GetSalaryOfLastMonth(emp);
Console.WriteLine("Employee number {0}'s salary of {1} month is ¥{2}", salaryDetail.Id, salaryDetail.Date.Month, salaryDetail.Salary);
Console.ReadKey();
}
}
}
在這裡,我們已經簡單的實現了WCF服務的實現和.net本平臺呼叫WCF,這一篇不是最重要的,下一篇是使用IIS託管WCF並使用PHP呼叫WCF。