简体   繁体   English

C#Type A无法转换为B类(InvalidCastException)...上下文地狱?

[英]C# Type A cannot be casted to Type B ( InvalidCastException)… Context hell?

I'm having a very unpleasant issue with my webapp. 我的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 ). - 根应用程序正在加载Flex SWF,后者又在子应用程序( MagickECB )中加载第三方Flex SWF模块。
- Both Apps reference Albums.dll , dll found in /bin and /MagickECB/bin and both Apps share the same Application Pool in IIS. - 两个应用程序都引用了Albums.dll ,在/ bin/ MagickECB / bin中找到的dll,两个应用程序在IIS中共享相同的应用程序池。

EDIT : That's two projects, the root app project and the subapp project. 编辑 :这是两个项目,根应用程序项目和子应用程序项目。 Subapp project reference root app project. Subapp项目参考根应用程序项目。

When loading the sub app's flex module from the root app's flex application, we randomly get a cast error as follow : 从根应用程序的flex应用程序加载子应用程序的flex模块时,我们会随机获得一个强制转换错误,如下所示:

There was an error getting configuration of Photobook: [A]Albums.LocalizationConfiguration cannot be cast to [B]Albums.LocalizationConfiguration. 获取Photobook的配置时出错:[A] Albums.LocalizationConfiguration无法转换为[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 '. 类型A源自'Albums,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'在上下文' Default '位置' 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 '. 类型B源自位于' C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\ Temporary ASP.NET Files的上下文' LoadFrom '中的'Albums,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null' \\ magickecb \\ 4414db97 \\ 126f5aaf \\ assembly \\ dl3 \\ 017fab88 \\ a91238d1_7977cb01 \\ Albums.dll '。 at Albums.LocalizationConfiguration.GetConfig() at Albums.CGlobal.GetUserLocale(String userHandle) 在Albums.CGlobal.GetUserLocale(String userHandle)中的Albums.LocalizationConfiguration.GetConfig()

Trying to investigate this issue, I found out the GetConfig method causing the cast error returned 试图调查此问题,我发现GetConfig方法导致返回强制转换错误

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

The section in the Web.config is declared as follow : ( EDIT : root app's web.config) Web.config中的部分声明如下:( 编辑 :根应用程序的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 ) 我的猜测是GetSection的内部代码试图在“LoadFrom”上下文中加载Albums.dll,因此导致与/ bin路径中加载的原始Albums.dll发生冲突( 请参阅上下文问题

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 我还注意到/ MagickECB / Bin中的其他dll也引用了Albums.dll,所以我重建所有项目,以便每个DLL引用相同版本的Albums.dll

Last, if you check the dll full path in Temporary files, the extensions are different (uppercase vs lowercase).. 最后,如果检查临时文件中的dll完整路径,则扩展名不同(大写与小写)。

Any input on how to solve this problem would be greatly appreciated !! 关于如何解决这个问题的任何意见将不胜感激!!

Double-check your references, if you compare your two dll locations they are different (extension upper case vs lower case is not an issue): 仔细检查您的参考文献,如果您比较两个dll位置它们是不同的(扩展大写与小写不是问题):

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

Temporary ASP.NET Files\\magickecb\\4414db97\\126f5aaf\\assembly\\dl3\\ 017fab88\\a91238d1_7977cb01 \\Albums.dll 临时ASP.NET文件\\ 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? 当您尝试构建有关参考的Web应用程序时,您是否有任何警告?

EDIT: __AssemblyInfo__.ini file in above folders will give you path from which Albums.dll had been copied. 编辑:上面文件夹中的__AssemblyInfo__.ini文件将为您提供复制Albums.dll的路径。

如果没有您的问题的解释,我建议在GAC注册程序集并从那里使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 派生类型时C#InvalidCastException - C# InvalidCastException while getting derived type InvalidCastException:无法从源类型转换为目标类型。 (Unity C#,foreach列表循环) - InvalidCastException: Cannot cast from source type to destination type. (Unity c# ,foreach list loop) unity C# InvalidCastException:无法从源类型转换为目标类型 - Unity C# InvalidCastException: Cannot cast from source type to destination type InvalidCastException:无法使用Unity(C#)从源类型转换为目标类型 - InvalidCastException: Cannot cast from source type to destination type, using Unity (C#) 知道类型A的变量是否可以转换为类型B. - Know if variable of type A can be casted to type B System.InvalidCastException:Object 不能存储在这种类型的数组中。 在 C# - System.InvalidCastException: Object cannot be stored in an array of this type. in C# 防止在工作目录中加载dll,InvalidCastException类型A不能强制转换为类型B - Preventing loading dll in working directory, InvalidCastException Type A cannot be cast to Type B C#抽象泛型类型:无法将类型&#39;A:B&#39;隐式转换为&#39;B&#39; - C# abstract generic type : Cannot implicitly convert Type 'A : B' to 'B' c#类型a =新类型(b); - c# type a = new type(b); C#:无法将 A 型转换为 B 型,其中 A = B - C#: Unable to convert type A to type B where A = B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM