Unable to cast object of type x to x with the WebConfigurationMana

S

Scott

Hello,

Using VS2008 in a C# web service application, a class has been created that
inherits from the ConfigurationSelection. This class file has been placed in
the App_Code folder. The web.config has been updated with the necessary
section.

Using System.Web.Configuration.WebConfiguration.GetSection(), the config
information is returned without any issues when the GetSection is set to an
object. When the object is casted explicitly to the class that inherits the
ConfigurationSelection, there is the runtime error of "Unable to cast object
of type x to x" when the application is run in debug mode.

When debugging, the types of the object returned from the GetSection is
correct.

Using reflection, the path to the assembly of the object that was returned
from the GetSection retunred the following path (using
theobject.GetType().Assembly.Location)
C:\Users\me\AppData\Local\Temp\Temporary ASP.NET
Files\root\6239671a\38ecbf2f\assembly\dl3\c7d35787\4e04a121_50bcc801\test.DLL

When a new class was created the path of the assembly was:
C:\Users\me\AppData\Local\Temp\Temporary ASP.NET
Files\root\6239671a\38ecbf2f\App_Code.zvdojd_1.dll


How do I resolve this issue?

Thanks,
 
S

Steven Cheng [MSFT]

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:
(e-mail address removed).

==================================================
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.
--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top