slarkleoric
1、 WCF是SOA(面向服务的框架)的开发模板2、WCF的服务不能孤立的存在,必须寄宿于一个运行着的进程(称为宿主),为服务指定给宿主的过程称为服务寄宿(Service Hosting)。常用的寄宿方式:自我寄宿(Self-Hosting)和IIS寄宿。VS中的WCF模板项目,就是用的IIS寄宿。
3、创建Self-Hosting程序,要引用 System.ServiceModel.dll 。
创建服务契约接口:IService.cs 应用 System.ServiceModel.ServiceContractAtribute特性,定义成服务契约
创建服务:Service.cs ,继承IService.cs接口
服务寄宿:使用终结点(Endpoint),在进程中寄宿。终结点由:地址(Address)、绑定(Binding)、契约(Contact)组成。
4、在进程中的寄宿
using (ServiceHost host = new ServiceHost(typeof(CalculatorService))) {
host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(),"http://127.0.0.1:3721/calculatorservice");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://127.0.0.1:3721/calculatorservice/metadata");
host.Description.Behaviors.Add(behavior);
}
host.Opened += delegate
{
Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
};
host.Open();
Console.Read();
}
添加终结点的函数:
// // 摘要:
// 使用指定的协定、绑定和终结点地址将服务终结点添加到承载服务中。
//
// 参数:
// implementedContract:
// 所添加终结点的协定的 System.Type。
//
// binding:
// 所添加终结点的 System.ServiceModel.Channels.Binding。
//
// address:
// 所添加终结点的地址。
//
// 返回结果:
// 添加到承载服务中的 System.ServiceModel.Description.ServiceEndpoint。
//
// 异常:
// T:System.ArgumentNullException:
// implementedContract、binding 或 address 为 null。
public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address);
页:
[1]