【发布时间】:2020-09-21 12:44:41
【问题描述】:
我在 HttpTrigger 函数下方创建了将 CosmosDb 项作为输入的函数。此代码在本地运行时运行良好。在本地成功运行后,我正在使用 Visual Studio Code 创建函数并部署到 Azure。但它在 Azure 上不起作用,它没有给出错误或似乎没有得到任何响应。
public static class HttpTriggerWithCosmosDb
{
[FunctionName("HttpTriggerWithCosmosDb")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "HttpTriggerWithCosmosDb/{id}")] HttpRequest req,
[CosmosDB(
databaseName : "func-io-learn-db",
collectionName : "Bookmarks",
ConnectionStringSetting = "CosmosDBConnection",
Id = "{id}",
PartitionKey = "{id}"
)] BookMarkItem item,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = item.Url;
Console.Write( item.Id);
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
发布时,在输出中我看到以下消息:
HttpTriggerCSharp1: https://nps-func.azurewebsites.net/api/HttpTriggerCSharp1
测试消息:https://nps-func.azurewebsites.net/api/TestMessage 下午 5:33:02 nps-func:警告:某些 http 触发 url 不能 显示在输出窗口中,因为它们需要身份验证 令牌。
更新: 完整代码见Git
【问题讨论】:
-
Azure Functions 配置中是否有(有效的)设置
CosmosDBConnection? -
您确定
CosmosDB绑定的所有值吗? Azure 功能配置中是否有有效的设置CosmosDBConnection?您将id用作PartitionKey和ID是否正确? -
它在 local.settings.json 中有,它在本地运行良好。我需要添加到 host.json 吗?
-
local.settings.json部署函数时文件未发布,您需要在资源上添加设置,通过 ARM 部署或转到 Azure 门户并在函数应用上设置配置本身。
标签: azure azure-functions azure-cosmosdb