WCF分布式开发步步为赢(3)WCF服务元数据交换、配置及编程开发
【4】WCF服务元数据交换编程实现过程详解:
以上的配置文件实现的WCF服务的元数据交换,同样我们也可以通过编程方式实现。前者配置简单、快捷,后者相对复杂。但是编程方式允许代码运行时控制或者设置元数据交换的信息。因而更加灵活。下面我们就来看看如何通过代码实现刚才的服务原数据交换的配置。
【4.1】WCF服务元数据交换HTTP-GET编程实现:
必须添加对命名空间的引用, using System.ServiceModel.Description;我们对服务元数据操作的类和接口信息定义在此命名空间里,具体的实现HTTP-GET的代码如下:
ServiceMetadataBehavior metadataBehavior;//定义服务行为变量,
metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
//获取宿主的行为列表
if (metadataBehavior == null)//如果没有服务原数据交换的行为,实例化添加服务原数据交换行为
{
metadataBehavior = new ServiceMetadataBehavior();
Uri httpAddress = new Uri("http://localhost:8001/");
metadataBehavior.HttpGetUrl =httpAddress;
metadataBehavior.HttpGetEnabled = true;//设置HTTP方式
host.Description.Behaviors.Add(metadataBehavior);
}
metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
//获取宿主的行为列表
if (metadataBehavior == null)//如果没有服务原数据交换的行为,实例化添加服务原数据交换行为
{
metadataBehavior = new ServiceMetadataBehavior();
Uri httpAddress = new Uri("http://localhost:8001/");
metadataBehavior.HttpGetUrl =httpAddress;
metadataBehavior.HttpGetEnabled = true;//设置HTTP方式
host.Description.Behaviors.Add(metadataBehavior);
}
首先是获得服务行为的列表信息,如果没有设置,我们就进行实例化服务原数据交换行为,并设置http方式可用。 host.Description.Behaviors.Add(metadataBehavior);添加宿主服务的行为。
【4.2】WCF服务元数据交换WS-*编程实现:
这里分别实现了HTTP、TCP、IPC三种方式的的元数据交换的代码。和http-get方式略有不同,我们需要实例化自己绑定元素和绑定,最后作为参数传递给host宿主实例。具体实现代码如下:
//2编程方式实现ws*原数据交换
//生命三个绑定节点类
BindingElement tcpBindingElement = new TcpTransportBindingElement();
BindingElement httpBindingElement = new HttpsTransportBindingElement();
BindingElement pipeBindingElement = new NamedPipeTransportBindingElement();
//实例化通用绑定类的实例
Binding tcpBinding = new CustomBinding(tcpBindingElement);
Binding httpBinding = new CustomBinding(httpBindingElement);
Binding pipeBinding = new CustomBinding(pipeBindingElement);
//
Uri tcpBaseAddress = new Uri("net.tcp://localhost:9001/");
Uri httpBaseAddress = new Uri("http://localhost:9002/");
Uri pipeBaseAddress = new Uri("net.pipe://localhost/");
host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetTcpBinding(), tcpBaseAddress);
host.AddServiceEndpoint(typeof(WCFService.IWCFService), new WSHttpBinding(), httpBaseAddress);
host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetNamedPipeBinding(), pipeBaseAddress);
//ServiceMetadataBehavior metadataBehavior;//定义服务行为变量,
metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
//获取宿主的行为列表
if (metadataBehavior == null)//如果没有服务原数据交换的行为,实例化添加服务原数据交换行为
{
metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
}
//如果没有可用的mex节点,可以使用一下代码判断,添加mex节点
host.AddServiceEndpoint(typeof(IMetadataExchange), tcpBinding, "mex");
host.AddServiceEndpoint(typeof(IMetadataExchange), httpBinding, "mex");
host.AddServiceEndpoint(typeof(IMetadataExchange), pipeBinding, "mex");
//生命三个绑定节点类
BindingElement tcpBindingElement = new TcpTransportBindingElement();
BindingElement httpBindingElement = new HttpsTransportBindingElement();
BindingElement pipeBindingElement = new NamedPipeTransportBindingElement();
//实例化通用绑定类的实例
Binding tcpBinding = new CustomBinding(tcpBindingElement);
Binding httpBinding = new CustomBinding(httpBindingElement);
Binding pipeBinding = new CustomBinding(pipeBindingElement);
//
Uri tcpBaseAddress = new Uri("net.tcp://localhost:9001/");
Uri httpBaseAddress = new Uri("http://localhost:9002/");
Uri pipeBaseAddress = new Uri("net.pipe://localhost/");
host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetTcpBinding(), tcpBaseAddress);
host.AddServiceEndpoint(typeof(WCFService.IWCFService), new WSHttpBinding(), httpBaseAddress);
host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetNamedPipeBinding(), pipeBaseAddress);
//ServiceMetadataBehavior metadataBehavior;//定义服务行为变量,
metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
//获取宿主的行为列表
if (metadataBehavior == null)//如果没有服务原数据交换的行为,实例化添加服务原数据交换行为
{
metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
}
//如果没有可用的mex节点,可以使用一下代码判断,添加mex节点
host.AddServiceEndpoint(typeof(IMetadataExchange), tcpBinding, "mex");
host.AddServiceEndpoint(typeof(IMetadataExchange), httpBinding, "mex");
host.AddServiceEndpoint(typeof(IMetadataExchange), pipeBinding, "mex");