gpt4 book ai didi

.net - 转换为 .NET 4 时应用程序崩溃(也绑定(bind) XML 程序集失败)

转载 作者:数据小太阳 更新时间:2023-10-29 01:52:05 24 4
gpt4 key购买 nike

我有一个解决方案,我正在尝试将其从 .NET 2 转换为 .NET 4。此解决方案中有多个项目,其中一些在 C# 中,一些在 VB 中。它们中的大多数使用 system.xml 来创建和解析 XML。

我已将所有项目重新定位到 .NET 4,并且它可以编译。但是,当我尝试运行时,我几乎立即得到一个 System.FieldAccessException,提示“如果类库中字段的访问级别已更改,请重新编译引用该库的所有程序集。”。在此之前我确实重新编译了整个解决方案(并在之后再次尝试,没有任何变化)。

然后我使用程序集绑定(bind)日志查看器查看是否有任何绑定(bind)失败,因为这是在它引用项目中的不同 dll 时发生的。果然,那里有失败。它们在两个不同的 VB 项目中,它们看起来都像这样:


The operation failed. 
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable <path to my exe>
--- A detailed error log follows.

=== Pre-bind state information ===
LOG: DisplayName = <projname>.XmlSerializers, Version=2013.20.0.0, Culture=neutral, PublicKeyToken=null, processorArchitecture=MSIL (Fully-specified)
LOG: Appbase = <projectpath>
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = <projname>.vshost.exe Calling assembly : System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
=== LOG: This bind starts in default load context.
LOG: Using application configuration file: <project path\config file>
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///<project path>/<projname>.XmlSerializers.DLL.
LOG: Attempting download of new URL file:///<project path>/<projname>.XmlSerializers/CMG.XmlSerializers.DLL.
LOG: Attempting download of new URL file:///<project path>/<projname>.XmlSerializers.EXE.
LOG: Attempting download of new URL file:///<project path>/<projname>.XmlSerializers.EXE.
LOG: All probing URLs attempted and failed.

果然,在我的obj文件夹中没有任何这样的文件。但以前也没有,而且效果很好。 (注意:这些绑定(bind)错误只出现在使用 XML 的两个 VB 项目中,而不是 C# 项目。)我删除并重新添加了对 system.xml 的引用,清理了解决方案,然后重建 - 仍然没有。

我检查过 system.xml v4 在 GAC 中,公钥与它在这里寻找的相同。我在构建选项卡上看到了对名为“生成序列化程序集”的项目选项的引用。在 VB 项目中,它位于编译 > 高级选项下。它被设置为自动。我试着打开它,重建,但没有任何变化。我不确定下一步该去哪里——这对我来说是全新的领域。那么这个文件应该来自哪里,为什么不存在,为什么没有它它在 v2 中工作?还是所有这些 xml 东西都是转移注意力的东西,与原始问题无关?我开始担心这一点,但现在还不确定。任何指导将不胜感激。

预计到达时间:如果我在 VS 之外运行它,错误日志会将崩溃显示为事件 1000,并带有“故障模块名称:kernelbase.dll”。到目前为止,我对此所做的搜索表明在 64 位操作系统上运行的 32 位应用程序存在问题,但我已经将整个东西设置为编译为任何 CPU。事实证明,当我运行 .NET 2 版本时,我也看到了相同的绑定(bind)错误,即使它在那里运行良好。我开始越来越认为 XML 是转移注意力的东西。

进一步的 ETA:XML 确实是一个转移注意力的问题。在下面添加了解决方案作为答案。

最佳答案

所以毕竟是安全问题。根据 http://msdn.microsoft.com/en-us/library/stfy7tfc.aspx :

A member is security-critical if it has the SecurityCriticalAttribute, if it belongs to a type that has the SecurityCriticalAttribute, or if it is in a security-critical assembly. Beginning with the .NET Framework 4, the rules for accessing security-critical members are as follows:

Transparent code cannot use reflection to access security-critical members, even if the code is fully trusted. A MethodAccessException, FieldAccessException, or TypeAccessException is thrown.

Code that is running with partial trust is treated as transparent.

进一步阅读,我发现随着 .NET 4 中的这一变化,他们实际上为这些规则引入了 2 个级别 - 级别 2 是这些新规则,级别 1 是 .NET 2 规则,用于向后兼容。我将无法访问的程序集设置为使用 1 级规则,方法是添加:

<Assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)> 

到 AssemblyInfo.vb 文件(并添加 system.security 作为引用)。现在一切正常。显然,理想的情况是将其更改为使用 2 级规则,但这是在进行中。这至少不仅暂时解决了问题,而且告诉我究竟是什么问题。谢谢大家!

关于.net - 转换为 .NET 4 时应用程序崩溃(也绑定(bind) XML 程序集失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18156188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com