所以我遇到的问题是我不能再次使用“ID”,它一直给我的 CosmosDBOutput 错误,我不确定为什么会这样,但如果有人可以帮助修复它,我需要帮助。
这是我到目前为止的代码,我正在使用 C# 使用 .Net 6 LTS
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Functions.Worker;
using System.Net.Http;
using System.Text;
namespace Company.Function
{
public static class GetResumeCounter
{
[FunctionName("GetResumeCounter")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[CosmosDBInput(databaseName:"AzureResume", containerName: "Counter", Connection = "AzureResumeConnectionString", Id = "1", PartitionKey = "1")] Counter counter,
[CosmosDBOutput(databaseName:"AzureResume", containerName: "Counter", Connection = "AzureResumeConnectionString", Id = "1", PartitionKey = "1")] out Counter updatedcounter,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
updatedcounter = counter;
updatedcounter.Count += 1;
var jsonToRetun = JsonConvert.SerializeObject(counter);
return new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent(jsonToRetun, Encoding.UTF8, "application/json")
};
}
}
}
我试图使用 dotnet build 来构建它,但我不断收到错误: 找不到类型或命名空间名称“Id”(您是否缺少 using 指令或程序集引用?)
对于我的 CosmosDBOutput 请帮忙
谢谢@David Makogon
正如 David 提到的
Id
属性不存在于 [CosmosDbOutput]
您可以使用
CosmosClient
更新现有数据,因为 [CosmosDbOutput]
会覆盖数据。
这对我有用。
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Cosmos;
using System.Net;
using Microsoft.AspNetCore.Http;
namespace FunctionApp13
{
public static class Function
{
private static readonly string cosmosEndpoint = "https://resumedbcount.documents.azure.com:443/";
private static readonly string cosmosKey = "xxxxxxxxxxxxxxxxxxxxxxx";
private static readonly string databaseName = "Resume";
private static readonly string containerName = "Counter";
private static readonly CosmosClient cosmosClient = new CosmosClient(cosmosEndpoint, cosmosKey);
private static readonly Container container = cosmosClient.GetContainer(databaseName, containerName);
[FunctionName("Function")]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Get the document by ID
var counter = await GetCounterById("1", "1");
if (counter == null)
{
counter = new Counter { id = "1", PartitionKey = "1", Count = 0 };
}
// Update the document
counter.Count++;
// Replace the document in Cosmos DB
var response = await container.ReplaceItemAsync(counter, counter.id, new PartitionKey(counter.PartitionKey));
return new OkObjectResult("replaced successfully");
}
private static async Task<Counter> GetCounterById(string id, string partitionKey)
{
try
{
var response = await container.ReadItemAsync<Counter>(id, new PartitionKey(partitionKey));
return response.Resource;
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
return null;
}
}
}
public class Counter
{
public string id { get; set; }
public string PartitionKey { get; set; }
public int Count { get; set; }
}
}
{
"id": "1",
"PartitionKey": "1",
"Count": 11
}
OUTPUT
:{
"id": "1",
"PartitionKey": "1",
"Count": 12
}