实战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类
using System;
using System.Xml ;
using System.Configuration ;
using System.Web ;
namespace RssLayer.Configuration
{
/// <summary>
/// ProviderConfig 的摘要说明。
/// </summary>
public class ProviderConfig
{
public ProviderConfig(string _type,string _connectionstring)
{
type = _type ;
connectionstring =_connectionstring ;
}
private string type;
public string Type
{
get{return type ;}
}
private string connectionstring;
public string ConnectionString
{
get{return connectionstring;}
}
public static ProviderConfig GetConfig()
{
if(HttpContext.Current.Cache["ProviderConfig"] ==null)
HttpContext.Current.Cache.Insert("ProviderConfig",ConfigurationSettings.GetConfig("RssConfig/ProviderConfig"));
return (ProviderConfig)HttpContext.Current.Cache["ProviderConfig"];
}
internal static ProviderConfig GetConfig(XmlNode section)
{
if(section.Attributes.Count > 0)
{
string type=string.Empty ;
string connstr = string.Empty ;
if(section.Attributes["type"]!=null)
type= section.Attributes["type"].Value ;
if(section.Attributes["connectionString"]!=null)
connstr = section.Attributes["connectionString"].Value ;
ProviderConfig c = new ProviderConfig(type,connstr);
return c;
}
return null;
}
}
}
using System.Xml ;
using System.Configuration ;
using System.Web ;
namespace RssLayer.Configuration
{
/// <summary>
/// ProviderConfig 的摘要说明。
/// </summary>
public class ProviderConfig
{
public ProviderConfig(string _type,string _connectionstring)
{
type = _type ;
connectionstring =_connectionstring ;
}
private string type;
public string Type
{
get{return type ;}
}
private string connectionstring;
public string ConnectionString
{
get{return connectionstring;}
}
public static ProviderConfig GetConfig()
{
if(HttpContext.Current.Cache["ProviderConfig"] ==null)
HttpContext.Current.Cache.Insert("ProviderConfig",ConfigurationSettings.GetConfig("RssConfig/ProviderConfig"));
return (ProviderConfig)HttpContext.Current.Cache["ProviderConfig"];
}
internal static ProviderConfig GetConfig(XmlNode section)
{
if(section.Attributes.Count > 0)
{
string type=string.Empty ;
string connstr = string.Empty ;
if(section.Attributes["type"]!=null)
type= section.Attributes["type"].Value ;
if(section.Attributes["connectionString"]!=null)
connstr = section.Attributes["connectionString"].Value ;
ProviderConfig c = new ProviderConfig(type,connstr);
return c;
}
return null;
}
}
}
再次,我们写一个类实现IConfigurationSectionHandler接口类。
using System;
using System.Configuration ;
using System.Xml ;
namespace RssLayer.Configuration
{
/// <summary>
/// ProviderConfigSectionHandler 的摘要说明。
/// </summary>
public sealed class ProviderConfigSectionHandler:IConfigurationSectionHandler
{
public object Create(object parent,object configContext,
XmlNode section)
{
return ProviderConfig.GetConfig(section);
}
}
}
using System.Configuration ;
using System.Xml ;
namespace RssLayer.Configuration
{
/// <summary>
/// ProviderConfigSectionHandler 的摘要说明。
/// </summary>
public sealed class ProviderConfigSectionHandler:IConfigurationSectionHandler
{
public object Create(object parent,object configContext,
XmlNode section)
{
return ProviderConfig.GetConfig(section);
}
}
}
最后,我们根据写好的程序去配置Web.Config
<configSections>
<sectionGroup name="RssConfig">
<section name="ProviderConfig" type="RssLayer.Configuration.ProviderConfigSectionHandler,RssLayer"></section>
</sectionGroup>
</configSections>
<RssConfig>
<ProviderConfig type="RssLayer.DataProvider.SqlDataProvider,RssLayer" connectionString="Server=rss;UID=sa;Pwd=123;DataBase=rsscn"></ProviderConfig>
</RssConfig>
最后我们数据抽象类DataProvider调用<sectionGroup name="RssConfig">
<section name="ProviderConfig" type="RssLayer.Configuration.ProviderConfigSectionHandler,RssLayer"></section>
</sectionGroup>
</configSections>
<RssConfig>
<ProviderConfig type="RssLayer.DataProvider.SqlDataProvider,RssLayer" connectionString="Server=rss;UID=sa;Pwd=123;DataBase=rsscn"></ProviderConfig>
</RssConfig>
using System;
using System.Data ;
using System.Web;
using System.Web.UI ;
using System.Web.Caching ;
using System.Reflection ;
using System.Collections.Specialized ;
using RssLayer.Object ;
using RssLayer.Configuration ;
namespace RssLayer.DataProvider
{
/// <summary>
/// RsscnProvider 的摘要说明。
/// </summary>
public abstract class RssDataProvider {
#region"Instance"
/// <summary>
/// Instance
/// </summary>
public static RssDataProvider Instance
{
get
{
System.Web.Caching.Cache cache = HttpRuntime.Cache ;
if(cache["DataProvider"]==null)
{
ProviderConfig config = ProviderConfig.GetConfig();
string[] types = config.Type.Split(',');
AssemblyName name = new AssemblyName();
name.Name=types[1];
Assembly ass = Assembly.Load(name);
Type t = ass.GetType(types[0]);
//object obj = Activator.CreateInstance(t,new object[]{config.ConnectionString});
//Or
ConstructorInfo construct = t.GetConstructor(new Type[]{typeof(string)});
object obj = construct.Invoke(new object[]{config.ConnectionString});
cache["DataProvider"] = obj;
}
return (RssDataProvider)cache["DataProvider"];
}
}
#endregion
}
}
本人比较少写文章,有可能描述不清的,欢迎赐教与修正。如果管理员觉得不好的。可以撤销在首页。本程序在http://www.rsscn.net通过。using System.Data ;
using System.Web;
using System.Web.UI ;
using System.Web.Caching ;
using System.Reflection ;
using System.Collections.Specialized ;
using RssLayer.Object ;
using RssLayer.Configuration ;
namespace RssLayer.DataProvider
{
/// <summary>
/// RsscnProvider 的摘要说明。
/// </summary>
public abstract class RssDataProvider {
#region"Instance"
/// <summary>
/// Instance
/// </summary>
public static RssDataProvider Instance
{
get
{
System.Web.Caching.Cache cache = HttpRuntime.Cache ;
if(cache["DataProvider"]==null)
{
ProviderConfig config = ProviderConfig.GetConfig();
string[] types = config.Type.Split(',');
AssemblyName name = new AssemblyName();
name.Name=types[1];
Assembly ass = Assembly.Load(name);
Type t = ass.GetType(types[0]);
//object obj = Activator.CreateInstance(t,new object[]{config.ConnectionString});
//Or
ConstructorInfo construct = t.GetConstructor(new Type[]{typeof(string)});
object obj = construct.Invoke(new object[]{config.ConnectionString});
cache["DataProvider"] = obj;
}
return (RssDataProvider)cache["DataProvider"];
}
}
#endregion
}
}
Msn:genson123@hotmail.com