Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function binding attributes - Database name setting - VS2017 preview tooling

I'm using the new tooling for Azure Functions projects in VS2017 Preview and have ported some functions from the Azure portal to the new projects.

I'm binding to an Azure Document DB - it's working fine, but when I use the DocumentDB attribute I have to supply the database name as the first parameter.

In my case it's the DEV database for now .. but of course there will be other environments - is there a way I can pick up the database name via an application setting for the function app?

[FunctionName("TimerTriggeredFunction")]
public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, TraceWriter log,
                       [DocumentDB("my-db-DEV", "MyCollection")]  IEnumerable<dynamic> inputDocuments)
like image 632
Garth Mason Avatar asked Mar 10 '23 02:03

Garth Mason


2 Answers

Yes, you can use app settings for these values by using the %% auto resolve syntax for app settings. For example, if you've added app settings named DB_DEV and COLLECTION_DEV (either in the portal, or when running on your local box), you can reference those in your attribute like so:

[DocumentDB("%DB_DEV%", "%COLLECTION_DEV%")]

Those app settings will then be resolved at runtime. The "Resolving App Settings" section in our documentation here explains this in more detail.

like image 96
mathewc Avatar answered Mar 11 '23 15:03

mathewc


I'd overlooked the fact that the Azure Cosmos DB connection string can be specified via the local.settings.json file for my Azure function project.

It's in here that I'd provide the appropriate connection string for the environment I'm targetting. Then I can use consistently named Databases and Collections within those accounts, meaning the function signature is correct in all environments.

[DocumentDB("my-db", "MyCollection")] IEnumerable<dynamic> inputDocuments

And provide the connection string in local.settings.json under the default key, AzureWebJobsDocumentDBConnectionString

like image 36
Garth Mason Avatar answered Mar 11 '23 15:03

Garth Mason