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; }
}
}
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; }
}
}
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;
}
}
}
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。