将.net 4.5应用程序降级到4.0

我想将.net库从框架版本4.5降级到.net 4.0。

我使用nuget安装了几个库:

  • Microsoft.AspNet.WebApi.Client及其依赖项:
  • Newtonsoft.Json
  • System.Net.Http(Microsoft .Net 4 HTTP客户端库)

我做了以下事情

  • 在我的解决方案中的每个项目的设置中,我将目标框架设置为4.0 。 之后我尝试重建我的解决方案但当然没有成功因为错误The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) ,HTTP Client libs也是如此

  • 使用nuget ui manager我删除了依赖项并尝试重新安装 。 但是有一个错误Could not install package 'Microsoft.AspNet.WebApi.Client 5.1.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. Could not install package 'Microsoft.AspNet.WebApi.Client 5.1.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

我的问题是:我可以降级这个项目,还是应该用一些支持.net 4替换这些库并重写代码的某些部分?

.NET 4.5是.NET 4.0的就地升级,这意味着CLR是相同的,但添加了新的库以及错误修复和性能改进,并且两者都指向.NET CLR 4。

在您的情况下,由于已经安装了.NET 4.5,因此4.5更新完全取代了.NET 4.0运行时。

即使您将项目更改为.NET 4.0,内部使用的库仍然指向4.5但仅限于可用于4.0的库,但行为可能与您在纯.NET 4中的预期不同。

因此,为了将您的项目从4.5降级到4.0,我同意您的意见,您需要:

  1. 仅将.NET框架重新安装到.NET 4(如果不再需要.NET 4.5)
  2. 将项目更改为指向.NET 4
  3. 重写.NET 4不支持的部分代码(例如,在您的情况下,Microsoft.AspNet.WebApi.Client 5.1.1随.NET 4.5中的WebApi2一起提供,您需要将其降级为使用方式如.NET 4所提供的)

汉塞尔曼和里克解释得非常好。