AWS CDK:检测到依赖约束之外的包版本

AWS CDK: Detected package version outside of dependency constraint

我目前正在研究 AWS moderization workshop。当我添加 Amazon.CDK.AWS.EC2 nuget 库并构建应用程序时,出现以下错误: 该应用程序使用最新的亚马逊。 CDK.Lib、构造、Amazon.CDK.AWS.来自 Nuget 的 EC2 库。我还尝试使用 https://docs.aws.amazon.com/cdk/v2/guide/troubleshooting.html#troubleshooting_toolkit 中记录的故障排除步骤 运行 命令 npm update -g aws-cdk 但这没有帮助。

我错过了什么?

构建错误:

Rebuild started...
1>------ Rebuild All started: Project: NorthwindCdk, Configuration: Debug Any CPU ------
Restored C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj (in 54 ms).
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.AWS.EC2 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.AWS.CloudWatch 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.AWS.IAM 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.AWS.KMS 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.AWS.Logs 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.AWS.S3.Assets 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.AWS.S3 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.AWS.SSM 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.Assets 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdk.csproj : warning NU1608: Detected package version outside of dependency constraint: Amazon.CDK.AWS.Events 1.137.0 requires Constructs (>= 3.3.69 && < 4.0.0) but version Constructs 10.0.22 was resolved.
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdkStack.cs(7,38,7,43): error CS0433: The type 'Stack' exists in both 'Amazon.CDK.Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Amazon.CDK, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdkStack.cs(9,36,9,45): error CS0104: 'Construct' is an ambiguous reference between 'Amazon.CDK.Construct' and 'Constructs.Construct'
1>C:\Users146\northwind-cdk\src\NorthwindCdk\NorthwindCdkStack.cs(9,64,9,75): error CS0433: The type 'IStackProps' exists in both 'Amazon.CDK.Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Amazon.CDK, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
1>Done building project "NorthwindCdk.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

NorthwindCdk.cs:

using Amazon.CDK;
using Constructs;
using Amazon.CDK.AWS.EC2;

namespace NorthwindCdk
{
    public class NorthwindCdkStack : Stack
  {
        internal NorthwindCdkStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
      var vpc = new Vpc(this, "LabVpc", new VpcProps
      {
        MaxAzs = 2
      });
    }
    }
}

Amazon.CDK.AWS.EC2 nuget 库似乎已被弃用。我卸载了库和应用程序构建并正确部署。

这是因为您同时安装了 CDK v1 和 v2。在 CDKv1 中,每个模块都是它自己的包 - 错误中包含的包都是 v1(例如 Amazon.CDK.AWS.EC2Amazon.CDK.AWS.Cloudwatch 等)。对于 CDKv2,所有这些模块都包含在 Amazon.CDK.Lib 软件包中,您也已安装该软件包。

需要说明的是,CDKv1 并未弃用,两者均受支持。但不能同时使用。