Enterprise Library 2.0 技巧(1):如何使用外部配置文件
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
namespace EntLibConfig
{
class Program
{
static void Main(string[] args)
{
try
{
Database db = DatabaseFactory.CreateDatabase("EntLibInstance");
db.ExecuteNonQuery("ProcName");
}
catch (Exception ex)
{
if (ExceptionPolicy.HandleException(ex, "Event Policy"))
throw;
}
}
}
}
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Event Policy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="ThrowNewException" name="Exception">
<exceptionHandlers>
<add exceptionMessage="This is a test!" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Replace Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
<dataConfiguration defaultDatabase="EntLibInstance" />
<connectionStrings>
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
并新建一个File Configuration Source,指定文件的路径和文件名
在Configuration Source节点设置SelectSource为File Configuration Source
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
<sources>
<add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
filePath="D:\Visual Studio2005 Project\EntLibConfig\EntLibConfig\external.config" />
<add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</sources>
</enterpriseLibrary.ConfigurationSource>
</configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Event Policy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="ThrowNewException" name="Exception">
<exceptionHandlers>
<add exceptionMessage="This is a test!" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Replace Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
<dataConfiguration defaultDatabase="EntLibInstance" />
<connectionStrings>
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
{
try
{
FileConfigurationSource dataSource = new FileConfigurationSource("data-filesource.config");
DatabaseProviderFactory dbFactory = new DatabaseProviderFactory(dataSource);
Database db = dbFactory.Create("EntLibInstance");
db.ExecuteNonQuery("ProcName");
}
catch (Exception ex)
{
FileConfigurationSource exceptionsSource = new FileConfigurationSource("exceptions-filesource.config");
ExceptionPolicyFactory exceptionFactory = new ExceptionPolicyFactory(exceptionsSource);
ExceptionPolicyImpl exceptionPolicy = exceptionFactory.Create("Event Policy");
if (exceptionPolicy.HandleException(ex))
throw;
}
}
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<dataConfiguration defaultDatabase="EntLibInstance" />
<connectionStrings>
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Event Policy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="ThrowNewException" name="Exception">
<exceptionHandlers>
<add exceptionMessage="This is a test!" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Replace Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<exceptionHandling configSource="exceptions.config" />
<connectionStrings configSource="data.config" />
</configuration>
<dataConfiguration defaultDatabase="EntLibInstance" />
<connectionStrings>
<add name="EntLibInstance" connectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Database=Northwind;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<exceptionHandling>
<exceptionPolicies>
<add name="Event Policy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="ThrowNewException" name="Exception">
<exceptionHandlers>
<add exceptionMessage="This is a test!" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Replace Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
以上几种方案仅仅是给你一个参考,你可以在开发中根据实际情况选用其中的一种或者使用默认的方式。
Enterprise Library 2.0 技巧(1):如何使用外部配置文件相关推荐
- Enterprise Library 2.0 技巧(3):记录ASP.NET站点中未处理的异常
这篇文章不能算是Enterprise Library 2.0的一个技巧,只是Logging Application Block的一个简单应用而已,在这里我们使用Logging Application ...
- Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
在本系列的技巧(1)和技巧(2)中分别介绍了使用外部配置文件,使用数据库记录配置信息两种方法,不知道大家有没有想过不使用任何配置文件,也不使用数据库而直接用编程的方法来实现呢?本文将会展示如何使用编程 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...
- Microsoft Enterprise Library 4.0 for Visual Studio 2008
Microsoft Enterprise Library 4.0 for Visual Studio 2008已经发布,可以下载看看 转载于:https://www.cnblogs.com/rover ...
- Enterprise Library 3.0 体验(3):使用配置文件的Validation Application Block
摘要:Enterprise Library 3.0 January 2007 CTP版发布了,这次发布的版对于Validation Application Block有了很大的改进,包括对配置工具的支 ...
- Enterprise Library 3.0 安装过程
微软刚刚发布Enterprise Library 3.0,其更新程度非常大,功能非常强大. 3.0的安装过程却非常复杂,下面是安装过程记录,希望对应用Entlib3.0的人有所借鉴. 相关的介绍资源 ...
- Enterprise Library 5.0发布
Microsoft Enterprise Library 5.0是一套可重用的应用程序块,帮助开发人员进行企业应用开发.包括:Caching Block.Cryptography Block.Data ...
- Enterprise Library 2.0 -- Caching Application Block
开始写Enterprise Library 2.0的学习体会,准备先把每个部分的入门部分写好,然后再继续深入的研究每一部分,希望能得到高手的指点和建议.今天写的是Enterprise Library ...
- Enterprise Library 5.0 开发向导- 简介(1)
Enterprise Library 5.0 开发向导- 简介(1) 微软企业库 Enterprise Library 5.0 正式发布!!! 在基于微软.NET 框架开发的应用程序中,无论是企业级的 ...
最新文章
- css中font-family的中文字体
- shell 语法格式
- 【Java】ArrayList 列表的泛型
- property field java_Java 中 field 和 variable 区别及相关术语解释
- svn导出java项目_【SVN】导出项目后报错汇总
- USACO 保护花朵 Protecting the Flowers, 2007 Jan
- Kafka设计原理看了又忘,忘了又看?
- 10分钟学会用Python轻松玩转Excel
- 单片机开发设计之十层功力
- 使用shiro框架,注销问题的解决
- CF876 F 思维 枚举
- asp.net mvc4 设置build项目时,编译view页面
- java常量池在哪里_java常量池在哪?有什么用处?
- 为何在网络上很难赚到钱?网络副业赚钱真的很难做吗?
- Js日期函数-Date方法
- 第12周前端学习周报
- cinamon桌面添加xkill快捷键
- 性感荷官在线发牌,真的靠谱吗?
- php 复制文件夹并压缩到最小_php压缩多个文件打包成zip并下载到本地
- QT 图片浏览器(一)
热门文章
- 善待离职员工,让他们成为企业的财富
- SpringBoot(十五)_springboot实现预览pdf
- API接口自动化之2 处理http请求的返回体,对返回体做校验
- 28个HTML5特征、窍门和技术
- [Android]打开eclipse报错:发现了以元素 'd:skin' 开头的无效内容。此处不应含有子元素。...
- 使用阿里云的maven私服的setting.xml, 提高maven项目jar下载速度
- 51Nod-1082 与7无关的数【进制+打表】
- 搭建LoadRunner中的场景(三)场景的执行计划
- [转] MMO即时战斗:地图角色同步管理和防作弊实现
- LAMP_apache安装_2