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

  • Thread starter Thread starter Scott
  • Start date Start date
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,
 
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.
--------------------
 
Back
Top