• 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著,张逸 译

  • 相关阅读:
    Java中断机制
    RPC原理
    synchronized和ReentrantLock的区别
    dubbo入门
    Zookeeper入门
    分布式事务
    Mysql索引会失效的几种情况
    java代码执行过慢的问题定位
    持续集成
    Mycat 数据库分库分表中间件
  • 原文地址:https://www.cnblogs.com/mane/p/2058063.html
Copyright © 2020-2023  润新知