大网数据广告活动
Web服务器与PHP

Troubleshoot .NET Framework targeting errors

· 52CE 我爱测

This article provides resolutions for MSBuild errors that might occur because of reference issues.

Applies to: Visual Studio

This topic describes MSBuild errors that might occur because of reference issues and how you can resolve those errors.

Reference a project or assembly that targets a different version of .NET

You can create applications that reference projects or assemblies that target different versions of .NET. For example, you can create an application that targets .NET 6 but references an assembly that targets .NET Core 3.1. However, you can't set a reference in a project that targets an earlier version of .NET to a project or assembly that targets .NET 6. Here's an example of the error you might see in this case:

error NU1201: Project ClassLibrary-NET6 is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project ClassLibrary-NET6 supports: net6.0 (.NETCoreApp,Version=v6.0)
2>Done building project "ClassLibrary-NET31.csproj" -- FAILED.

To resolve the error, make sure that your application targets a .NET version that's compatible with the version that's targeted by the projects or assemblies that your application references.

Re-target a project to a different version of .NET

If you change the target version of .NET for your application, Visual Studio changes some of the references, but you might have to update some references manually. For example, one of the previously mentioned errors might occur if you change an application to target .NET Core 3.1 and that application has references, resources, or settings that rely on .NET 6.

Update references in app.config

To work around application settings in .NET Framework applications, follow these steps:

  1. Open Solution Explorer.
  2. Select Show All Files, and then edit the app.config file in the XML editor of Visual Studio.
  3. Change the version in the settings to match the appropriate version of .NET. For example, you can change the version setting from 4.0.0.0 to 2.0.0.0.

Similarly, for an application that has added resources, follow these steps:

  1. Open Solution Explorer.
  2. Select Show All Files.
  3. Expand My Project (Visual Basic) or Properties (C#), and then edit the Resources.resx file in the XML editor of Visual Studio.
  4. Change the version setting from 4.0.0.0 to 2.0.0.0.

Update resources

If your application has resources such as icons or bitmaps or settings such as data connection strings, you can also resolve the error by removing all the items on the Settings page of the Project Designer and then readding the required settings.

You re-target a project to a different version of .NET and references aren't resolved

If you re-target a project to a different version of .NET, your references may not resolve properly in some cases. Explicit fully qualified references to assemblies often cause this issue, but you can resolve it by removing the references that don't resolve and then adding them back to the project. As an alternative, you can edit the project file to replace the references. First, remove references of the following form:

<Reference Include="System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" />

Then, replace them with the simple form:

<Reference Include="System.ServiceModel" />

Note: After you close and reopen your project, you should also rebuild it to ensure that all references resolve correctly.

References

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 10/07/2022
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。