哭佛林

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

1.使用EntLibConfig.exeApp.config中配置Enterprise Library中的各个配置文件:

配置完成后的EntLibConfig.exe视图如下:




 

其对应的App.config文件如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  
<configSections>
    
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  
</configSections>
  
<exceptionHandling>
    
<exceptionPolicies>
      
<add name="Event Policy">
        
<exceptionTypes>
          
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction
="NotifyRethrow" name="Exception">
            
<exceptionHandlers>
              
<add exceptionMessage="This is a test!" exceptionMessageResourceType=""
                replaceExceptionType
="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                type
="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                name
="Replace Handler" />
            
</exceptionHandlers>
          
</add>
        
</exceptionTypes>
      
</add>
    
</exceptionPolicies>
  
</exceptionHandling>
  
<dataConfiguration configProtectionProvider="RsaProtectedConfigurationProvider">
    
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
      xmlns
="http://www.w3.org/2001/04/xmlenc#">
      
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
      
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
          
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
          
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            
<KeyName>Rsa Key</KeyName>
          
</KeyInfo>
          
<CipherData>
            
<CipherValue>IMlnTKAicQZHsAgy0lUgsLMbnE/Ard5K7mdNrz0Kn64jZG5Rr6k5P5HdM3eldN6QWwcn0g2kd/D5F2IYdA6PQ34oe8RM5N+ir8m/pvr9zTlQcDypv0ccnWsOYy76jY/okypu91P23vmEXLm+U0xbm6+3Pi7EWPYS5PzdjrYQsAk=</CipherValue>
          
</CipherData>
        
</EncryptedKey>
      
</KeyInfo>
      
<CipherData>
        
<CipherValue>4bglJR2vkZwJJA9RMcB0Vlg71Zva35SsA9ocbXW2rdY1n3HOVQx5/i3WybgaFhQ+2Uqhfa7A6+hgP89SQCScdg==</CipherValue>
      
</CipherData>
    
</EncryptedData>
  
</dataConfiguration>
  
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
      xmlns
="http://www.w3.org/2001/04/xmlenc#">
      
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
      
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
          
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
          
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            
<KeyName>Rsa Key</KeyName>
          
</KeyInfo>
          
<CipherData>
            
<CipherValue>i2O7l8nO31YYjG91r0D7qJ9IL3UAJDxzOouxiv36otsIn1mjydaD8b3OvTa50H0Js1Y8SUoNPk5BNNsXJeohyoiGAYrA6kqMYGXDseacP7ognFAsrzibuPRTW5bMhwDzIrx/2OHYG9TKXtmolouWNJ9QD/QZv4mDUz70YghI9fM=</CipherValue>
          
</CipherData>
        
</EncryptedKey>
      
</KeyInfo>
      
<CipherData>
        
<CipherValue>1+pq+qtscsU16iC5MDJBnlkCoUu4ixEHlhoYbihz8DAfewwYj/LglBgGxT/oLLlObOgB07uLvZPrU3h70Ty+F+82FvTHqmd5VAf/VinW3KV57o53JwyDWWL6pKBhHvHTvJpqE8zcXAxmOzv8lOOMMMNoDcRtyiqeTheH1wIECPAF8CzO/C5NLVdQC5rOCFxsjP09sI8fue7VdEMo59Fhf5T6ZEQxI+CslODCPNcLl2MxGCFz082rCfOc+T7Rn4OAhuq2tZl3ktSIbimB6yylEEFEvGqFOMbmr0FdCAU8BZEzBdC8t5p4Vg==</CipherValue>
      
</CipherData>
    
</EncryptedData>
  
</connectionStrings>
</configuration>


 

2.建立Enterprise Library各自对应配置文件ConnectionStrings.configData.configException.config文件(文件夹Configuration),并将App.Config文件中对应的节点添加至这三个应用程序配置文件,结果如下:

ConnectionStrings.config

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
      xmlns
="http://www.w3.org/2001/04/xmlenc#">
        
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
        
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
                
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
                
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                    
<KeyName>Rsa Key</KeyName>
                
</KeyInfo>
                
<CipherData>
                    
<CipherValue>Rf3eDHU5C1zTM0mkLzd98JWs9SUP79vAU8g9GaejcyhZEX4+yrV335NQ6PQ9M9ygJdY9fhvImNfc6MY62SBegZU0fYXyK5T+NCD42a/QOyAJJnvoqstF20lZoq4uwBh/5tigArDqXM48Xd2Mhl6qJmAMRiSxA72fIEF/FdZZqus=</CipherValue>
                
</CipherData>
            
</EncryptedKey>
        
</KeyInfo>
        
<CipherData>
            
<CipherValue>9CCQ7qNaoDpZcbaauJcW/4IQhxzJXjq1rckYnqQ9xIevUh6zn18RSTcLzVyb61ztfVHKpkOB6fUIFNhI1LhmoBlf8u3NoA5oiSOoDpkgdN/kfSneZ3I68w/Lx8AkJeUkp/ByeORhLRjqDJ+p5jKCEdAYpNsxhXEu3cCS4LadSbTwN5h0BKQ6nGWAW2hQQV4Twhi+x3G57l6xeBtnfnmjCzUui/OxEdrZd/hzw0VPHRHRuLncUui3In8ZZ2FMPs4Gx73HkyCoFpwv/U47LDUduqhDQzlBPlG+b9texuRGd//kK2eGXx2ISg==</CipherValue>
        
</CipherData>
    
</EncryptedData>
</connectionStrings>


Data.config

<?xml version="1.0" encoding="utf-8" ?>
<dataConfiguration configProtectionProvider="RsaProtectedConfigurationProvider">
    
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
      xmlns
="http://www.w3.org/2001/04/xmlenc#">
        
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
        
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
                
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
                
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                    
<KeyName>Rsa Key</KeyName>
                
</KeyInfo>
                
<CipherData>
                    
<CipherValue>JsJqk/IX2CeuJSg+bGeT28pwqGcXfAGveQZqUnnLGZ6knG1WbyksyCd2DZwvh4GKHAcNjiDPiGDqQtj5R3g1TPRuBlscal8wULHZbAgah7c9KkvXsVcLnR4Z70k+7LA7pKuJJc/QsvR6EOALq3vg28j75CjPAoGxZ/CPaour8Gg=</CipherValue>
                
</CipherData>
            
</EncryptedKey>
        
</KeyInfo>
        
<CipherData>
            
<CipherValue>3EwB4sZkbx6t/0+rSPlYJxCJi0X2959/+jfM0P7lz8gXWjzZZ2WJEDldmLnGnpN4MdwIXcPv6p8fvoDxLIq+NQ==</CipherValue>
        
</CipherData>
    
</EncryptedData>
</dataConfiguration>


Exception.config

<?xml version="1.0" encoding="utf-8" ?>
<exceptionHandling>
    
<exceptionPolicies>
        
<add name="Event Policy">
            
<exceptionTypes>
                
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                  postHandlingAction
="NotifyRethrow" name="Exception">
                    
<exceptionHandlers>
                        
<add exceptionMessage="This is a test!" exceptionMessageResourceType="" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                          type
="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                          name
="Replace Handler" />
                    
</exceptionHandlers>
                
</add>
            
</exceptionTypes>
        
</add>
    
</exceptionPolicies>
</exceptionHandling>


 

3.修改App.Config文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  
<configSections>
    
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  
</configSections>

    
<connectionStrings configSource="Configuration\ConnectionStrings.config"/>
    
<exceptionHandling configSource="Configuration\Exceptions.config"/>
    
<dataConfiguration configSource="Configuration\Data.config"/>

</configuration>


 

4.客户程序:

 

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.Configuration;

namespace MutiConfigurationFile
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
try
            
{
                String a 
= ConfigurationManager.ConnectionStrings["EntLibInstance"].ConnectionString;
                Database db 
= DatabaseFactory.CreateDatabase("EntLibInstance");

                db.ExecuteNonQuery(
"ProcName");

                Console.ReadLine();
            }

            
catch (Exception ex)
            
{
                
if (ExceptionPolicy.HandleException(ex, "Event Policy"))

                    
throw;
            }


        }

    }

}



下载源码


posted on 2008-02-22 12:58  哭佛林<Kufolin>  阅读(612)  评论(0)    收藏  举报