WCF-基本知识

WCF-基本知识编程

(1)WCF服务元数据交换方式介绍:浏览器

WCF服务有两种方案能够发布本身的元数据。一种是基于HTTP-GET协议提供元数据;另外一种则为MEX终结点元数据交换方式,和WCF服务同样使用一个专门
的终结点,称为MEX元数据交换终结点。
System.ServiceModel.Description命名空间里MetadataExchangeClientMode枚举类型里进行了定义。代码以下:网络

using System;
namespace System.ServiceModel.Description
{
// Summary:
// Specifies the exchange mode used to obtain metadata.
public enum MetadataExchangeClientMode
{
// Summary:
// A WS-Transfer Get request is used.
MetadataExchange = 0,
//
// Summary:
// An HTTP GET request is used.
HttpGet = 1,
}
}

       元数据交换终结点与其它终结点类似,包含本身的地址(Address)、绑定(通讯协议Binding)、契约(服务、操做、数据Contract),可是使用的服务契约为
WCF提供的接口IMetadataExchange。两种发布元数据的方式使用了两种不一样的标准网络传输协议,前者为HTTP/GET请求,后者为WS-MetadataExchange(MEX:
WCF支持的基本绑定HTTP、HTTPS、TCP、IPC等绑定协议)。
      启用元数据交换服务后,必须显式配置元数据交换行为。下面咱们来分别详细介绍WCF服务元数据交换配置和编程两种方式的实现过程。tcp

(2)WCF服务元数据交换配置实现过程详解:网络传输协议

 

【2.1】配置HTTP-GET元数据交换方式:
须要配置服务的行为和基地址,客户端能够根据基地址查看服务的元数据。代码以下:spa

<service name="WcfServiceApp.WCFService" behaviorConfiguration="WcfServiceApp.WCFServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceApp.WCFServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadat
a endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Se
t to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>

3 WCF服务元数据交换编程实现过程详解:debug

【3.1】WCF服务元数据交换HTTP-GET编程实现:
必须添加对命名空间的引用, using System.ServiceModel.Description;咱们对服务元数据操做的类和接口信息定义在此命名空间里,具体的实现HTTP-GET的
代码以下:code

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

首先是得到服务行为的列表信息,若是没有设置,咱们就进行实例化服务原数据交换行为,并设置http方式可
用。host.Description.Behaviors.Add(metadataBehavior);添加宿主服务的行为。orm

在浏览器中查看以下图:后缀为wsdlblog

 

【3.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(), pipeBaseAdd
ress);
//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");

在浏览器中查看以下图:后缀变为mex.

相关文章
相关标签/搜索