468,785 Members | 1,675 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

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

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.GetSecti on(), 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,

Jun 27 '08 #1
1 2109
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: =?Utf-8?B?U2NvdHQ=?= <th*******@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,

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.GetSecti on(), 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\c7d3578 7\4e04a121_50bcc801\test.D
LL
>
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.dl l
How do I resolve this issue?

Thanks,

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Pankaj Jain | last post: by
reply views Thread by hlyall1189 | last post: by
1 post views Thread by =?Utf-8?B?RGF2ZQ==?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.