您的位置:首页 > 其它

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐