将项目从 .NET Framework 4.5 迁移到 .NET Standard 2.0

问题描述 投票:0回答:1

我有一个 .NET Framework 4.5 项目,我想将其迁移到 .NET Standard 2.0。

当我尝试打开此项目时,会弹出一个窗口,告诉我项目正在针对 .NET Framework 4.5,并且不再受支持。它还要求我将目标更新为.NET Framework 4.8,或者直接从浏览器下载.NET Framework4.5。否则我无法加载项目。

但我不想做其中任何一个。

在网上搜索我发现你只需要更新.csproj,选择“netstandard2.0”作为TargetFramework并使用NuGet更新包即可。

所以我的 .csproj 是这样的:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{756DBCF8-3349-4CAE-B58C-47B7998BE3FF}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Wke.Cms</RootNamespace>
    <AssemblyName>Wke.Cms</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>
...
More below
...

我只是做了这个小更改,在我的文件资源管理器上打开文件:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{756DBCF8-3349-4CAE-B58C-47B7998BE3FF}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Wke.Cms</RootNamespace>
    <AssemblyName>Wke.Cms</AssemblyName>
    <TargetFramework>netstandard2.0</TargetFramework>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>

将“TargetFrameworkVersion”更改为“TargetFramework”并放置“netstandard2.0”

现在我可以加载项目,但如果我尝试打开项目属性,则会收到以下错误:

12/04/2024 10:42:18
Recoverable
System.ArgumentException: Expected 1 values for property Build::DefineConstants, but got 0. The property page may not be defined for all configurations.
Nombre del parámetro: values
   en Microsoft.Requires.Argument(Boolean condition, String parameterName, ValidationInterpolatedStringHandler& message)
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.Property.Update(ImmutableArray`1 values)
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.Property..ctor(PropertyMetadata metadata, ImmutableArray`1 values, PropertyContext context, ImmutableHashSet`1 varyByDimensions)
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.PropertyContextFactoryBase.ToProperty(IUIPropertySnapshot property, Page page, Category category, Int32 order, PropertyContext propertyContext, IPropertyEditorRegistry propertyEditorRegistry)
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.ProjectPropertyDataAccess.Observer.<HandleDataAsync>g__CreateProperties|14_5(<>c__DisplayClass14_0& )
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.ProjectPropertyDataAccess.Observer.<HandleDataAsync>g__ProcessInitialData|14_1(<>c__DisplayClass14_0& )

尝试用谷歌搜索并找到this,但对我不起作用。

我在这里缺少什么?我需要做更多改变吗?非常欢迎任何帮助。

migration .net-framework-version .net-standard-2.0 target-framework
1个回答
0
投票

将 csproj 升级到新语法可能是最简单的。您的 csproj 现在仍然是旧风格,这使事情变得复杂。尝试更新它并再次重新添加您的核心内容和参考文献,您应该可以开始了。

您的 csproj 应该如下所示:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <RootNamespace>Wke.Cms</RootNamespace>
    <AssemblyName>Wke.Cms</AssemblyName>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

</Project>
© www.soinside.com 2019 - 2024. All rights reserved.