0
votes

I have created below HttpTrigger function which takes CosmosDb item as input. This code works perfectly fine on when run locally. I am using Visual Studio Code to create function and deploying to Azure after successfully ran locally. But it doesn't work on Azure, it doesn't give error or it seems it not getting any response.

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);
    }
}

While publishing, in output I see below message:

HttpTriggerCSharp1: https://nps-func.azurewebsites.net/api/HttpTriggerCSharp1
TestMessage: https://nps-func.azurewebsites.net/api/TestMessage 5:33:02 PM nps-func: WARNING: Some http trigger urls cannot be displayed in the output window because they require an authentication token.

It is not showing This above function in response. enter image description here

Update: Full code can be found on Git

1
Is there a (valid) setting CosmosDBConnection in the Azure Functions Configuration?rickvdbosch
Are you sure of all the values for the CosmosDB binding? Is there a valid setting CosmosDBConnection in the Azure Function Configuration? Is it correct you're using id as both PartitionKey and ID?rickvdbosch
it has there in local.settings.json, it is working fine locally. Do I need to add to host.json?Nps
local.settings.json file does not get published when the Function is deployed, you need to add the settings on the resource, either by ARM deployment or going to the Azure Portal and setting the Configuration on the Function App itself.Matias Quaranta

1 Answers

0
votes

I got the issue, what is happening here. I had to manually add CosmosDBConnection to Azure with Azure Function:Add New Settings... command before I publish the function.

I am new to function, I was not aware this is how app_settings need to be added.