【问题标题】:AWS CDK C# Namespace could not be found IngressProperty找不到 AWS CDK C# 命名空间 IngressProperty
【发布时间】:2022-01-22 02:45:25
【问题描述】:

我刚刚从此处“https://docs.aws.amazon.com/cdk/api/v1/dotnet/api/Amazon.CDK.AWS.EC2.CfnSecurityGroup. html”

但是当我尝试合成/部署时,我收到了这个错误

错误 CS0246:找不到类型或命名空间名称“IngressProperty”(您是否缺少 using 指令或程序集引用?

似乎文档本身不可靠或不工作。

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

namespace Sg
{
    public class SgStack : Stack
    {
        internal SgStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            var cfnSecurityGroup = new CfnSecurityGroup(this, "MyCfnSecurityGroup", new CfnSecurityGroupProps {
                GroupDescription = "test",

                GroupName = "test",
                SecurityGroupIngress = new [] { new IngressProperty  {
                    IpProtocol = "tcp",
                    CidrIp = "0.0.0.0/0",
                    Description = "description",
                    FromPort = 22,
                    ToPort = 22
                } },
                VpcId = "vpc-12345"
            });
        }
    }
}

【问题讨论】:

  • 您在参考 v1 文档时可能正在使用 v2 API DLL。
  • 我刚刚尝试了 v1 和 v2 但得到了同样的错误。
  • 进行了清理以测试 v1 并收到此错误命名空间“Amazon.CDK”中不存在类型或命名空间名称“AWS”

标签: c# amazon-web-services aws-cdk aws-security-group


【解决方案1】:

IngressPropertyCfnSecurityGroup 类的一部分,因此要使其正常工作,您必须像这样更改代码:

var cfnSecurityGroup = new CfnSecurityGroup(this, "MyCfnSecurityGroup", new
    CfnSecurityGroupProps
{
    GroupDescription = "test",
    GroupName = "test",
    SecurityGroupIngress = new[] {

            // The change
            new CfnSecurityGroup.IngressProperty  {

            IpProtocol = "tcp",
            CidrIp = "0.0.0.0/0",
            Description = "description",
            FromPort = 22,
            ToPort = 22
        } 
    },
    VpcId = "vpc-12345"
});

或添加using static Amazon.CDK.AWS.EC2.CfnSecurityGroup;

【讨论】:

  • 成功了!谢谢你
猜你喜欢
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 2011-06-28
  • 2010-12-23
  • 2015-06-20
相关资源
最近更新 更多