【问题标题】:HTTPTrigger Azure Function with CosmosDb input is not working after deployment具有 CosmosDb 输入的 HTTPTrigger Azure 函数在部署后不起作用
【发布时间】: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 用作PartitionKeyID 是否正确?
  • 它在 local.settings.json 中有,它在本地运行良好。我需要添加到 host.json 吗?
  • local.settings.json 部署函数时文件未发布,您需要在资源上添加设置,通过 ARM 部署或转到 Azure 门户并在函数应用上设置配置本身。

标签: azure azure-functions azure-cosmosdb


【解决方案1】:

我得到了问题,这里发生了什么。在发布函数之前,我必须使用 Azure Function:Add New Settings... 命令手动将 CosmosDBConnection 添加到 Azure。

我是新手,我不知道这就是 app_settings 需要添加的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2015-05-03
    • 2023-03-25
    相关资源
    最近更新 更多