Connecting to the Azure Storage from Visual Studio
In both of the previous recipes, you learned how to create and execute Azure Functions in a local environment. You triggered the functions from a local browser. However, in this recipe, you'll learn how to trigger an Azure function in your local environment when an event occurs in Azure. For example, when a new blob is created in an Azure storage account, we can have our function triggered on our local machine. This helps developers to test their applications upfront, before deploying them to the production environment.
Getting ready
Perform the following steps:
- Create a storage account, and then a blob container named cookbookfiles, in Azure.
- Install Microsoft Azure Storage Explorer from http://storageexplorer.com/.
How to do it...
In this section, you'll learn how to create a blob trigger that will trigger as soon as a blob is created in the storage account.
Perform the following steps:
- Open the FunctionAppInVisualStudio Azure Function application in Visual Studio, and then add a new function by right-clicking on the FunctionAppInVisualStudio project. Click on Add | New Azure Function, which will open a pop-up window. Here, for the name field, enter BlobTriggerCSharp and then click on the Add button.
- This will open another dialog box, where you can provide other parameters, as shown in Figure 4.12:
Figure 4.12: New Azure Function—HTTP trigger
- In the Connection string setting field, provide AzureWebJobsStorage as the name of the connection string, and also provide the name of the blob container (in this case, it is cookbookfiles) in the Path input field, and then click on the OK button to create the new blob trigger function. A new blob trigger function will be created, as shown in Figure 4.13:
Figure 4.13: Azure Functions—Solution Explorer
- As you learned in the Building a back-end web API using HTTP triggers recipe from Chapter 1, Accelerating cloud app development using Azure Functions, the Azure Management portal allows us to choose between a new or existing storage account. However, the preceding dialog box is not connected to our Azure subscription. So, let's navigate to the storage account and copy the Connection string, which can be found in the Access keys blade of the storage account in the Azure Management portal, as shown in Figure 4.14:
Figure 4.14: The storage account—the Access keys blade
- Paste the Connection string in the local.settings.json file, which is in the root folder of the project. This file is created when you create the function application. Once you add the Connection string to the key named AzureWebJobsStorage, the local.settings.json file should look as shown in Figure 4.15:
Figure 4.15: Azure Functions—application settings
- Open the BlobTriggerCSharp.cs file and create a breakpoint, as shown in Figure 4.16:
Figure 4.16: The Azure Functions blob trigger—creating a breakpoint
- Now, press the F5 key to start the job host, as shown in Figure 4.17:
Figure 4.17: The Azure Functions host log—generating functions
- Let's add a new blob file using Azure Storage Explorer, as shown in Figure 4.18:
Figure 4.18: Storage Explorer
- As soon as the blob has been added to the specified container (in this case, it is cookbookfiles), which is sitting in the cloud in a remote location, the job host running in the local machine will detect that a new blob has been added and the debugger will hit the function, as shown in Figure 4.19:
Figure 4.19: Azure Functions blob trigger—breakpoint
That's it. You have learned how to trigger an Azure function in your local environment when an event occurs in Azure.
How it works…
In this BlobTriggerCSharp class, the Run method has the WebJobs attribute with a connection string (in this case, it is AzureWebJobsStorage). This instructs the runtime to refer to the Azure Storage connection string in the local settings configuration file with the key named after the AzureWebJobsStorage connection string. When the job host starts running, it uses the connection string and keeps an eye on the storage account containers that you have specified. Whenever a new blob is added or updated, it automatically triggers the blob trigger in the current environment.
There's more…
When we create Azure Functions in the Azure Management portal, we need to create triggers and output bindings in the Integrate tab of each Azure function. However, when we create a function from the Visual Studio IDE, we can just configure WebJobs attributes to achieve this.
Note
Learn more about WebJobs attributes at https://docs.microsoft.com/azure/app-service/webjobs-sdk-get-started.
In this recipe, you have learned how to create a blob trigger. In the next recipe, you'll learn how to deploy it to Azure.