Hi Fixer,
From your description, you're creating a custom ConfigurationSection and
use it in ASP.NET application, however, you encountered " object can not be
cast to ..." error at runtime when try referecing to the custom section
object, correct?
Regarding on this issue, I've performed some tests on my side, seems a very
simple and typical custom configurationSection can work correctly in
ASP.NET application. I think the problem is likely be specific to the
project or environment. I would suggest you also try creating a very simple
custom configurationSection and use it in a new created ASP.NET web site
project(or you can also try move the custom section into a separate class
library project).
In addition, I'm wondering how to configure the section in web.config and
reference it in code. Here is my test code(include custom section class,
web.config section and referencing code):
#the custom section class is put in the App_Code folder of the ASP.NET
application
===========custom section code=================
public class MySection: ConfigurationSection
{
public MySection()
{
}
[ConfigurationProperty("key", IsKey = true)]
public string Key
{
get
{
return ((string)(base["key"]));
}
set
{
base["key"] = value;
}
}
[ConfigurationProperty("value")]
public string Value
{
get
{
return ((string)(base["value"]));
}
set
{
base["value"] = value;
}
}
}
=========web.config=============
<configSections>
<section name="MySection" type="MySection, __code" />
</configSections>
.....................
========code in page=========
ConfigurationSection section =
WebConfigurationManager.GetSection("MySection") as ConfigurationSection;
if (section != null)
Response.Write("<br/>mysection: " + ((MySection)section).Key +
"|" + ((MySection)section).Value);
============================
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: =?Utf-8?B?U2NvdHQ=?= <the_Fixer@online.nospam>
>Subject: Unable to cast object of type x to x with the WebConfigurationMana
>Date: Thu, 22 May 2008 14:55:00 -0700
>Hello,
>
[quoted text clipped - 25 lines]
>
>Thanks,