WCF元数据交换

      服务有两种方案发布自己的元数据。一种是基于HTTP-GET协议提供元数据另一种是使用专门的元数据交换终结点的方式。WCF能够为服务自动提供基于HTTP-GET的元数据,但需要显式地添加服务行为(Behavior)以支持这一功能。

基于HTTP-GET协议提供元数据:

如下:所有引用了定制<behavior>配置节的托管服务都支持基于HTTP-GET协议实现元数据交换。

       <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

一旦启用了基于HTTP-GET的元数据交换,在浏览器中就可以通过HTTP基地址(如果存在)进行访问。

元数据交换终结点
元数据交换终结点是一种特殊的终结点,有时候又被称为MEX终结点。服务能够根据元数据交换终结点发布自己的元数据。

元数据交换终结点支持元数据交换的行业标准,在WCF中表现为IMetadataExchange接口:

[ServiceContract(...)]
public interface IMetadataExchange
{
[OperationContract(...)]
Message Get(Message request);
//更多成员
}

 

WCF自动地为服务宿主提供了IMetadataExchange接口的实现,公开元数据交换终结点。我们只需要指定使用的地址和绑定,以及添加服务元数据行为。

      <services>
           <service name="Host.Service1">
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

               <host>
                   <baseAddresses>
                        <add baseAddress="http://localhost:8732/Service1/" />
                    </baseAddresses>
                </host>
            </service>
        </services>

完整配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="Host.Service1">
                <endpoint address="" binding="wsHttpBinding" contract="Host.IService1">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8732/Service1/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

 

摘自:《WCF服务编程》 出版社:机械工业出版社华章公司  Juval Lowy著,张逸 译

posted @ 2011-05-26 12:17  Yao,Mane  阅读(529)  评论(0编辑  收藏  举报