Azure Function Binding Attributes - Setting Database Name - VS2017 Preview Tool
I am using the new Azure Functions project tool in VS2017 Preview and have ported some functionality from the Azure portal to new projects.
I am binding to the Azure Document DB - it works fine, but when I use the attribute DocumentDB
, I have to provide the database name as the first parameter.
In my case it is a DEV database at the moment .. but of course there will be other environments as well - is there a way to get the name of the database through the app setup 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)
source to share
Yes, you can use app preferences for these values ββusing the %%
auto-resolve syntax for app preferences. For example, if you added app settings named DB_DEV
and COLLECTION_DEV
(either in the portal or when launched in a local box), you can link to the ones in your attribute like:
[DocumentDB("%DB_DEV%", "%COLLECTION_DEV%")]
These application settings will be resolved at runtime. The "Troubleshooting Application Settings" section of our documentation here explains this in more detail.
source to share
I would overlook the fact that the Azure Cosmos DB connection string can be specified via a file local.settings.json
for my Azure Function project.
Here, I have to provide the appropriate connection string for the environment I am targeting. I can then use the sequentially named databases and collections on those accounts, which means the function signature is correct across all environments.
[DocumentDB("my-db", "MyCollection")] IEnumerable<dynamic> inputDocuments
And enter the connection string in the local.settings.json
default turnkey,AzureWebJobsDocumentDBConnectionString
source to share