Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Type A cannot be casted to Type B ( InvalidCastException)... Context hell?

I'm having a very unpleasant issue with my webapp.

The app is designed as follow :

  • The root App is loading a Flex SWF, which in turn loads a 3rd party Flex SWF module in a sub app (MagickECB).
  • Both Apps reference Albums.dll, dll found in /bin and /MagickECB/bin and both Apps share the same Application Pool in IIS.

EDIT : That's two projects, the root app project and the subapp project. Subapp project reference root app project.

When loading the sub app's flex module from the root app's flex application, we randomly get a cast error as follow :

There was an error getting configuration of Photobook: [A]Albums.LocalizationConfiguration cannot be cast to [B]Albums.LocalizationConfiguration. Type A originates from 'Albums, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\magickecb\4414db97\126f5aaf\assembly\dl3\cf281292\4a6ecec8_8a7bcb01\Albums.DLL'. Type B originates from 'Albums, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\magickecb\4414db97\126f5aaf\assembly\dl3\017fab88\a91238d1_7977cb01\Albums.dll'. at Albums.LocalizationConfiguration.GetConfig() at Albums.CGlobal.GetUserLocale(String userHandle)

Trying to investigate this issue, I found out the GetConfig method causing the cast error returned

return (LocalizationConfiguration)ConfigurationManager.GetSection("Localization/Localization");

The section in the Web.config is declared as follow : (EDIT : root app's web.config)

<sectionGroup name="Localization"> 
    <section name="Localization" type="Albums.LocalizationConfigurationHandler, Albums"/>
</sectionGroup>

My guess would be that the internal code of GetSection tries to load Albums.dll in the "LoadFrom" context and therefore cause a conflict with the original Albums.dll loaded in the /bin path (see context problematics)

I also note that other dlls in /MagickECB/Bin also reference Albums.dll, so I rebuild all projects so that every DLL reference the same version of Albums.dll

Last, if you check the dll full path in Temporary files, the extensions are different (uppercase vs lowercase)..

Any input on how to solve this problem would be greatly appreciated !!

like image 880
Breakdown Avatar asked Nov 08 '10 10:11

Breakdown


1 Answers

Double-check your references, if you compare your two dll locations they are different (extension upper case vs lower case is not an issue):

Temporary ASP.NET Files\magickecb\4414db97\126f5aaf\assembly\dl3\ cf281292\4a6ecec8_8a7bcb01 \Albums.DLL

Temporary ASP.NET Files\magickecb\4414db97\126f5aaf\assembly\dl3\ 017fab88\a91238d1_7977cb01 \Albums.dll

Probably means you're referencing two different files - maybe referencing one as a project and the other directly as a file. Do you have any warnings when you try building your web application regarding references?

EDIT: __AssemblyInfo__.ini file in above folders will give you path from which Albums.dll had been copied.

like image 176
Mihailo Avatar answered Oct 03 '22 23:10

Mihailo