当前位置: 首页 > news >正文

学习 WCF (4)--学会使用配置文件

无论是Web应用程序还是Win应用程序,我们都会经常用到配置文件。WCF作为分布式开发的基础框架,在定义服务以及定义消费服务的客户端时,都使用了配置文件的方法。配置文件的重要性和实用性是大家所熟知的,它可以给我们WCF开发的灵活性上带来很大的提高。下面说说我学习使用配置文件的所得。

   WCF的配置使用.NET FrameworkSystem.Configuration配置系统。在Visual Studio中配置一个WCF服务时,如果是自托管宿主Windows Services宿主
,则配置文件为App.confing,如果是IIS宿主,则配置文件为Web.config

   先来看看简单的system.ServiceModel结构

 

 

Code
<system.ServiceModel>

   
<services>
      
<service>
         
<endpoint/>
      
</service>
   
</services>

   
<bindings>
   
<!—定义一个或多个系统提供的binding元素,例如<basicHttpBinding> --> 
   
<!—也可以是自定义的binding元素,如<customBinding>. -->
      
<binding>
      
<!—例如<BasicHttpBinding>元素. -->
      
</binding>
   
</bindings>

   
<behaviors>
   
<!—一个或多个系统提供的behavior元素. -->
      
<behavior>
      
<!—例如<throttling>元素. -->
      
</behavior>
   
</behaviors>

</system.ServiceModel>

 

(1)<services>

 

服务是在配置文件的 services 节中定义的。每个服务都有自己的 service 配置节。在Service中定义特定服务的address,binding,contract(也就是传说中重要的ABC)。

 

Code
        <services>
            
<service name="WCFStudent.WCFStudentText" behaviorConfiguration="ServiceBehavior">
                
<endpoint address="" binding="wsHttpBinding" contract="WCFStudent.IStuServiceContract"></endpoint>
                
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            
</service>
        
</services>

<!--一个Service中可以有多个endpoint,这样就可以同时定制多个服务。如果address值为空,那么endpoint的地址就是默认的基地址(Base Address)。那么则需要在host中声明Base Address-->

 

(2)<bindings>

 

 

 

此节包含标准绑定和自定义绑定的集合。每一项都是一个可由其唯一 name 进行标识的 binding 元素。服务通过用 name 与绑定进行链接来使用绑定。

 

个人感觉比较常用的是<basicHttpBinding>,<basicHttpBinding>等。由于属性比较多,在这里就不一一说明了。

 

Code
<bindings>
      
<basicHttpBinding>
        
<binding name="IStuServiceContract" closeTimeout="00:01:00"
                    openTimeout
="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal
="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize
="524288" maxReceivedMessageSize="65536"
                    messageEncoding
="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies
="false">
      
</basicHttpBinding>
    
</bindings>
<!--相应属性的设置大家可以参看MSDN.
http://msdn.microsoft.com/zh-cn/library/ms731361.aspx
-->

 

(3)<behaviors>

 

此元素定义名为 endpointBehaviorsserviceBehaviors 的两个子集合。每个集合分别定义终结点和服务所使用的行为元素。每个行为元素由其唯一的 name 属性标识。如果需要指定服务在执行方面的相关特性时,就必须定义服务的behavior。在WCF中,定义behavior就可以设置服务的运行时属性,甚至于通过自定义behavior插入一些自定义类型。

 

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

(4)<client>

 有的时候我们还需要定义<client>节点,主要定义客户端可以连接的终结点的列表。

Code
<client>
            
<endpoint address="http://localhost:39113/WCFServiceText/WCFStudentText.svc"
                binding
="wsHttpBinding" bindingConfiguration="WSHttpBinding_IStuServiceContract"
                contract
="ServiceReference1.IStuServiceContract" name="WSHttpBinding_IStuServiceContract">
                
<identity>
                    
<dns value="localhost" />
                
</identity>
            
</endpoint>
        
</client>

<!--如果宿主是IIS,WIN应用程序调用WCF时,会自动在App.config中生成<client>节的设置-->

三种宿主形式的配置文件的声明基本上一样。如果使用svcutil.exe生成客户程序,会在svcutil.exe的根目录中生成一个配置文件,值得大家一看:

Code
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    
<system.serviceModel>
        
<bindings>
            
<wsHttpBinding>
                
<binding name="WSHttpBinding_IStuServiceContract" closeTimeout="00:01:00"
                    openTimeout
="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal
="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize
="524288" maxReceivedMessageSize="65536"
                    messageEncoding
="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies
="false">
                    
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead
="4096" maxNameTableCharCount="16384" />
                    
<reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled
="false" />
                    
<security mode="Message">
                        
<transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm
="" />
                        
<message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite
="Default" establishSecurityContext="true" />
                    
</security>
                
</binding>
            
</wsHttpBinding>
        
</bindings>
        
<client>
            
<endpoint address="http://localhost:39113/WCFServiceText/WCFStudentText.svc"
                binding
="wsHttpBinding" bindingConfiguration="WSHttpBinding_IStuServiceContract"
                contract
="IStuServiceContract" name="WSHttpBinding_IStuServiceContract">
                
<identity>
                    
<dns value="localhost" />
                
</identity>
            
</endpoint>
        
</client>
    
</system.serviceModel>
</configuration>


    文章作者:高维鹏(Brian)
    文章出处:http://www.cnblogs.com/gaoweipeng
    欢迎转载,转载时请注明出处。谢谢合作。

相关文章:

  • 《3D Math Primer for Graphics and Game Development》读书笔记2
  • LAMP之php 与 mysql 配置(三)
  • 走进wordpress 详细说说template-loader.php
  • ubuntu的备份和还原
  • 【转】Android 带checkbox的listView 实现多选,全选,反选----解决checkbox错位问题
  • ACL访问控制列表及特殊权限
  • [裴礼文数学分析中的典型问题与方法习题参考解答]4.5.5
  • Ubuntu APT使用指南
  • 6个常见的 PHP 安全性攻击
  • new/delete malloc/free深入剖析
  • Linux下使用locale命令设置语言环境
  • EOS的综合除尘系统
  • 树状数组 POJ 2481 Cows
  • 匿名内部类
  • HDU1664 BFS + 数论 + 剪枝
  • 【译】JS基础算法脚本:字符串结尾
  • @angular/forms 源码解析之双向绑定
  • [NodeJS] 关于Buffer
  • 08.Android之View事件问题
  • Angular Elements 及其运作原理
  • angular组件开发
  • create-react-app做的留言板
  • HTTP 简介
  • java8-模拟hadoop
  • JavaScript的使用你知道几种?(上)
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • js学习笔记
  • js正则,这点儿就够用了
  • Linux Process Manage
  • MySQL几个简单SQL的优化
  • PaddlePaddle-GitHub的正确打开姿势
  • Python进阶细节
  • SpriteKit 技巧之添加背景图片
  • VUE es6技巧写法(持续更新中~~~)
  • 大主子表关联的性能优化方法
  • 翻译:Hystrix - How To Use
  • 复杂数据处理
  • 缓存与缓冲
  • 警报:线上事故之CountDownLatch的威力
  • 前端路由实现-history
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 原生js练习题---第五课
  • 终端用户监控:真实用户监控还是模拟监控?
  • 说说我为什么看好Spring Cloud Alibaba
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • #Spring-boot高级
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (Forward) Music Player: From UI Proposal to Code
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (三)终结任务
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境