元数据交换绑定的秘密
WCF提供了一種特殊的終結(jié)點——元數(shù)據(jù)交換終結(jié)點(MEX終結(jié)點),通過它,服務(wù)就能夠發(fā)布元數(shù)據(jù)。此外,它專門提供了一個元數(shù)據(jù)交換的服務(wù)契約接口IMetadataExchange:
[ServiceContract]public?interface?IMetadataExchange
{
??? [OperationContract]
??? Message Get(Message request);
}
既然要配置終結(jié)點,就必須有對應(yīng)的綁定來支持。MEX終結(jié)點可以支持多種不同的傳輸協(xié)議,包括HTTP(S),TCP和Named Pipe,支持MEX傳輸?shù)慕壎ǖ拿Q分別為mexHttpBinding、mexHttpsBinding、mexTcpBinding、 mexNamedPipeBinding。例如,這樣的配置文件:
<endpoint??? address =?"MEX"
??? binding =?"mexHttpBinding"
??? contract =?"IMetadataExchange"
/>
<endpoint
??? address =?"MEX"
??? binding =?"mexTcpBinding"
??? contract =?"IMetadataExchange"
/>
那么,是否需要為MEX終結(jié)點配置一個相應(yīng)的綁定呢?不過,如果我們試著去查找System.ServiceModel等與WCF有關(guān)的程序集,是找不到這樣的綁定類的。Aaron Skonnard在其博客中揭開了一個秘密,他認(rèn)為,在這里,微軟的開發(fā)人員玩了一個花招,那些綁定元素并沒有分別映射到一個專門的類中,而是統(tǒng)一放到了一個單獨的類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();
????//...
}
查看這些方法的實現(xiàn),可以發(fā)現(xiàn)它們都創(chuàng)建了WCF的內(nèi)建綁定,然后根據(jù)情況調(diào)整了某些默認(rèn)值,并重寫了綁定的名稱和命名空間(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;
}
這樣做的目的是由于不同傳輸協(xié)議的MEX終結(jié)點在配置文件中的配置只是稍有不同,為了元數(shù)據(jù)交換而專門定義一個新的綁定,是沒有意義的。(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.)
從這一實現(xiàn)可以看出,當(dāng)我們在配置MEX終結(jié)點時,使用WCF提供的MEX綁定并非必須的,我們也可以為其指定內(nèi)建綁定,只要該綁定符合MEX終結(jié)點的場景。
本文轉(zhuǎn)自wayfarer51CTO博客,原文鏈接:http://blog.51cto.com/wayfarer/280126,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的元数据交换绑定的秘密的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity4.6新UI系统初探(uGUI
- 下一篇: MS CRM 2011 用Jscript