怀疑WebConfigurationManager.GetSection有Bug

 

环境:

Windows Server 2003 Enterprise Edition with SP1

Microsoft .NET Framework v2.0.50727

Microsoft Visual Studio 2005 Version 8.0.50727.42 (RTM.050727-4200)

 

一句话摘要:System.Web.Configuration.WebConfigurationManager.GetSection的返回值不能cast成对应的类型,怀疑其返回类型不正确,是bug。估计System.Configuration.ConfigurationManager.GetSection有类似问题,不过没有测试过。

 

System.Web.Configuration.WebConfigurationManager静态类提供了操作Web Application配置文件的很多实用方法。

WebConfiguration.GetSection用于返回一个ConfigurationSection,很奇快MSDN里面说其返回值是Object,既然用于返回ConfigurationSection,为何要用Object作为返回类型而不返回ConfigurationSection类型?System.Configuration.Configuration.GetSection就返回ConfigurationSection

文档里面说要将返回值Castsection的实际类型,还推荐使用as

 "Notes to Implementers The return value must be cast to the expected configuration type before use. To avoid possible casting exceptions, you should use a conditional casting operation like the as operator in C# or the TryCast function in Visual Basic. "

MSDN里的示例:

// Show the use of GetSection(string).
// It gets the appSettings section.
// It can only be called from within a Web application. // Note. If you run this method from a console app // you get a NullReferenceException.
static void GetSection19()
{
try
{
// Get the appSettings section.
AppSettingsSection appSettingsSection =
WebConfigurationManager.GetSection("appSettings")
as AppSettingsSection;
// Get the appSettings key,value pairs collection.
KeyValueConfigurationCollection appSettings =
appSettingsSection.Settings;
// Get the collection enumerator.
IEnumerator appSettingsEnum =
appSettings.GetEnumerator();
// Loop through the collection and // display the appSettings key, value pairs.
int i = 0;
Console.WriteLine("[Display the appSettings]");
while (appSettingsEnum.MoveNext())
{
string key = appSettings.AllKeys[i].ToString();
Console.WriteLine("Key: {0} Value: {1}",
key, appSettings[key]);
i += 1;
}
Console.WriteLine();
}
catch (NullReferenceException e)
{
Console.WriteLine(e.ToString());
}
}

事实上根本就不能cast,如果用as,每次结果都为null,如果explicit cast则每次都是InvalidCastException。用Debugger看,发现WebConfigurationManager.GetSection返回值的实际类型是System.Configuration.KeyValueInternalCollection,而MSDN里根本找不到这个类的文档。返回值有个名为_rootNon-public member,类型为AppSettingsSection,看来这个返回值是一个wrapper,可是怎么用这个wrapper呢?

 

google了以下,在odetocode.com找到一片文章The Configuration API in .NET 2.0 (by Scott)

http://odetocode.com/Articles/418.aspx,其中片断:

protected void readImpersonationButton_Click(object sender, EventArgs e)

{

    // note: currently broken in BETA2, works in post BETA2 builds.

    // in BETA2 GetSection returns a wrapper

    // that will not cast to IdentitySection

    IdentitySection section;

    section = WebConfigurationManager.GetSection("system.web/identity")

                    as IdentitySection;

 

    if (section != null)

    {

        WriteMessage("Impersonate = " + section.Impersonate);

    }

}

我用的不是beta2,是RTM。很奇怪,feature or bug?如果是feature,为什么文档还是按照以前的behavior写?怀疑是bug,为了保证工作进度,只好work around

AppSettingsSection section = WebConfigurationManager.OpenWebConfiguration("~/web.config").GetSection("appSettings") as AppSettingsSection;

替换掉

AppSettingsSection section = WebConfigurationManager.GetSection("appSettings") as AppSettingsSection;

 

Advertisements
This entry was posted in .NET. Bookmark the permalink.

4 Responses to 怀疑WebConfigurationManager.GetSection有Bug

  1. 筱玥 says:

    新年快乐~~~~

  2. ethan says:

    今年就没来过?

  3. jingling says:

    来了,所以踩一脚,新学期加油~~~

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s