How to get MetaData on client side in WCF?如何在客户端获取WCF service的元数据
2008-04-18 10:15
666 查看
1.Why do we need to get the metadata on client side?
Sometimes the client need to programmatically verify whether a particular endpoint (identified by its address) supports a particular contract/operations. for example, imagine an application where the end user specifies or configures the application during setup to consume and interact with a service. if the service does not support the required contracts, the application should alert the user that an invalid address was specified, and ask for an alternative or a corretct address.
in order to support such functinoality, the application needs to retrieve the metadata os the serve endpoints and see if at least one of the endpoints supports the requetsted contract.
As we know, when contracts of a serviced are exposed, we can consume the service through Endpoint. Actually, there are two kinds of Endpoint with the contracts exposed --Bisiness Endpoint and MEX Endpoint. and MEX Endpoint is the interface from which the client side can retrieve the metadata, such as what contracts are exposed.etc, just as the diagram shows.
有时,在客户端消费某个特定的service时候,客户端需要在程序中确认某个address指定的特定endpoint是否支持某个contract.比如,在某个应用程序中,终端用户必须与service进行交互设置应用程序。如果某个service不支持相应的contract,那么应该告诉用户,从而让用户另作选择或者选择正确的service address.
那么如何知道所发布的WCFservice支持那些contracts/operation呢?我们就需要在客户端获取service的元数据
我们知道,WCF service通过contract向外发布service, 然后我们通过endpoint来consume 服务。实际上,每当服务发布的时候,我们得到了2种类型的endpoint, 一种是业务端点(我们所说的endpoint一般都是指这种),一种是元数据端点。
//注释:所谓元数据,通俗讲就是描述数据的数据。该元数据描述了所发布service的endpoint,contract,operaion.等等。
2.How to retrieve MetaData?
There are several classes concerned with retrieving MetaData on client side. they are:
i) public enum MetaDataExchangeClientMode
{
MetadataExchange,
HttpGet
}
this enumeration specify the metadata exchange mode
这个枚举类型制定了元数据交换的模式
ii) class MetadataSet
this class is designed to store the metadata as XML format, and the instance of this class can be a contructor parametor for class WsdlImport
这个类用来将所获取的元数据用xml格式保存起来,这个类的对象可以做为WsdlImporter的构造函数的参数。
iii)class MetadataExchangeClient
this class is very critical, for retrieving metadata from the client side. and its command method is GetMetadata(), and its return value is a MetadataSet reference.
这个类是最关键的,用来在客户端获取metadata,常用的method就是GetMetadata(),其返回一个MetadataSet reference.
iv)public abstact class MetadataImporter
this is an abstract class, is the base class of WsdlImporter.
抽象类,作为WsdlImporter 的基类。
v) public class WsdlImport:MetadataImporter
this class is used to process the metadataset ,and generate respective collections, the comman methods is ImportAllEndpoints(),ImportAllContracts().etc.
该类用来对所获得的metadataset进行处理,获得相应的metadata的collection.常用方法包括ImportAllEndpoints,ImportAllContracts.
3 the Demo sample for retrieving metadata on the client side.
using System.ServiceModel;
using System.ServiceModel.Description;
string mxAddress=http://localhost:8081/myservice?wsdl;
//this is the service address exposed on the server side.
MetadataExchangeClient mxClient = new MetadataExchangeClient(new Uri(mxAddress), MetadataExchangeClientMode.HttpGet);
//instantiate an instance mxClient
MetadataSet metadta = mxClient.GetMetadata();
//retrieve the metadata, stored as metadataSet
MetadataImporter importer = new WsdlImporter(metadta);
ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
//get relavant collection from metadataSet.
foreach (ServiceEndpoint endpoint in endpoints)
Console.WriteLine("Address:{0},Contract Name:{1},Namespace:{2}",endpoint.Address.ToString(),endpoint.Contract.Name,endpoint.Contract.Namespace);
//show the results by the console.
Sometimes the client need to programmatically verify whether a particular endpoint (identified by its address) supports a particular contract/operations. for example, imagine an application where the end user specifies or configures the application during setup to consume and interact with a service. if the service does not support the required contracts, the application should alert the user that an invalid address was specified, and ask for an alternative or a corretct address.
in order to support such functinoality, the application needs to retrieve the metadata os the serve endpoints and see if at least one of the endpoints supports the requetsted contract.
As we know, when contracts of a serviced are exposed, we can consume the service through Endpoint. Actually, there are two kinds of Endpoint with the contracts exposed --Bisiness Endpoint and MEX Endpoint. and MEX Endpoint is the interface from which the client side can retrieve the metadata, such as what contracts are exposed.etc, just as the diagram shows.
有时,在客户端消费某个特定的service时候,客户端需要在程序中确认某个address指定的特定endpoint是否支持某个contract.比如,在某个应用程序中,终端用户必须与service进行交互设置应用程序。如果某个service不支持相应的contract,那么应该告诉用户,从而让用户另作选择或者选择正确的service address.
那么如何知道所发布的WCFservice支持那些contracts/operation呢?我们就需要在客户端获取service的元数据
我们知道,WCF service通过contract向外发布service, 然后我们通过endpoint来consume 服务。实际上,每当服务发布的时候,我们得到了2种类型的endpoint, 一种是业务端点(我们所说的endpoint一般都是指这种),一种是元数据端点。
//注释:所谓元数据,通俗讲就是描述数据的数据。该元数据描述了所发布service的endpoint,contract,operaion.等等。
2.How to retrieve MetaData?
There are several classes concerned with retrieving MetaData on client side. they are:
i) public enum MetaDataExchangeClientMode
{
MetadataExchange,
HttpGet
}
this enumeration specify the metadata exchange mode
这个枚举类型制定了元数据交换的模式
ii) class MetadataSet
this class is designed to store the metadata as XML format, and the instance of this class can be a contructor parametor for class WsdlImport
这个类用来将所获取的元数据用xml格式保存起来,这个类的对象可以做为WsdlImporter的构造函数的参数。
iii)class MetadataExchangeClient
this class is very critical, for retrieving metadata from the client side. and its command method is GetMetadata(), and its return value is a MetadataSet reference.
这个类是最关键的,用来在客户端获取metadata,常用的method就是GetMetadata(),其返回一个MetadataSet reference.
iv)public abstact class MetadataImporter
this is an abstract class, is the base class of WsdlImporter.
抽象类,作为WsdlImporter 的基类。
v) public class WsdlImport:MetadataImporter
this class is used to process the metadataset ,and generate respective collections, the comman methods is ImportAllEndpoints(),ImportAllContracts().etc.
该类用来对所获得的metadataset进行处理,获得相应的metadata的collection.常用方法包括ImportAllEndpoints,ImportAllContracts.
3 the Demo sample for retrieving metadata on the client side.
using System.ServiceModel;
using System.ServiceModel.Description;
string mxAddress=http://localhost:8081/myservice?wsdl;
//this is the service address exposed on the server side.
MetadataExchangeClient mxClient = new MetadataExchangeClient(new Uri(mxAddress), MetadataExchangeClientMode.HttpGet);
//instantiate an instance mxClient
MetadataSet metadta = mxClient.GetMetadata();
//retrieve the metadata, stored as metadataSet
MetadataImporter importer = new WsdlImporter(metadta);
ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
//get relavant collection from metadataSet.
foreach (ServiceEndpoint endpoint in endpoints)
Console.WriteLine("Address:{0},Contract Name:{1},Namespace:{2}",endpoint.Address.ToString(),endpoint.Contract.Name,endpoint.Contract.Namespace);
//show the results by the console.
相关文章推荐
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- How to get Hidden Column Value in GridView(ASP.net GridView如何获取隐藏列的值)
- How to get the size of file in C++ - 在C++中如何获取文件的大小
- How and Why to use the System.servicemodel.MessageParameterAttribute in WCF
- How to get random number within a range using C++ - 如何用C++获取一定范围内的随机数
- How to get the file's icon using C++ - 用C++如何获取文件的图标
- How to get random string using C++ - 用 C++ 如何获取随机字符串
- How to get file path in onActivityResult in Android 4.4
- <boost-03>如何使用boost filesystem获取给定子文件夹的目录?how to use boost filesystem to get subfolders?
- LINQ to SQL and WCF - Sharing types, subverting the DataContext on the client side(转)
- How to get and set the drawing order of layers in globe(获取并设置Globe图层的叠加次序:)
- How To Create A Client Server Application In Minutes[如何在几分钟的时间里编写一个客户机/服务器控制台程序]
- How to get Android Phone ServiceState - APK应用如何获得Android Phone的ServiceState,两种方法
- TORQUE引擎里如何生成一个在天空中移动的太阳(How to create a sun moving on the sky in the Torque)
- How to use toolbar control on dialog using CToolBar class in VC++ - VC++ 中用CToolBar 类如何使用ToolBar控件
- How to get data from Oracle DB in silverlight via WCF ?
- LINQ to SQL and WCF - Sharing types, subverting the DataContext on the client side
- How to make a simplest WCF service work on Win7 with VS2010
- 如何获取BootStrap Modal中按钮的值,How to catch value from buttons in BootStrap Modal
- How to access gridview cell values on client side