实战1.1下Web.Config配置标记configSections

genson

Microsoft ASP.NET 1.1
Microsoft Visual Studio2003
摘要:了解如何自定义一个新的Section

SDK描述:

您可以用自己的 XML 配置标记扩展标准的 ASP.NET 配置设置集。若要完成该操作,您必须创建自己的配置节处理程序。该处理程序必须是一个实现 IConfigurationSectionHandler 接口的 .NET Framework 类。节处理程序解释并处理 Web.config 文件特定部分中 XML 标记中定义的设置并根据配置设置返回适当的配置对象。处理程序类返回的配置对象可以是任何数据结构;它不限于任何基配置类或配置格式。
大体的意思是要我们写一个类并实现IConfigurationSectionHandler接口,IConfigurationSectionHandler接口只有一个方法

object Create(
   object parent,
   object configContext,
   XmlNode section
);

程序大体功能。。通过Section配置节读取数据库连接串,把配置信息保存到Cache里面。再利用反射读取工厂模式的DataProvider

首先,我们先写一个读取Web.Config配置节的ProviderConfig类

None.gif using  System;
None.gif
using  System.Xml ;
None.gif
using  System.Configuration ;
None.gif
using  System.Web ;
None.gif
namespace  RssLayer.Configuration
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ProviderConfig 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ProviderConfig
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public ProviderConfig(string _type,string _connectionstring)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            type 
= _type ;
InBlock.gif            connectionstring 
=_connectionstring ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string type;
InBlock.gif        
public string Type
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return type ;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string connectionstring;
InBlock.gif        
public string ConnectionString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return connectionstring;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static ProviderConfig GetConfig()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(HttpContext.Current.Cache["ProviderConfig"==null)
InBlock.gif                HttpContext.Current.Cache.Insert(
"ProviderConfig",ConfigurationSettings.GetConfig("RssConfig/ProviderConfig"));
InBlock.gif
InBlock.gif            
return (ProviderConfig)HttpContext.Current.Cache["ProviderConfig"];
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
internal static ProviderConfig GetConfig(XmlNode section)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(section.Attributes.Count  > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string type=string.Empty ;
InBlock.gif                
string connstr = string.Empty ;
InBlock.gif                
if(section.Attributes["type"]!=null)
InBlock.gif                    type
= section.Attributes["type"].Value ;
InBlock.gif                
if(section.Attributes["connectionString"]!=null)
InBlock.gif                    connstr 
= section.Attributes["connectionString"].Value ;
InBlock.gif
InBlock.gif                ProviderConfig c 
= new ProviderConfig(type,connstr);
InBlock.gif                
return c;    
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

 

再次,我们写一个类实现IConfigurationSectionHandler接口类。

None.gif using  System;
None.gif
using  System.Configuration ;
None.gif
using  System.Xml ;
None.gif
namespace  RssLayer.Configuration
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ProviderConfigSectionHandler 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public sealed class ProviderConfigSectionHandler:IConfigurationSectionHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public object Create(object parent,object configContext,
InBlock.gif            XmlNode section)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ProviderConfig.GetConfig(section);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

最后,我们根据写好的程序去配置Web.Config

None.gif       < configSections >
None.gif         
< sectionGroup  name ="RssConfig" >
None.gif            
< section  name ="ProviderConfig"  type ="RssLayer.Configuration.ProviderConfigSectionHandler,RssLayer" ></ section >
None.gif        
</ sectionGroup >
None.gif            
</ configSections >     
None.gif    
None.gif    
None.gif    
< RssConfig >
None.gif        
< ProviderConfig  type ="RssLayer.DataProvider.SqlDataProvider,RssLayer"  connectionString ="Server=rss;UID=sa;Pwd=123;DataBase=rsscn" ></ ProviderConfig >
None.gif    
</ RssConfig >
最后我们数据抽象类DataProvider调用
None.gif using  System;
None.gif
using  System.Data ;
None.gif
using  System.Web;
None.gif
using  System.Web.UI ;
None.gif
using  System.Web.Caching ;
None.gif
using  System.Reflection ;
None.gif
using  System.Collections.Specialized ;
None.gif
using  RssLayer.Object ;
None.gif
using  RssLayer.Configuration ;
None.gif
None.gif
namespace  RssLayer.DataProvider
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// RsscnProvider 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif    public abstract class RssDataProvider    dot.gif{
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
"Instance"#region"Instance"
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Instance
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static RssDataProvider Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Web.Caching.Cache cache 
= HttpRuntime.Cache ;
InBlock.gif                
if(cache["DataProvider"]==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ProviderConfig config 
= ProviderConfig.GetConfig();
InBlock.gif                    
string[] types = config.Type.Split(',');
InBlock.gif                    AssemblyName  name 
= new AssemblyName();
InBlock.gif                    name.Name
=types[1];
InBlock.gif                    Assembly ass 
= Assembly.Load(name);
InBlock.gif                    Type t 
=  ass.GetType(types[0]);
InBlock.gif                    
//object obj = Activator.CreateInstance(t,new object[]{config.ConnectionString});
InBlock.gif                    
//Or
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    ConstructorInfo construct = t.GetConstructor(new Type[]dot.gif{typeof(string)});
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
object obj  = construct.Invoke(new object[]dot.gif{config.ConnectionString});
InBlock.gif
InBlock.gif                    cache[
"DataProvider"= obj;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
return (RssDataProvider)cache["DataProvider"];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}
本人比较少写文章,有可能描述不清的,欢迎赐教与修正。如果管理员觉得不好的。可以撤销在首页。本程序在 http://www.rsscn.net通过。

Msn:genson123@hotmail.com

转载于:https://www.cnblogs.com/genson/archive/2006/04/10/371461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值