现在的位置: 首页 > 综合 > 正文

WCF热带鱼书学习手记 – metadata

2012年07月09日 ⁄ 综合 ⁄ 共 2611字 ⁄ 字号 评论关闭

WCF – MetaData

WCF的metadata描述了客户端如何同服务进行交互。通过metadata,我们可以利用一些工具生成proxy代码,比如SvcUtil.exe,客户端编程基本上是基于这些proxy的

服务有两种方案可以发布自己的元数据。一种是基于HTTP-GET协议提供元数据;另一种则为元数据交换方式,它往往使用一个专门的终结点,称之为元数据交换终结点。元数据交换终结点与其它终结点相似,仍然包含了地址、绑定与契约,但是使用的服务契约为WCF提供的接口IMetadataExchange。

实际上,这两种发布元数据的方式代表了它使用了两种不同的标准协议,前者为HTTP/GET请求,后者为WS-MetadataExchange(MEX)。在WCF,以MetadataExchangeClientMode枚举类型表示这两种元数据交换模式:

 

public enum MetadataExchangeClientMode
{
    MetadataExchange,
    HttpGet
} 

 

1. HTTP-GET

起用了这种方式的metadata发布,客户端可以通过浏览器察看并确认元数据。同前面几篇文章介绍的一样,启用方法也是2种:编成和配置。

配置元数据发布是通过添加ServiceBehavior来实现的,例子如下:

<system.serviceModel>
    <services>
      <service name="MyNamespace.MyService" behaviroConfiguration="MEXGET">
        <endpoint contract = "MyNamespace.IMyService"
                  binding  = "wsHttpBinding"
                  address  = "http://localhost:8000/MyService"
        />
      </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MEXGET">
                <serviceMetadata httpGetEnabled="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

 

编程方式:

 

ServiceHost host = new ServiceHost(typeof(MyService));
ServiceMetadataBehavior metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if(metadataBehavior == null)
{
    metadataBehavior = new ServiceMetadataBehavior();
    metadataBehavior.HttpGetEnabled = true;
    host.Description.Behaviors.Add(metadataBehavior);
}
Binding wsHttpBinding = new WSHttpBinding();
host.AddServiceEndpoint(typeof(IMyService),
                        wsHttpBinding,
                        new Uri("http://localhost:8086/MyService/"));
host.Open();
...            
host.Close();

 

2. Metadata-Exchange

这种方式是通过定义特定的endpoint的方式来发布元数据,这种endpoint被称作元数据终结点或者MEX终结点。WCF为MEX终结点的ABC分别给出了各自的定义。

  • A - 类似于普通的endpoint地址。
  • B - 基于http, https, tcp和icp协议的binding。例如,mexTcpBinding, mexNamePipeBinding, mexHttpBinding….
  • C - IMetadataExchange接口。其实现由WCF自动提供。

以下是一个配置Mex endpoint的例子:

 

<system.serviceModel>
    <services>
      <service name="MyNamespace.MyService" behaviroConfiguration="MEX">
        <endpoint contract = "MyNamespace.IMyService"
                  binding  = "wsHttpBinding"
                  address  = "http://localhost:8000/MyService"
        />
        <endpoint contract = "IMetadataExchange"
                          binding  = "mexHttpBinding"
                          address  = "http://localhost:8000/MEX"
        />
      </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MEX">
                <serviceMetadata/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

 

同样可以通过编程的方式来启动mex endpoint:

 

BindingElement bindingElement = new TcpTransportBindingElement();
CustomBinding binding = new CustomBinding(bindingElement);
Uri tcpBaseAddress = new Uri("net.tcp://localhost:9000");
ServiceHost host = new ServiceHost(typeof(MyService), tcpBaseAddress);
if(metadataBehavior == null)
{
    metadataBehavior = new ServiceMetadataBehavior();
    host.Description.Behaviors.Add(metadataBehavior);
}
host.AddServiceEndpoint(typeof(IMetadataExchange), binding, "MEX");
host.Open();
...            
host.Close();

 

抱歉!评论已关闭.