代码之家  ›  专栏  ›  技术社区  ›  Manu Chadha

错误CS8803:顶级语句必须位于命名空间和类型声明之前

  •  0
  • Manu Chadha  · 技术社区  · 2 年前

    我不知道怎么写 c# 密码我正试图在这里运行一个示例代码- https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/quickstart-dotnet?tabs=azure-portal%2Cwindows%2Cpasswordless%2Csign-in-azure-cli#create-account

    这是我复制的代码 powershell 。当我这样做的时候 dotnet run ,我出错了

    /home/manu/Program.cs(24,1): error CS8803: Top-level statements must precede namespace and type declarations. [/home/manu/manu.csproj]
    
    The build failed. Fix the build errors and run again.
    

    密码

    // See https://aka.ms/new-console-template for more information
    using Microsoft.Azure.Cosmos;
    using Azure.Identity;
    
    // New instance of CosmosClient class
    using CosmosClient client = new(
        accountEndpoint: Environment.GetEnvironmentVariable("COSMOS_ENDPOINT"),
        tokenCredential: new DefaultAzureCredential()
    );
    
    
    // C# record representing an item in the container
    public record Product(
        string id,
        string categoryId,
        string categoryName,
        string name,
        int quantity,
        bool sale
    );
    
    
    // Database reference with creation if it does not already exist
    Database database = client.GetDatabase(id: "cosmicworks");
    
    Console.WriteLine("Hello, World!");
    
    Console.WriteLine($"New database:\t{database.Id}");
    
    
    // Container reference with creation if it does not alredy exist
    Container container = database.GetContainer(id: "products");
    
    Console.WriteLine($"New container:\t{container.Id}");
    
    
    // Create new object and upsert (create or replace) to container
    Product newItem = new(
        id: "70b63682-b93a-4c77-aad2-65501347265f",
        categoryId: "61dba35b-4f02-45c5-b648-c6badc0cbd79",
        categoryName: "gear-surf-surfboards",
        name: "Yamba Surfboard",
        quantity: 12,
        sale: false
    );
    
    Product createdItem = await container.CreateItemAsync<Product>(
        item: newItem
    );
    
    Console.WriteLine($"Created item:\t{createdItem.id}\t[{createdItem.categoryName}]");
    
    // Point read item from container using the id and partitionKey
    Product readItem = await container.ReadItemAsync<Product>(
        id: "70b63682-b93a-4c77-aad2-65501347265f",
        partitionKey: new PartitionKey("61dba35b-4f02-45c5-b648-c6badc0cbd79")
    );
    
    // Create query using a SQL string and parameters
    var query = new QueryDefinition(
        query: "SELECT * FROM products p WHERE p.categoryId = @categoryId"
    )
        .WithParameter("@categoryId", "61dba35b-4f02-45c5-b648-c6badc0cbd79");
    
    using FeedIterator<Product> feed = container.GetItemQueryIterator<Product>(
        queryDefinition: query
    );
    
    while (feed.HasMoreResults)
    {
        FeedResponse<Product> response = await feed.ReadNextAsync();
        foreach (Product item in response)
        {
            Console.WriteLine($"Found item:\t{item.name}");
        }
    }
    

    构建代码的正确方法是什么?

    1 回复  |  直到 2 年前
        1
  •  5
  •   Jon Skeet    2 年前

    您当前的代码结构是:

    • using 指令
    • 顶级语句(声明 client 变量)
    • 记录类型声明
    • 更多顶级声明

    正如错误消息所说,所有顶级语句都必须出现 之前 任何类型声明。

    有两种解决方案:

    • 停止使用顶级语句:创建 Program 使用的类 Main 方法,并将代码放入其中,然后声明 Product 单独记录
    • 只要移动 产品 记录到所有顶级语句下面,或记录到其他文件中