我一直在寻找一种通过浏览器内代码编辑器使用Azure Functions删除Cosmos数据库中的项目的技术。我不想在VS上使用本地开发的代码有多种原因。
我正在使用的代码在这里可用,我正在使用带有CosmosDB输入和输出绑定的HttpTrigger。它们的名称很明显(inputDocument,outputDocument)。
这段代码非常适合从db中读取项目并编写新文档,但是我希望能够删除单个项目。我正在制作一个游戏“拍卖行”系统,并且要“购买”一个物品,我需要将其从数据库中删除。
我现在搜索的地方很少,很多人说使用DocumentDB,但是我不认为浏览器编辑器可以支持此功能,我无法识别能够使用此功能的正确Azure库。如果我错过了一步,请告诉我。当我添加时失败
#r "Microsoft.Azure.Documents.Client"
using Microsoft.Azure.Documents.Client;
编辑在与Azure支持人员交谈之后,我发现v3使用Documents.Core,而不是Documents.Client。如果有人可以提供Documents.Core的文档,我将非常感激!
谢谢。代码复制到下面;
#r "Newtonsoft.Json"
using System.Net;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public class AuctionItem
{
[JsonProperty("itemID")]
public string itemID { get; set; }
[JsonProperty("price")]
public string Price { get; set; }
[JsonProperty("amount")]
public string Amount { get; set; }
}
public static IActionResult Run(HttpRequest req, out object outputDocument,
IEnumerable<AuctionItem> inputDocument, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string price = req.Query["price"];
string amount = req.Query["amount"];
string command = req.Query["command"];
outputDocument = null;
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(price) && !string.IsNullOrEmpty(amount))
{
string responseMessage = "{\"Message\":\"Success\",\n\"Data\": [" + "\n";
if (!string.IsNullOrEmpty(command)) {
if (command == "1") {
foreach (var item in inputDocument) {
responseMessage += "{\n\t\"itemID\":\"" + item.itemID + "\"," + "\n";
responseMessage += "\t\"price\":\"" + item.Price + "\"," + "\n";
responseMessage += "\t\"amount\":\"" + item.Amount + "\"}," + "\n";
}
} else if (command == "2") {
var item = inputDocument.Where(x => x.itemID == name).FirstOrDefault();
if (item != null) {
inputDocument = inputDocument.Where(x => x != item);
}
} else if (command == "3") {
responseMessage += "{\n\t\"itemID\":\"" + name + "\",\n";
responseMessage += "\t\"price\":\"" + price + "\",\n";
responseMessage += "\t\"amount\":\"" + amount + "\"}\n";
log.LogInformation(responseMessage);
outputDocument = new {
itemID = name,
price = price,
amount = amount
};
}
}
responseMessage += "]}";
return new OkObjectResult(responseMessage);
} else {
outputDocument = null;
return new BadRequestResult();
}
}
您需要在http触发器中创建一个名为“ function.proj”的文件。
内容:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.12.0" />
</ItemGroup>
</Project>
创建步骤:
下面是我的测试代码,它可以正常工作:
#r "Newtonsoft.Json"
#r "Microsoft.Azure.DocumentDB.Core"
using System.Net;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Threading.Tasks;
private static DocumentClient client;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
client = new DocumentClient(new Uri("endpointUrl"), "authorizationKey");
ResourceResponse<Document> response = await client.DeleteDocumentAsync(
UriFactory.CreateDocumentUri("databaseName", "collectionName", "id"),
new RequestOptions { PartitionKey = new PartitionKey("partition-key-value") });
log.LogInformation("Request charge of delete operation: {0}", response.RequestCharge);
log.LogInformation("StatusCode of operation: {0}", response.StatusCode);
return new OkResult();
}
顺便说一下,您可以在此处找到文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句