允许使用 .NET 2.0 构建的 C# 应用程序在 .NET 4.0/4.5 上运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13461185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:39:50  来源:igfitidea点击:

Allow C# application built with .NET 2.0 to run on .NET 4.0/4.5

c#.net.net-4.0.net-2.0

提问by windfly2006

We have a C# DLL (let's call it myapp.exe) built with .NET 2.0 Framework (VS2005) and we found out that our application won't work on machines where only .NET 4.0 or above is installed. To let our application work on .NET 4.0, I added the following some lines to myapp.exe.configby following this article Installing .NET Framework V4.0 and Running .NET 2.0/3.0/3.5/3.5Sp1 Applications

我们有一个myapp.exe使用 .NET 2.0 Framework (VS2005) 构建的 C# DLL(我们称之为 DLL ),但我们发现我们的应用程序无法在仅安装了 .NET 4.0 或更高版本的机器上运行。为了让我们的应用程序在 .NET 4.0 上运行,我myapp.exe.config按照这篇文章添加了以下几行代码安装 .NET Framework V4.0 和运行 .NET 2.0/3.0/3.5/3.5Sp1 应用程序

<startup>
  <supportedRuntime version="v4.0.30319"/>    
</startup>

And it is working.

它正在发挥作用。

Then I saw that this article also mentioned the following (especially second paragraph):

然后我看到这篇文章还提到了以下(尤其是第二段):

Now, I knew that you can't just take a 3.5 Service Pack 1 application and run it on the V4.0 CLR. It needs a V2.0 CLR or reconfiguring with a tag in order to bend the application to run on the V4.0 CLR and that bending might be something that you don't want to do.

What I hadn't realised though was that installing .NET 4.0 wouldn't install the bits that you need for a 2.0/3.0/3.5/3.5Sp1 application. It would only install the V4.0 CLR and the V4.0 assemblies and not additionally install the equivalent of .NET Framework V3.5 Sp1. So, you'd need to install (e.g.) .NET Framework V3.5 Sp1 yourself along with .NET 4.0

现在,我知道您不能只采用 3.5 Service Pack 1 应用程序并在 V4.0 CLR 上运行它。它需要一个 V2.0 CLR 或使用标记重新配置,以便使应用程序在 V4.0 CLR 上运行,而这种弯曲可能是您不想做的事情。

但我没有意识到的是,安装 .NET 4.0 不会安装 2.0/3.0/3.5/3.5Sp1 应用程序所需的位。它只会安装 V4.0 CLR 和 V4.0 程序集,而不会另外安装等效的 .NET Framework V3.5 Sp1。因此,您需要自己安装(例如).NET Framework V3.5 Sp1 和 .NET 4.0

From my testing it would mean that I could run my 2.0 C# application on .NET 4.0 with .NET 4.0 framework (4.0 assembly/libraries) which is contradicting to what the articles said.

从我的测试来看,这意味着我可以使用 .NET 4.0 框架(4.0 程序集/库)在 .NET 4.0 上运行我的 2.0 C# 应用程序,这与文章所说的相矛盾。

Or am I missing something here? It could be helpful if someone could clarify on this. Microsoft doesn't really make this clear at all.

还是我在这里遗漏了什么?如果有人可以对此进行澄清,可能会有所帮助。微软根本没有明确说明这一点。

采纳答案by Reed Copsey

From my testing it would mean that I could run my 2.0 C# application on .NET 4.0 with .NET 4.0 framework (4.0 assembly/libraries) which is contradicting to what the articles said.

从我的测试来看,这意味着我可以使用 .NET 4.0 框架(4.0 程序集/库)在 .NET 4.0 上运行我的 2.0 C# 应用程序,这与文章所说的相矛盾。

You canrun your application on .NET 4 using the .NET 4 assemblies. There is always the possibility that there may be a slight change in runtime behavior, however, as you won't be using the same runtime and framework which you used for development. I suspect the article is trying to suggest that you won't get the exact same behavior by just installing 4.0, though, as you've seen, it should work.

可以使用 .NET 4 程序集在 .NET 4 上运行您的应用程序。但是,运行时行为可能会略有变化,因为您将不会使用用于开发的相同运行时和框架。我怀疑这篇文章试图建议您通过安装 4.0 不会获得完全相同的行为,但是,正如您所见,它应该可以工作。

Doing this is likely fine, though I would recommend doing thorough testing of your application if this is going to be a standard deployment option.

这样做可能没问题,但如果这将成为标准部署选项,我会建议对您的应用程序进行彻底测试。

回答by user2736158

Just setting web.config to supportedRuntime version="v4.0.30319"/ is not going to be enough.

仅将 web.config 设置为 supportedRuntime version="v4.0.30319"/ 是不够的。

You need to actually open up your project in Visual Studio, change its target framework (properties-->build) to 4.0 - and THEN redploy the built solution to your 4.0 clients. Several system assemblies are different between 2.0 and 4.0 (system.web etc..) - although, as the previous answer suggested, backwards compatibility has been provided.

您需要在 Visual Studio 中实际打开您的项目,将其目标框架(属性--> 构建)更改为 4.0,然后将构建的解决方案重新部署到您的 4.0 客户端。2.0 和 4.0(system.web 等)之间的几个系统程序集是不同的 - 尽管正如前面的答案所建议的那样,已经提供了向后兼容性。

The only way to consistently provide your clients with an actuual '4.0 compatible' version, is to compile it against a 4.0 runtime. This will entail upgrading any 3rd party, open source libraries to their 4.0 versions as well.

始终为您的客户提供实际“4.0 兼容”版本的唯一方法是针对 4.0 运行时编译它。这也需要将任何 3rd 方开源库升级到它们的 4.0 版本。

回答by Scott Solmer

And I quote:

我引用:

"The .NET Framework 4 is backward-compatible with applications that were built with the .NET Framework versions 1.1, 2.0, 3.0, and 3.5. In other words, applications and components built with previous versions of the .NET Framework will work on the .NET Framework 4."

“.NET Framework 4 向后兼容使用 .NET Framework 版本 1.1、2.0、3.0 和 3.5 构建的应用程序。换句话说,使用早期版本的 .NET Framework 构建的应用程序和组件将在.NET 框架 4。”

Taken from Version Compatibility in the .NET Framework

取自.NET Framework 中的版本兼容性

You have the right idea with the App.config file, but your really limiting yourself with the one line.
Might I suggest a more liberal use of supportedRuntime lines?

您对 App.config 文件的想法是正确的,但您实际上只用一行来限制自己。
我可以建议更自由地使用 supportedRuntime 行吗?

For example:

例如:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v2.0.50727"/>
    <!-- 
    <supportedRuntime version="v3.5"/>  "The .NET Framework version 3.0 and 3.5 use version 2.0.50727 of the CLR."  
    -->
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.1,Profile=Client" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.1" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.2,Profile=Client" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.2" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.3,Profile=Client" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.3" /> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

Why is supportedRuntime version="v3.5"commented out? Remember, this configuration identifies which versions of the Common Language Runtime (CLR) your application is compatible with. There is no 3.0 or 3.5 version of the CLR. Refer to .NET Framework Versions and Dependencies

为什么被supportedRuntime version="v3.5"注释掉了?请记住,此配置标识您的应用程序与哪些版本的公共语言运行时 (CLR) 兼容。没有 3.0 或 3.5 版本的 CLR。请参阅.NET Framework 版本和依赖项

回答by MJonas

Just to be a little more concise. In the App.config you are showing support for the CLR. From .Net v2 and on there are only 2 version of the CLR. So the following will give you support of .Net v2 and on. Make sure to include your exe.config file that is built with your project otherwise you are likely to still see the popup asking to install .Net.

只是为了更简洁一点。在 App.config 中,您显示了对 CLR 的支持。从 .Net v2 开始,只有2 个版本的 CLR。因此,以下内容将为您提供对 .Net v2 及后续版本的支持。确保包含与项目一起构建的 exe.config 文件,否则您可能仍会看到要求安装 .Net 的弹出窗口。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v2.0.50727"/>
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

For support for .Net v2 to v3.5 use:

要支持 .Net v2 到 v3.5,请使用:

<supportedRuntime version="v2.0.50727"/>

For support for .Net v4.0 to 4.6 use:

要支持 .Net v4.0 到 4.6,请使用:

<supportedRuntime version="v4.0"/>

More information can be found for Configure an App to Support .NET Framework

可以找到有关配置应用程序以支持 .NET Framework 的更多信息

回答by Gour Gopal

From: microsoft docs:

来自:微软文档

snapshot .net framework

快照.net框架

you can choose any App.config file setting.

您可以选择任何 App.config 文件设置。