元数据交换绑定的秘密

new_wincf-logo.jpgWCF提供了一种特殊的终结点——元数据交换终结点(MEX终结点),通过它,服务就能够发布元数据。此外,它专门提供了一个元数据交换的服务契约接口IMetadataExchange:
[ServiceContract]
public interface IMetadataExchange
{
    [OperationContract]
    Message Get(Message request);
}

既然要配置终结点,就必须有对应的绑定来支持。MEX终结点可以支持多种不同的传输协议,包括HTTP(S),TCP和Named Pipe,支持MEX传输的绑定的名称分别为mexHttpBinding、mexHttpsBinding、mexTcpBinding、mexNamedPipeBinding。例如,这样的配置文件:
<endpoint
    address = "MEX"
    binding = "mexHttpBinding"
    contract = "IMetadataExchange"
/>
<endpoint
    address = "MEX"
    binding = "mexTcpBinding"
    contract = "IMetadataExchange"
/>

那么,是否需要为MEX终结点配置一个相应的绑定呢?不过,如果我们试着去查找System.ServiceModel等与WCF有关的程序集,是找不到这样的绑定类的。Aaron Skonnard在其博客中揭开了一个秘密,他认为,在这里,微软的开发人员玩了一个花招,那些绑定元素并没有分别映射到一个专门的类中,而是统一放到了一个单独的类MetadataExchangeBindings,(The trick is these binding element names don't map to individual classes but rather to a single class named MetadataExchangeBindings.)它提供了CreateMexHttpBinding, CreateMexHttpsBinding, CreateMexNamedPipeBinding和CreateMexTcpBinding四个方法,如下所示:
public static class MetadataExchangeBindings
{
    // Methods
    public static Binding CreateMexHttpBinding();
    public static Binding CreateMexHttpsBinding();
    public static Binding CreateMexNamedPipeBinding();
    public static Binding CreateMexTcpBinding();
    ...
}

查看这些方法的实现,可以发现它们都创建了WCF的内建绑定,然后根据情况调整了某些默认值,并重写了绑定的名称和命名空间(If you inspect the implementation of any of these methods, you'll notice they just create one of the built-in bindings, adjusting some of the defaults, and then they override the binding name and namespace.),例如:
public static Binding CreateMexHttpBinding()
{
    return CreateHttpBinding();
}

private static WSHttpBinding CreateHttpBinding()
{
    WSHttpBinding binding = new WSHttpBinding(SecurityMode.None, false);
    binding.Name = "MetadataExchangeHttpBinding";
    binding.Namespace = "http://schemas.microsoft.com/ws/2005/02/mex/bindings";
    return binding;
}

这样做的目的是由于不同传输协议的MEX终结点在配置文件中的配置只是稍有不同,为了元数据交换而专门定义一个新的绑定,是没有意义的。(they probably figured it wouldn't make sense to define completely new binding classes just for the MEX scenarios...they're really just slightly different configurations.

从这一实现可以看出,当我们在配置MEX终结点时,使用WCF提供的MEX绑定并非必须的,我们也可以为其指定内建绑定,只要该绑定符合MEX终结点的场景。

posted on 2008-03-11 11:54 张逸 阅读(1992) 评论(6)  编辑 收藏 所属分类: WCF Tips

评论

#1楼  2008-03-11 13:35 阿不      

老大改成真名啦。   回复  引用  查看    

#2楼  2008-03-12 14:38 G yc {Son of VB}      

学习到了~~   回复  引用  查看    

#3楼  2008-03-13 01:12 lexus      

请问一下,发布元数据的作用是什么,发布的元数据包含的哪些元素,正在看大哥翻译的wcf服务编程,不知道是不是老外的思路和国人不太一样,我擅长看的是来一个真实实际的例子的介绍,而不是一块内容、一块讲的非常仔细,这样只能当字典来翻,而我喜欢速成,大哥可否提供几本符合我口味的wcf相关的书籍?   回复  引用  查看    

#4楼  2008-03-13 01:14 lexus      

大哥,是不是能操刀举个实际利用WCF的例子,你的那本软件设计精要与模式上举的例子都非常的好,给人很大的启发   回复  引用  查看    

#5楼 [楼主] 2008-03-13 11:17 张逸      

@lexus
其实《Programming WCF Services》一书自身提供了非常多的实例,内容简单易懂,又对你学习WCF很有帮助。下载地址是在http://www.idesign.net。   回复  引用  查看    

导航

公告

logo.gif
我的著作与译作

《软件设计精要与模式》

《WCF服务编程》

MVP_Horizontal_BlueOnly.png

From 03-03-2006
Counter: site stats

与我联系

常用链接

我参加的小组

我参与的团队

随笔分类(244)

随笔档案(235)

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜