这已经在这里得到了回答。但是,这是使用门户的 func 代码编辑器。
您可以改为将 的实例DocumentClient
直接绑定到您的 HTTP 触发器函数:
[FunctionName("Function1")]
public async Task<IActionResult> DeleteProduct(
[HttpTrigger(
authLevel: AuthorizationLevel.Anonymous,
methods: "delete",
Route = "products/{productId}")] HttpRequest request,
[CosmosDB(
databaseName: "my-database",
collectionName: "products")] DocumentClient client,
Guid productId)
{
Uri productUri = UriFactory.CreateDocumentUri("my-database", "products", productId.ToString());
PartitionKey partitionKey = new PartitionKey("my-partition-key");
RequestOptions requestOptions = new RequestOptions { PartitionKey = partitionKey };
ResourceResponse<Document> response = await client.DeleteDocumentAsync(productUri, requestOptions);
// Use response for something or not...
return new NoContentResult();
}
该示例要求您使用键“CosmosDBConnection”配置 Cosmos DB 连接字符串。这必须local.settings.json
为本地开发设置:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"CosmosDBConnection": "my-cosmos-db-connection"
}
}
并在门户上的函数应用的应用程序设置中。
您需要以下软件包才能使其工作:
Microsoft.Azure.Cosmos
Microsoft.Azure.WebJobs.Extensions.CosmosDB