元數(shù)據(jù)的配置、交換
WCF服務(wù)元數(shù)據(jù)是WCF服務(wù)的核心部分服務(wù)地址(Address)、綁定(通信協(xié)議Binding)、契約(服務(wù)、操作、數(shù)據(jù)Contract)的原始描述信息。服務(wù)所公開的元數(shù)據(jù)包括 XSD(文檔中出現(xiàn)的元素、文檔中出現(xiàn)的屬性、子元素、子元素的數(shù)量、子元素的順序、元素是否為空、元素和屬性的數(shù)據(jù)類型、元素或?qū)傩缘哪J(rèn)和固定值)和 WSDL 文檔(用于描述服務(wù)的方法、參數(shù)、參數(shù)個數(shù)、順序、返回值、返回值的類型等方法的相關(guān)信息)。.Disco文檔(描述服務(wù)的協(xié)議、地址、命名空間等信息)。
元數(shù)據(jù)的信息除了包括服務(wù)、數(shù)據(jù)、操作等契約的相關(guān)的信息外,還有其他的如事物、可靠性、錯誤處理等相關(guān)的信息。
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceApp.WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8001/" />
</behavior>
</serviceBehaviors>
</behaviors>
在設(shè)置httpGetEnabled="true"以后便會在我們的服務(wù)幫助頁面( 幫助頁面可以設(shè)置行為的<serviceDebug httpHelpPageEnabled="false"/>關(guān)閉)的SvcUtil.exe指令之后帶一個?WSDL后綴的活動鏈接,點擊該鏈接,便會進(jìn)入該服務(wù)的WSDL文檔中。
httpGetUrl 屬性, 如果httpGetEnabled為true,這個屬性指示使用哪個URL地址發(fā)布服務(wù)的WSDL。
要發(fā)布Metadata,需要對外提供一個Http的地址,由HttpGetUrl 屬性指定。
如果HttpGetUrl指定了絕對地址,那么對外發(fā)布Metadata的地址就為:HttpGetUrl 后加?wsdl。
如果HttpGetUrl指定了相對地址,那么對外發(fā)布Metadata的地址就為: ServiceHost的baseAddress + HttpGetUrl 后加?wsdl。
如果沒有設(shè)置HttpGetUrl,那么Metadata的地址就是ServiceHost的baseAddress后加?wsdl
<service behaviorConfiguration="NewBehavior" name="Messaging.Host.Service">
<endpoint address="" binding="basicHttpBinding"
contract="Messaging.Host.IService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/WCFService/Service" />
</baseAddresses>
</host>
</service>
那么,他的WSDL就是http://localhost:8080/WCFService/Service?WSDL
ServiceMetadataBehavior metadataBehavior;//定義服務(wù)行為變量,
metadataBehavior
= host.Description.Behaviors.Find<ServiceMetadataBehavior>();
//獲取宿主的行為列表
if (metadataBehavior == null)//如果沒有服務(wù)原數(shù)據(jù)交換的行為,實例化添加服務(wù)原數(shù)據(jù)交換行為
{
metadataBehavior = new ServiceMetadataBehavior();
Uri httpAddress = new Uri("http://localhost:8001/");
metadataBehavior.HttpGetUrl =httpAddress;
metadataBehavior.HttpGetEnabled = true;//設(shè)置HTTP方式
host.Description.Behaviors.Add(metadataBehavior);
}
首先是獲得服務(wù)行為的列表信息,如果沒有設(shè)置,我們就進(jìn)行實例化服務(wù)原數(shù)據(jù)交換行為,并設(shè)置http方式可用。 host.Description.Behaviors.Add(metadataBehavior);添加宿主服務(wù)的行為。
2.WS-MEX
配置方式:
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
<add baseAddress="net.tcp://localhost:8002/"/>
<add baseAddress="net.pipe://localhost/"/>
</baseAddresses>
</host>
元數(shù)據(jù)的地址就是對應(yīng)的baseAddress/mes。比如http://localhost:8001/mex
//2編程方式實現(xiàn)ws*原數(shù)據(jù)交,聲明三個綁定節(jié)點類
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(), pipeBaseAddress);
//ServiceMetadataBehavior metadataBehavior;//定義服務(wù)行為變量,
metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
//獲取宿主的行為列表
if (metadataBehavior == null)//如果沒有服務(wù)原數(shù)據(jù)交換的行為,實例化添加服務(wù)原數(shù)據(jù)交換行為
{
metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
}
//如果沒有可用的mex節(jié)點,可以使用一下代碼判斷,添加mex節(jié)點
host.AddServiceEndpoint(typeof(IMetadataExchange), tcpBinding, "mex");
host.AddServiceEndpoint(typeof(IMetadataExchange), httpBinding, "mex");
host.AddServiceEndpoint(typeof(IMetadataExchange), pipeBinding, "mex");
參考:
http://www.cnblogs.com/frank_xl/archive/2009/03/30/1421862.html
http://www.cnblogs.com/chnking/archive/2008/01/21/1047754.html
http://www.cnblogs.com/chnking/archive/2008/01/22/1049345.html
http://www.cnblogs.com/jillzhang/archive/2008/01/30/1059169.html