zoukankan      html  css  js  c++  java
  • 四、WCF的配置文件

    注:本文为学习摘抄,原文地址:http://www.cnblogs.com/iamlilinfeng/archive/2012/10/02/2710224.html

    一、概述

      配置也是WCF编程中的主要组成部分。在以往的.net应用程序中,我们会把DBConn和一些动态加载类及变量写在配置文件里。但WCF有所不同。他指定向客户端公开的服务,包括服务的地址、服务用于发送和接收消息的传输和消息编码,以及服务需要的安全类型等。使用配置文件后,我们无需编译即可修改WCF的变化的信息,提高了程序的灵活性。

      如果在代码里写了配置,那么配置文件将不起作用。

      Web程序在Web.config中配置,应用程序中在App.config中配置。

    二、服务配置的主要部分

      在Config中配置服务的结点为:<system.serviceModel></system.serviceModel>,在这个节点中主要有三个平级的部分。如下代码所示:

    复制代码
     1 <?xml version="1.0" encoding="utf-8"?>
     2  <configuration>
     3    <system.serviceModel>
     4  
     5      <!--配置服务和终结点开始-->
     6      <services>
     7        <service>
     8          <endpoint></endpoint>
     9        </service>
    10      </services>
    11      <!--配置服务和终结点结束-->
    12  
    13      <!--配置绑定开始-->
    14      <bindings>
    15        <netTcpBinding>
    16          <binding>
    17          </binding>
    18        </netTcpBinding>
    19      </bindings>
    20      <!--配置绑定结束-->
    21  
    22      <!--配置行为开始-->
    23      <behaviors>
    24        <serviceBehaviors>
    25          <behavior>
    26          </behavior>
    27        </serviceBehaviors>
    28      </behaviors>
    29      <!--配置行为结束-->
    30  
    31    </system.serviceModel>
    32  </configuration>
    复制代码

      Service配置节[必须有]:配置服务、接口和终结点。每个Service都会有以下两个属性。name:名称空间.类名[服务的具体实现类]。behaviorConfiguration:一个在behaviors节点中找到的名称。

      Binding配置节[可有可无]:配置绑定,如http,tcp等

      Behavior配置节[可有可无]:配置行为,如认证等。

    三、实例

    复制代码
     1 <?xml version="1.0"?>
     2 <configuration>
     3   <system.serviceModel>
     4 
     5     
     6     <!--服务-->
     7     <services>
     8       <!--name:名称空间.类型名-->
     9       <!--behaviorConfiguration:behavior的名称,请看behavior配置节的名称-->
    10       <service name="WCFLibrary.User" behaviorConfiguration="MyBehavior">
    11         <host>
    12           <baseAddresses>
    13             <!-- 每种传输协议的baseAddress,用于跟使用同样传输协议Endpoint定义的相对地址组成完整的地址,
    14                  每种传输协议只能定义一个baseAddress。HTTP的baseAddress同时是service对外发布服务说明页面的URL -->
    15             <add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFLibrary/Service/"/>
    16           </baseAddresses>
    17         </host>
    18         <!-- 除非完全限定,否则地址将与上面提供的基址相关,每个服务可以有多个Endpoint -->
    19         <!-- Address:指定这个Endpoint对外的URI,这个URI可以是个绝对地址,也可以是个相对于baseAddress的
    20                       相对地址。如果此属性为空,则这个Endpoint的地址就是baseAddress-->
    21         <!--bindingConfiguration:binding的名称,请看binding配置节的名称-->
    22         <endpoint address="" binding="wsHttpBinding" contract="WCFLibrary.IUser" bindingConfiguration="myHttpBinding">
    23           <identity>
    24             <dns value="localhost"/>
    25           </identity>
    26         </endpoint>
    27         <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
    28         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    29       </service>
    30     </services>
    31 
    32     
    33     <!--绑定-->
    34     <bindings>
    35       <wsHttpBinding>
    36         <binding name="myHttpBinding">
    37           <security mode="None">
    38             <message clientCredentialType="Windows" />
    39           </security>
    40         </binding>
    41       </wsHttpBinding>
    42     </bindings>
    43 
    44     
    45     <!--行为-->
    46     <behaviors>
    47       <serviceBehaviors>
    48         <behavior name="MyBehavior">
    49           <!-- httpGetEnabled - bool类型的值,表示是否允许通过HTTP的get方法获取sevice的WSDL元数据 -->
    50           <serviceMetadata httpGetEnabled="True"/>
    51         </behavior>
    52       </serviceBehaviors>
    53     </behaviors>
    54     
    55   </system.serviceModel>
    56 </configuration>
    复制代码
  • 相关阅读:
    苹果一体机发射Wi-Fi
    iphone 屏蔽系统自动更新,消除设置上的小红点
    data parameter is nil 异常处理
    copy与mutableCopy的区别总结
    java axis2 webservice
    mysql 远程 ip访问
    mysql 存储过程小问题
    mysql游标错误
    is not writable or has an invalid setter method错误的解决
    Struts2中关于"There is no Action mapped for namespace / and action name"的总结
  • 原文地址:https://www.cnblogs.com/Herzog3/p/5191666.html
Copyright © 2011-2022 走看看