zoukankan      html  css  js  c++  java
  • WCF入门教程(五)配置文件

    服务协定以及实现写好后,需要将相关服务公布出去,就需要HOST来承载,供客户端来调用。

    承载服务有两种方式,一种通过配置文件,一种通过代码进行配置。上一章已经介绍了代码方式来架设服务。

    当然配置文件的方式还是很灵活,应用广泛。

    通过此文章具体介绍WCF如果通过配置文件的方式进行配置。

    一、配置文件功能

    需要配置服务的EndPoint,设置相关绑定协议以及服务协定,并且还可以限定其具体的行为等。 

    二、配置文件结构

    主要配置内容包括service节点、binding和behavior节点,还有其他的一些节点这里暂时不展开讨论。

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
      <!-- 部署服务库项目时,必须将配置文件的内容添加到 
      主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
      <system.serviceModel>
        <!--配置服务节点Start-->
        <services>
          <!--配置某一服务,在这里可以指定服务名称-->
          <service name="SecondWCFSample.Service1">
          </service>
        </services>
        <!--配置服务节点End-->
    
        <!--配置绑定节点Start-->
        <bindings>
          <basicHttpBinding></basicHttpBinding>
          <mexHttpBinding></mexHttpBinding>
          <wsHttpBinding></wsHttpBinding>
        </bindings>
        <!--配置绑定节点End-->
    
    
        <!--配置行为节点Start-->
        <behaviors>
          <serviceBehaviors>
            <behavior>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <!--配置行为节点End-->
        
        <!--路由设置Start-->
        <routing>
          <filters>
            <filter name="filter1" filterType="Action"/>
          </filters>
        </routing>
        <!--路由设置End-->
        
        
      </system.serviceModel>
    
    </configuration>
    复制代码

     三、具体配置说明

    1、Service节点

    属性说明:

    name - 指定这个service配置是针对的那个服务,为一个实现了某些Contract的服务类的完全限定名(名称空间.类型名),ServiceHost载入 一个服务后,会到配置文件中的<services>下找有没有name属性跟服务匹配的<service>的配置
    behaviorConfiguration - 指定在<serviceBehaviors>下的一个<behavior>的name,这个特 定<behavior>给这个service制定了一些行为,比如服务是否允许身份模拟-->

    案例:

    <!--属性设置,name:服务的完全限定名,命名空间+实现类名;behaviorConfiguration行为配置,取设置的behavior的名称,具体可见behaviors节点-->
    <service name="SecondWCFSample.Service1" behaviorConfiguration="basicBehavior">
    </service>

    2、Endpoint节点

    定义endpoint,指定三个要素ABC,地址(Address),绑定(Binding)以及协定(Contract)。

    案例:

    复制代码
            <!--定义endpoint,指定地址(address),绑定(binding)以及协定(contract)-->
            <!-- 属性说明:
                 address - 服务端地址,相对于baseAddress的相对地址,如果为空则为baseAddress,也可以设定为绝对地址
                 binding - 绑定协议,系统的某一协议,basicHttpBinding,mexHttpBinding,wsHttpBinding等
                 contract - 协议完全限定名(名称空间.类型名)
                 name - 客户端代理类的构造方法中的endpointConfigurationName对应到这个name
                 bindingConfiguration - 指定客户端binding的具体设置,指向<bindings>元素下同类型binding的name -->
            <endpoint address ="" binding="wsHttpBinding" contract="SecondWCFSample.IService1">
              <!--服务标识。 部署时,应删除或替换下列标识元素,以反映用来运行所部署服务的标识。删除之后,WCF将自动推断相应标识。-->
              <identity>            
                <dns value="localhost"/>
              </identity>
            </endpoint>
    复制代码

     

    3、Binding

    绑定可指定在与终结点通话时所使用的通信机制,并指示如何连接到终结点。

    绑定包含以下元素:

      • 协议堆栈确定用于发送到终结点的消息的安全性、可靠性和上下文流设置。

      • 传输确定将消息发送到终结点时使用的基础传输协议,例如 TCP 或 HTTP。

      • 编码确定用于发送到终结点的消息的网络编码,例如,文本/XML、二进制或消息传输优化机制 (MTOM)。

    具体的binding,功能不同,设置的属性不同,具体参照以下

     

    绑定配置元素说明

    BasicHttpBinding

    <basicHttpBinding>

    一个绑定,适用于与符合 WS-Basic Profile 的 Web 服务(例如基于 ASP.NET Web 服务 (ASMX) 的服务)进行的通信。此绑定使用 HTTP 作为传输协议,并使用文本/XML 作为默认的消息编码。

    WSHttpBinding

    <wsHttpBinding>

    一个安全且可互操作的绑定,适合于非双工服务约定。

    WSDualHttpBinding

    <wsDualHttpBinding>

    一个安全且可互操作的绑定,适用于双工服务协定或通过 SOAP 媒介进行的通信。

    WSFederationHttpBinding

    <wsFederationHttpBinding>

    一个支持 WS-Federation 协议的安全的、可互操作的绑定,使联盟中的组织可以高效地对用户进行身份验证和授权。

    NetTcpBinding

    <netTcpBinding>

    一个安全且经过优化的绑定,适用于 WCF 应用程序之间跨计算机的通信。

    NetNamedPipeBinding

    <netNamedPipeBinding>

    一个安全、可靠且经过优化的绑定,适用于 WCF 应用程序之间计算机上的通信。

    NetMsmqBinding

    <netMsmqBinding>

    一个排队绑定,适用于 WCF 应用程序之间的跨计算机的通信。

    NetPeerTcpBinding

    <netPeerTcpBinding>

    一个支持多计算机安全通信的绑定。

    MsmqIntegrationBinding

    <msmqIntegrationBinding>

    一个适合于 WCF 应用程序和现有消息队列应用程序之间的跨计算机通信的绑定。

    BasicHttpContextBinding

    <basicHttpContextBinding>

    一个绑定,适用于与符合 WS-Basic Profile 且允许使用 HTTP Cookie 交换上下文的 Web 服务进行的通信。

    NetTcpContextBinding

    <netTcpContextBinding>

    一个安全且经过优化的绑定,适用于允许使用 SOAP 标头交换上下文的 WCF 应用程序之间跨计算机的通信。

    WebHttpBinding

    <webHttpBinding>

    一个绑定,可用于为通过 HTTP 请求(而不是 SOAP 消息)公开的 WCF Web 服务配置终结点。

    WSHttpContextBinding

    <wsHttpContextBinding>

    一个安全且可互操作的绑定,适用于允许使用 SOAP 标头交换上下文的非双工服务协定。


     案例:

    复制代码
        <!--配置绑定节点Start-->
        <bindings>
          <basicHttpBinding>
            <binding name="basic" transferMode="Streamed" messageEncoding="Text" allowCookies="false" textEncoding=""></binding>
          </basicHttpBinding>
          <netTcpBinding>        
          </netTcpBinding>
          <customBinding>       
          </customBinding>
        </bindings>
        <!--配置绑定节点End-->
    复制代码

    4、Behavior节点

    behavior设置服务的运行时行为,比如:是否允许客户端获得服务的元数据、路由修改、操作访问权限设置、服务的限制机制、服务请求的超时时间等。

    Behavior的具体含义可见:

    WCF入门教程(一)简介

    http://www.cnblogs.com/yank/p/3653160.html

    案例:

    复制代码
        <!--配置行为节点Start-->
        <behaviors>
          <serviceBehaviors>
            <behavior name="basicBehavior">
              <!-- 为避免泄漏元数据信息,
              请在部署前将以下值设置为 false 并删除上面的元数据终结点  -->
              <serviceMetadata httpGetEnabled="True"/>
              <!-- 要接收故障异常详细信息以进行调试,
              请将以下值设置为 true。在部署前设置为 false 
                以避免泄漏异常信息-->
              <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <!--配置行为节点End-->
    复制代码

    具体子节点功能介绍如下:

    元素说明

    <dataContractSerializer>

    包含 DataContractSerializer 的配置数据。

    <persistenceProvider>

    指定要使用的持久性提供程序实现的类型以及用于持久性操作的超时值。

    <serviceBehavior> 的 <routingExtension>

    提供对路由服务的运行时访问以允许对路由配置进行动态修改。

    <serviceAuthentication>

    提供一个工作流配置元素,该元素在服务级别建立传输、消息或发起方的有效性。

    <serviceAuthorization> 元素

    指定用于授予服务操作访问权限的设置。

    <serviceCredentials>

    指定要用于对服务进行身份验证的凭据以及与客户端凭据验证相关的设置。

    <serviceDebug>

    指定 Windows Communication Foundation (WCF) 服务的调试和帮助信息功能。

    <serviceDiscovery>

    指定服务终结点的可发现性。

    <serviceMetadata>

    指定服务元数据的发布和相关信息。

    <serviceSecurityAudit>

    指定用于在服务操作过程中启用安全事件审核的设置。

    <serviceThrottling>

    指定 WCF 服务的限制机制。

    <serviceTimeouts>

    指定服务的超时。

    <workflowRuntime>

    指定用于承载基于工作流的 WCF 服务的 WorkflowRuntime 实例的设置。

    <useRequestHeadersForMetadataAddress>

    允许从请求消息头中检索元数据地址信息。

    详细可见:

    http://msdn.microsoft.com/zh-cn/library/aa967282(v=vs.100).aspx

    四、完整实例

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
      <system.web>
        <compilation debug="true" />
      </system.web>
      <!-- 部署服务库项目时,必须将配置文件的内容添加到 
      主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
      <system.serviceModel>
        <!--配置服务节点Start-->
        <services>
          <!--配置某一服务,在这里可以指定服务名称-->
          <!--属性设置,name:服务的完全限定名,命名空间+实现类名;behaviorConfiguration行为配置,取设置的behavior的名称,具体可见behaviors节点-->
          <service name="SecondWCFSample.Service1" behaviorConfiguration="basicBehavior">
            <!--主机地址-->
            <host>
              <!--基址,访问某服务地址的基址-->
              <baseAddresses>
                <add baseAddress = "http://localhost:8732/Design_Time_Addresses/SecondWCFSample/Service1/" />           
              </baseAddresses>
              <!--访问超时时间-->
              <timeouts />
            </host>
            <!-- Service Endpoints -->
            <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
            <!--定义endpoint,指定地址(address),绑定(binding)以及协定(contract)-->
            <!-- 属性说明:
                 address - 服务端地址,相对于baseAddress的相对地址,如果为空则为baseAddress,也可以设定为绝对地址
                 binding - 绑定协议,系统的某一协议,basicHttpBinding,mexHttpBinding,wsHttpBinding等
                 contract - 协议完全限定名(名称空间.类型名)
                 name - 客户端代理类的构造方法中的endpointConfigurationName对应到这个name
                 bindingConfiguration - 指定客户端binding的具体设置,指向<bindings>元素下同类型binding的name -->
            <endpoint address ="" binding="wsHttpBinding" contract="SecondWCFSample.IService1">
              <!--服务标识。 部署时,应删除或替换下列标识元素,以反映用来运行所部署服务的标识。删除之后,WCF将自动推断相应标识。-->
              <identity>            
                <dns value="localhost"/>
              </identity>
            </endpoint>
            <!-- Metadata Endpoints -->
            <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 --> 
            <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <!--配置服务节点End-->
    
        <!--配置绑定节点Start-->
        <bindings>
          <basicHttpBinding>
            <binding name="basic" transferMode="Streamed" messageEncoding="Text" allowCookies="false"></binding>
          </basicHttpBinding>
          <netTcpBinding>        
          </netTcpBinding>
          <customBinding>       
          </customBinding>
        </bindings>
        <!--配置绑定节点End-->
    
        <!--配置行为节点Start-->
        <behaviors>
          <serviceBehaviors>
            <behavior name="basicBehavior">
              <!-- 为避免泄漏元数据信息,
              请在部署前将以下值设置为 false 并删除上面的元数据终结点  -->
              <serviceMetadata httpGetEnabled="True"/>
              <!-- 要接收故障异常详细信息以进行调试,
              请将以下值设置为 true。在部署前设置为 false 
                以避免泄漏异常信息-->
              <serviceDebug includeExceptionDetailInFaults="False" />
              <!---->
              <serviceSecurityAudit auditLogLocation="Security"/>
              <!--服务终结点的可发现性-->
              <serviceDiscovery>            
                <announcementEndpoints>
                </announcementEndpoints>
              </serviceDiscovery>
              <!--maxItemsInObjectGraph指定要序列化或反序列化的最大项数-->
              <dataContractSerializer maxItemsInObjectGraph="1048576"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <!--配置行为节点End-->
    
        <!--路由设置Start-->
        <routing>
          <filters>
            <filter name="filter1" filterType="Action"/>
          </filters>
        </routing>
        <!--路由设置End-->  
        
      </system.serviceModel>
    
    </configuration>
    复制代码

    五、其他配置

    关于其他配置项,研究比较深入的朋友可以参见MSDN详细文档

    http://msdn.microsoft.com/zh-cn/library/ms731354(v=vs.100).aspx

  • 相关阅读:
    SpringBoot系列: Pebble模板引擎语法介绍
    SpringBoot系列: Pebble模板引擎
    SpringBoot系列: CommandLineRunner接口的用处
    SpringBoot系列: 设计Restful风格的API
    SpringBoot系列: 所有配置属性和官方文档
    Spring Security 之API 项目安全验证(基于basic-authentication)
    Spring Security 之方法级的安全管控
    SpringBoot系列: 使用 Swagger 生成 API 文档
    SpringBoot系列: Web应用鉴权思路
    SpringBoot系列: RestTemplate 快速入门
  • 原文地址:https://www.cnblogs.com/feng-NET/p/5142071.html
Copyright © 2011-2022 走看看