我是Azure开发新手,有以下功能
[FunctionName("BindingTrigger")]
public async Task<IActionResult> BindingTrigger(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log,
[Blob("sample-blob/test.txt", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream outputBlob,
[CosmosDB("azurefunctions", "FileContents", Connection = "CosmosConnStr", CreateIfNotExists = true)] IAsyncCollector<FileContents> documents)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
await documents.AddAsync(new FileContents
{
PartitionKey = Guid.NewGuid().ToString(),
RowKey = "1",
Name = "test.txt",
Contents = outputBlob.Length.ToString()
});
}
catch (Exception ex)
{
log.LogInformation(ex.Message);
}
return new OkObjectResult($"Size of file: {outputBlob.Length}");
}
当我执行时,它会记录“C# HTTP 触发器函数处理了一个请求。”,但之后没有任何反应。无异常,30分钟后函数超时。
我正在尝试在 docker 中运行 CosmosDB 模拟器。尝试使用 Azure CosmosDB 实例,得到相同的结果。还想知道我的系统是否有问题,我尝试了另一个系统,结果相同。知道可能出了什么问题吗?
确保根据您的 Azure CosmosDB 架构设置文件内容中的正确数据,并根据 Matias Quaranta 的评论为您的 AzureWebJobsStorage 和 CosmosConnStr 使用有效连接字符串。
我通过引用此MS Document创建了我的CosmosDB数据库和容器。
我的功能代码:-
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
public class FileContents
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("category")]
public string Category { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("isComplete")]
public bool IsComplete { get; set; }
}
public static class MyFunctions
{
[FunctionName("BindingTrigger")]
public static async Task<IActionResult> BindingTrigger(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log,
[Blob("sample-blob/test.txt", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream outputBlob,
[CosmosDB(
databaseName: "ToDoList",
containerName: "Items", // Replace with your container name
Connection = "CosmosConnStr",
CreateIfNotExists = true)] IAsyncCollector<FileContents> documents)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
if (outputBlob != null)
{
outputBlob.Position = 0; // Reset stream position
// Deserialize the JSON document structure based on your Cosmos DB schema
// Modify this according to the actual data you want to insert
var fileContent = new FileContents
{
Id = "1",
Category = "personal",
Name = "groceries",
Description = "Pick up apples and strawberries.",
IsComplete = false
};
await documents.AddAsync(fileContent);
return new OkObjectResult("Data inserted into Cosmos DB");
}
else
{
log.LogError("Blob is null or inaccessible.");
return new BadRequestObjectResult("Blob is null or inaccessible.");
}
}
catch (Exception ex)
{
log.LogError(ex, "Error processing the request");
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
}
我的local.settings.json:-
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=siliconstrg89;AccountKey=xxxxxxxxxx+AStEQBMoA==;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"CosmosConnStr": "AccountEndpoint=https://silicon-cosmos.documents.azure.com:443/;AccountKey=xxxxxxxxxxAoACDb4rmfgA==;"
}
}
输出:-
根据您的架构,您的代码应如下所示:-
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;
public class FileContents
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Name { get; set; }
public string Contents { get; set; }
}
public static class MyFunctions
{
[FunctionName("BindingTrigger")]
public static async Task<IActionResult> BindingTrigger(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log,
[Blob("sample-blob/test.txt", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream outputBlob,
[CosmosDB("azurefunctions", "FileContents", Connection = "CosmosConnStr", CreateIfNotExists = true)] IAsyncCollector<FileContents> documents)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
if (outputBlob != null)
{
outputBlob.Position = 0; // Reset stream position
await documents.AddAsync(new FileContents
{
PartitionKey = Guid.NewGuid().ToString(),
RowKey = "1",
Name = "test.txt",
Contents = outputBlob.Length.ToString()
});
return new OkObjectResult($"Size of file: {outputBlob.Length}");
}
else
{
log.LogError("Blob is null or inaccessible.");
return new BadRequestObjectResult("Blob is null or inaccessible.");
}
}
catch (Exception ex)
{
log.LogError(ex, "Error processing the request");
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
}