代码之家  ›  专栏  ›  技术社区  ›  Ela Buwa

无法获取布尔数据类型

  •  0
  • Ela Buwa  · 技术社区  · 2 年前

    C#中的新手。 尝试使用下面的代码来检查S3中是否存在某个bucket。

    using System;
    using Amazon.S3;
    using Amazon.S3.Util;
    
    namespace CRO
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", "123");
                Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", "456");
                Environment.SetEnvironmentVariable("AWS_REGION", "ap-southeast-2");
    
                Console.WriteLine(DoesBucketExist("randombucket").GetType());
                Console.WriteLine("Done");
            }
    
    
            public static async Task<bool> DoesBucketExist(string bucketName)
            {
                using (var s3Client = new AmazonS3Client(Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID"), Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY")))
                {
                    DateTime currentDateTime = DateTime.Now;
                    Console.WriteLine("Current date and time: " + currentDateTime);
    
                    return await AmazonS3Util.DoesS3BucketExistV2Async(s3Client, bucketName);
                }
            }
        }
    }
    

    我希望返回一个布尔值(true或false)。 但当我检查下面的数据类型时,我得到的不是“布尔值”。

    System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1+AsyncStateMachineBox 1[System.Boolean,CRO.Program+d_1]

    对于C#世界来说是非常新鲜的。只是想知道我做错了什么。

    尝试使用.net核心(如果有帮助的话)。

    1 回复  |  直到 2 年前
        1
  •  1
  •   Yong Shun    2 年前

    要从async方法获取值,您应该使用 await 。此外,您应该更改 Main 方法转换为异步方法。

    static async Task Main(string[] args)
    {
        ...
    
        Console.WriteLine(await DoesBucketExist("randombucket"));
    }
    

    Object.GetType() 返回实例的类型名称。

    推荐文章