C#运行报错CS8803,Azure Cosmos DB示例代码如何正确结构化?
解决CS8803错误:顶级语句需优先于类型声明
错误CS8803的核心规则是:C#顶级语句(无类/方法包裹的直接执行代码)必须放在所有命名空间、类、record等类型声明的前面。你当前代码把Product这个record类型定义插在了顶级执行代码中间,违反了这个编译规则。
下面提供两种修正方案:
方案1:调整类型声明位置(推荐,保留顶级语句简洁风格)
把Product record移到using语句之后、所有执行代码之前即可:
// See https://aka.ms/new-console-template for more information using Microsoft.Azure.Cosmos; using Azure.Identity; // C# record representing an item in the container public record Product( string id, string categoryId, string categoryName, string name, int quantity, bool sale ); // New instance of CosmosClient class using CosmosClient client = new( accountEndpoint: Environment.GetEnvironmentVariable("COSMOS_ENDPOINT"), tokenCredential: new DefaultAzureCredential() ); // 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 already 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}"); } }
方案2:改用传统控制台程序结构(包裹在类和Main方法中)
如果更习惯传统C#控制台的类+方法结构,可以把所有逻辑放到Program类的异步Main方法里:
using Microsoft.Azure.Cosmos; using Azure.Identity; // C# record representing an item in the container public record Product( string id, string categoryId, string categoryName, string name, int quantity, bool sale ); class Program { static async Task Main(string[] args) { // New instance of CosmosClient class using CosmosClient client = new( accountEndpoint: Environment.GetEnvironmentVariable("COSMOS_ENDPOINT"), tokenCredential: new DefaultAzureCredential() ); // 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 already 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更贴合.NET 6+的极简风格,只需调整代码位置;方案2适合熟悉传统C#结构的开发者。
内容的提问来源于stack exchange,提问作者Manu Chadha
相关产品推荐
相关产品推荐

