元数据交换绑定的秘密
WCF提供了一種特殊的終結點——元數據交換終結點(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終結點的場景。
本文轉自wayfarer51CTO博客,原文鏈接:http://blog.51cto.com/wayfarer/280126,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的元数据交换绑定的秘密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity4.6新UI系统初探(uGUI
- 下一篇: MS CRM 2011 用Jscript