question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Azure Function Isolated] Cosmos change feed listener tries to start before CreateContainerIfNotExistsAsync

See original GitHub issue

For isolated azure functions, I believe that the cosmos change feed tries to listen before containers have been created. Either that or I’m doing something wrong?

Thanks

Setup:

Program.cs

var host = new HostBuilder()
...
.Build()

var client = host.Services.GetRequiredService<CosmosClient>();
var databaseResponse = await client.CreateDatabaseIfNotExistsAsync(Configuration["DbName"]);
var database = databaseResponse.Database;

await database.CreateContainerIfNotExistsAsync("container", "/id");

host.Run();
Function.cs

[Function("DoSomething")]
public async Task Run(
    [CosmosDBTrigger("%DbName%", "container", 
    ConnectionStringSetting = "CosmosDbConnectionString", 
    LeaseCollectionName = "leases", 
    CreateLeaseCollectionIfNotExists = true)] IReadOnlyList<Entity> input)
{
} 

Error message on local machine:

The listener for function ‘DoSomething’ was unable to start. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Either the source collection ‘container’ (in database ‘db’) or the lease collection ‘leases’ (in database ‘db’) does not exist. Both collections must exist before the listener starts. To automatically create the lease collection, set ‘CreateLeaseCollectionIfNotExists’ to ‘true’

Versions

Microsoft.Azure.Functions.Worker.Sdk 1.3.0 Microsoft.Azure.Functions.Worker.Extensions.Http 3.0.13 Microsoft.Azure.Cosmos 3.26.1 Microsoft.Azure.Functions.Worker.Extensions.CosmosDB 3.0.9

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ealsurcommented, Apr 20, 2022

Closing as out of the scope of this repo. https://github.com/Azure/azure-functions-host might be a good candidate to ask how you can execute code before the runtime loads the declared Functions.

0reactions
ealsurcommented, Apr 20, 2022

@andersson09 - Correct, but that error message is just the Extension saying that it cannot find the container, it has nothing to do with your intent to create the container before the Function is initialized. The Extensions start when the Host/Runtime tell them to start, they do not have control over when that happens.

What is happening is that you have some code that you want to run before the Host signals the Extensions to start, but you are not able to put that code in a place where the Host is executing it before. The Host is starting the Extensions either at the same time or before your code.

In order to know where to put your code to achieve what you want, you need guidance or at least someone that knows how the Function Host works. This is not the repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Azure Function Isolated] Cosmos change feed listener ...
For isolated azure functions, I believe that the cosmos change feed tries to listen before containers have been created.
Read more >
Troubleshoot issues with the Azure Functions trigger for ...
This article discusses common issues, workarounds, and diagnostic steps when you're using the Azure Functions trigger for Azure Cosmos DB.
Read more >
How to use Azure Cosmos DB change feed with ...
In this article. Requirements; Create your Azure Functions trigger for Azure Cosmos DB; Run your trigger locally; Next steps.
Read more >
Azure Function for Cosmos DB change feed feed won't run
To fix, open your .csproj file and remove the Microsoft.Azure.WebJobs.Extensions.CosmosDB package. Add a new entry for Microsoft.Azure.Functions ...
Read more >
Azure Cosmos DB Change Feed
The two main options for consuming the Cosmos DB change feed are Azure Functions and the Change Feed Processor library. We'll start with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found