使用“useLegacyV2RuntimeActivationPolicy”时出现问题&应用程序中支持的运行时

发布于 2024-08-26 13:51:44 字数 1380 浏览 12 评论 0原文

我已经修改了几个不同应用程序的 .config 文件,如下所示:

<startup useLegacyV2RuntimeActivationPolicy="true">
   <supportedRuntime version="v4.0"/>
</startup>  

当我对 devenv.exe.config (VS 2005 - 不要问:))执行此操作时,一切都很好 - 大多数 Visual Studio 使用 .NET 2.0,但我能够使用针对 .NET 4.0 框架的程序集。

我尝试对自定义 .exe 执行相同的操作,该 .exe 恰好基于 MS CAB(稍作修改),并且混合了 WPF 和 WinForms 内容。一旦我修改了该应用程序的应用程序配置文件,我就开始在应用程序启动期间的某个时候收到此异常:

撤消操作遇到的上下文与相应设置操作中应用的上下文不同。可能的原因是在线程上设置了上下文并且未恢复(撤消)。 System.InvalidOperationException:撤消操作遇到的上下文与相应的设置操作中应用的上下文不同。可能的原因是在线程上设置了上下文并且未恢复(撤消)。

有一个很长的堆栈跟踪,它不直接显示我的应用程序代码中的任何内容(只是一堆 MS 程序集)。

如果我将应用程序的 .config 文件修改为:

<startup useLegacyV2RuntimeActivationPolicy="true">
</startup>

即删除supportedRuntime元素,则应用程序不会抛出此异常。但是,当我在代码中尝试加载 .NET 4 程序集时,如果失败并出现以下情况:

System.BadImageFormatException:无法加载文件或程序集“”或其依赖项之一。该程序集是由比当前加载的运行时更新的运行时构建的,无法加载。

我想这是预料之中的。

我有两个问题:

1) 知道为什么当我修改此应用程序的配置文件以包含supportedRuntime 元素、添加 .NET 4 支持时收到 System.InvalidOperationException 异常吗?以及有关我可以采取哪些措施的任何建议?

2) 如果答案是“不知道为什么/不知道你能做什么”,那么我的 .NET 3.5 SP1 代码 (C#) 可以为有条件添加 .NET 4 运行时支持提供更细粒度的支持对于某个程序集,而不将整个应用程序转换为目标 .NET 4,或者不使用声明性配置文件方法?在某些时候,我会将整个应用程序转换为目标 .NET 4,但从短期来看,这是一项艰巨的任务,我希望有一些短期解决方案/技巧。

非常感谢您提供的任何建议!

I've modified a couple of different applications' .config file like this:

<startup useLegacyV2RuntimeActivationPolicy="true">
   <supportedRuntime version="v4.0"/>
</startup>  

When I did this for devenv.exe.config (VS 2005 - don't ask :) ), things work great - most of the Visual Studio used .NET 2.0 but I was able to make use of an assembly targeting .NET 4.0 framework.

I tried to do the same thing for a custom .exe, which happens to be based on MS CAB (slightly modified) and has a hybrid mix of WPF and WinForms content. As soon as I modified this application's app config file, I started getting this exception, sometime during application startup:

The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was Set on the thread and not reverted(undone).
System.InvalidOperationException: The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was Set on the thread and not reverted(undone).

There's a big long stack trace that doesn't show anything in my application code directly (just a bunch of MS assemblies).

If I modify the application's .config file to this:

<startup useLegacyV2RuntimeActivationPolicy="true">
</startup>

i.e.I remove the supportedRuntime element, then the application doesn't throw this exception. But when I go to the point in my code where I try to load my .NET 4 assembly, if fails with this:

System.BadImageFormatException: Could not load file or assembly '' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

I guess this is expected.

I have two questions:

1) Any idea why I'm getting the System.InvalidOperationException exception when I modify this application's configuration file to include the supportedRuntime element, adding .NET 4 support and any suggestions on what I can do about it?

2) If the answer is "no idea why/don't know what you can do about it", then is possible for my .NET 3.5 SP1 code (C#) to provide more fine grain support for conditionally adding .NET 4 runtime support for a certain assembly(ies) without converting my entire application to target .NET 4, or without using the declarative config file approach? At some point I would convert the entire application to target .NET 4, but for the short term this is daunting task and I'm hope for some short term solution/hack.

Thank you very much for any advice you can give!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2024-09-02 13:51:44

我不确定您在 VS2005 中看到的具体行为是什么,但不应该是“大多数 Visual Studio 使用 .NET 2.0,但我能够使用针对 .NET 4.0 框架的程序集”的情况。该配置应该导致进程中的所有内容在 v4 上运行。如果情况并非如此,我当然想知道它,因为它可能是一个错误。 (顺便说一句,我确信您通过将 VS2005 向前滚动到 v4 意识到您超出了受支持的场景。)

至于无效操作异常,我的猜测是您遇到了框架之间的某种不兼容问题。我确信我们也希望在这方面发现一个错误。您可以分享异常时的堆栈跟踪吗?

至于采取增量升级方法......确实没有一个好的答案。 CLR 自动支持 COM 组件的进程内 SxS 激活,因此,如果您的应用程序以可以将模块提取为 COM 组件的方式实现,那么您可以利用它。问题是大多数应用程序并不是以这种方式编写的,并且简单地将所有内容升级到 v4 比重新构建应用程序的工作量要少。

单独的程序集加载发生在触发加载的运行时中。对于正常的程序集加载,没有进程内 SxS 激活。

I'm not sure exactly what behavior you're seeing for VS2005, but it shouldn't be the case that "most of the Visual Studio used .NET 2.0 but I was able to make use of an assembly targeting .NET 4.0 framework". That config should cause everything in the process to run on v4. If this is not the case, I'd certainly like to know about it because it's likely a bug. (BTW, I'm sure you realize that you're outside a supported scenario by rolling VS2005 forward to v4.)

As to the invalid operation exception, my guess would be that you're hitting some kind of incompatibility between frameworks. I'm sure we'd like to get a bug on that as well. Can you share the stack trace at the exception?

As far as taking an incremental upgrade approach... There is not really a good answer. The CLR supports in-proc SxS activation automatically for COM components, so if your app is implemented in a way where you can extract modules as COM components, then you could leverage that. The problem is that most apps are not written in this way, and simply upgrading everything to v4 is less work that re-architecting the app.

Individual assembly loads occur in the runtime that triggered the load. There's no in-proc SxS activation for normal assembly loads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文