WCF牛刀小试
1. 创建解决方案,结构如下:
首先,简单说明下5个程序集的作用:
WCFDataContracts 这是一个最底层的一个程序集,用于数据的序列化,格式化。这个程序集对于WCF来说不是必需的,该程序集成员需要用DataContract,DataMember特性标记。
WCFServiceContracts 如果程序中有用到DataContract,则该程序集将会调用到WCFDataContracts程序集。WCFServiceContracts主要用于提供一种契约,供Service,Client以及ServiceHost中的Endpoint来使用,该程序集成员需要用ServiceContract,OperationContract特性标记。
WCFServices 该程序集所有Contracts,实现ServiceContract中声明的需要提供的服务,以供远程调用。
WCFServiceHostPoint 该程序集用于托管WCFServices,为一宿主程序集。运行该程序集时,Service会被ServiceHost启动。其需要引用到ServiceContract和Service两部分的程序集。
ClientEntryPpoint 客户端运行的程序,远程调用Service,需要引用到所有Contracts,同时需要提供EndpointAddress来找到Service。
结构图如下:
2. 创建DataContract
using System.ServiceModel;
using WCFDataContracts;
namespace WCFServiceContracts
{
[ServiceContract]
public interface IWCFServiceContract
{
[OperationContract]
string GetNameViaString(string name);
[OperationContract]
string GetNameViaContract(WCFDataContract dataContractInstance);
}
}
3. 创建ServiceContract
using System.ServiceModel;
using WCFDataContracts;
namespace WCFServiceContracts
{
[ServiceContract]
public interface IWCFServiceContract
{
[OperationContract]
string GetNameViaString(string name);
[OperationContract]
string GetNameViaContract(WCFDataContract dataContractInstance);
}
}
4. 创建Service
using System;
using WCFDataContracts;
using WCFServiceContracts;
namespace WCFServices
{
public class WCFService:IWCFServiceContract
{
public string GetNameViaString(string name)
{
Console.WriteLine("Execute GetName with string param...");
return name;
}
public string GetNameViaContract(WCFDataContract dataContractInstance)
{
Console.WriteLine("Execute GetName with data contract param...");
return string.Format("{0} {1}", dataContractInstance.FirstName, dataContractInstance.SecondName);
}
}
}
5. 创建ServiceHost
using System;
using System.ServiceModel;
using WCFServiceContracts;
using WCFServices;
namespace WCFSserviceHostPoint
{
class WCFServiceHostPoint
{
static void Main()
{
using (ServiceHost theHost = new ServiceHost(typeof(WCFService)))
{
theHost.AddServiceEndpoint(typeof(IWCFServiceContract), new WSHttpBinding(), "http://localhost:7654/WCFServiceUri");
theHost.Opened += delegate
{
Console.WriteLine("The service host is running...");
};
theHost.Open();
Console.Read();
}
}
}
}
6. 创建Client
using System;
using System.ServiceModel;
using WCFDataContracts;
using WCFServiceContracts;
namespace ClientEntryPoint
{
class ClientEntryPoint
{
static void Main()
{
using (ChannelFactory<IWCFServiceContract> channelFactory = new ChannelFactory<IWCFServiceContract>(new WSHttpBinding()))
{
EndpointAddress address = new EndpointAddress("http://localhost:7654/WCFServiceUri");
IWCFServiceContract serviceInstance = channelFactory.CreateChannel(address);
Console.WriteLine("This is {0}", serviceInstance.GetNameViaString("Anders"));
WCFDataContract dataConstractInstance = new WCFDataContract() { FirstName = "Anders", SecondName = "Fan" };
Console.WriteLine("This is {0}", serviceInstance.GetNameViaContract(dataConstractInstance));
Console.ReadKey();
};
}
}
}
7. 编译解决方案,运行WCFServiceHostPoint以启动服务。
ServiceHost