473,245 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

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 2306
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A is exposed to client through remoting on Http...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
8
by: | last post by:
I have the following class : Public Class MyTreeNode Inherits TreeNode Dim mystring As String End Class Now, when I try to do this : ''''''''''''nodes is a TreeNodeCollection, s is string
0
by: sam | last post by:
Hi: I am not sure if this is the right place to post this question. Please let me know if it is not and I appreciate if someone could point me in the right direction. I am getting this error...
0
by: hlyall1189 | last post by:
Hi, I recently started upgrading some of my old vs 2003 apps to vs 2005 and used the conversion tool but now i get the following error after building the page. I have typecasted the lines as...
3
by: keithb | last post by:
What could be causing this? this code: String Com = ""; if (Com != (String)rw.ItemArray) fails at runtime with the error message: Unable to cast object of type 'System.Int32' to type...
10
by: mypetrock | last post by:
Has anyone run into this error message? Unable to cast object of type 'Foo.Bar' to type 'Foo.Bar'. I'm trying to cast an object of type Foo.Bar that I got out of a hash table into a variable...
0
by: Pieter | last post by:
Hi, I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from BindingList(Of T) ) for all my lists. The NHibernate documentation told me that I had to implement...
1
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have a master page and a content page to that master. When I try to work with them I get an inconsistent error of casting ability and it happens irregularly, which means sometimes it can work...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.