zoukankan      html  css  js  c++  java
  • 学习 WCF (4)学会使用配置文件

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

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

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


    <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)。


            <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>等。由于属性比较多,在这里就不一一说明了。


    <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插入一些自定义类型。


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

    (4)<client>

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


    <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的根目录中生成一个配置文件,值得大家一看:


    <?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
        欢迎转载,转载时请注明出处。谢谢合作。

  • 相关阅读:
    删除 Visual studio 生成后的临时文件
    C# 中的委托和事件(转)
    C#复制DataRow出现“该行已经属于此表”错误的解决办法(转)
    ini配置文件读取类
    c# wpf窗体前端显示问题
    注册系统热键类(原创)
    C# 窗体最小化的托盘/系统通知区域(转)
    php explode()返回值
    $_SERVER['SCRIPT_NAME']
    svn合并初次使用心得
  • 原文地址:https://www.cnblogs.com/soundcode/p/2131052.html
Copyright © 2011-2022 走看看