ASP.NET Cafe
Tips and Tricks

Solution for InvalidCastException error in an ASP.NET

Wednesday, 19 December 2007 11:49 by Dmitriy

I write a lot about errors because it's hard to find a right solution for them over internet. Many forums with a lot of offtop... makes me mad.

So, two errors: 

Unable to cast object of type 'ASP.type' to type 'ASP.type'.

or

Could not load file or assembly 'App_Web_xxxxxxxx, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

Both errors appear in ASP.NET application from time to time. There are some info about master pages, nested user controls related to this stuff. And hotfix from MS, usually. Not sure that hotifx helps, but you can try. Anyway, this error last till application automatic rebuild. So, you can just reupload/change some files of your project, and force rebuild. There are some notices that this error can occur weekly or even daily. Restart app every day is not looks good. Bad news. Avoid of master pages and user controls usage - is a bad idea. 

But immediate cause of this - incorrect automatic compiler work. So, to correct this error on production hosting, you can use precompiling.

The advantages of precompiling:

  1. You don't need to upload any source code on server. (Woo Hoo - hosting guys can't take your application)
  2. Above error not occurs, because source already compiled. (I recommend -fixednames option usage).
  3. Less delays because of recompiling.
  4. Small site size.

How to make your site precompiled (prepare for publish)?

Let's start. First of all, locate this utility C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe (your path can be different because of windows directory). After that create a cmd-file with following content:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler -fixednames -p d:\projects\web1\source\ -v / d:\projects\web1\build\

Some descriptions regarding this line:
     -p  sets physical path to your web site source
     -v  sets virtual directory where you want to publish your site ( / set the root of virtual directory, 99% that you don't need to change this )
     -fixednames binaries will be given fixed names

I'm suggest that this should be used only for production hosting, because in case of frequent updates... not very comfortable to work through build.

The article on Microsoft about issues mentioned above.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   ASP.NET
Actions: E-mail | del.icio.us | Kick it! | DZone it! | Permalink | Comments (0) | Comment RSSRSS comment feed

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Preview

January 6. 2009 09:24