Azure Serverless Computing Cookbook
上QQ阅读APP看书,第一时间看更新

Debugging Azure Function hosted in Azure using Visual Studio

Once the basic setup of your Function creation is complete, the next step is to start working on developing the application as per your needs. Developers end up facing numerous technical issues that require tools to identify the root cause of the problem and fix it. These tools include debugging tools that help developers to step into each line of the code to view the values of the variables and objects and get a detailed view of the exceptions.

Getting ready

Download and install the Azure CLI (if these tools are not installed, Visual Studio will automatically download them when you run your functions from Visual Studio).

How to do it...

In this section, you'll learn how to configure and debug an Azure function in a local development environment within Visual Studio.

Perform the following steps:

  1. In the previous recipe, you created the HTTP trigger function using Visual Studio. Let's build the application by clicking on Build and then clicking on Build Solution.
  2. Open the HttpTriggerCSharpFromVS.cs file and create a breakpoint by pressing the F9 key, as shown in Figure 4.4:
    Figure 4.4: The HTTP trigger function code
  3. Press the F5 key to start debugging the function. When we press F5 for the first time, Visual Studio prompts us to download the Azure Functions CLI tools if they aren't already installed. These tools are essential for executing an Azure function in Visual Studio:
    Figure 4.5: Azure Functions core tool installation

    Note

    The Azure Functions CLI has now been renamed Azure Functions Core Tools. Find out more about them at https://www.npmjs.com/package/azure-functions-core-tools.

  4. Clicking on Yes, as shown in Figure 4.5, will start downloading the CLI tools. The download and installation of the CLI tools will take a few minutes.
  5. Once the Azure Functions CLI tools have been installed successfully, a job host will be created and started. It will start monitoring requests on a specific port for all the functions of your function application. Figure 4.6 shows that the job host has started monitoring the requests to the function application:
    Figure 4.6: The Azure function job host
  6. Let's try to access the function application by making a request to http://localhost:7071 in any web browser:
    Figure 4.7: The Azure Functions 3.0 default web page

    Now, type the complete URL of your HTTP trigger in the browser. The URL should look like this:

    http://localhost:7071/api/HttpTriggerCsharpFromVS?name=Praveen Sreeram.

  7. After entering the correct URL of the Azure function, as soon as you hit the Enter key in the address bar of the browser, the Visual Studio debugger will hit the debugging point (if you have one), as shown in Figure 4.8:
    Figure 4.8: The Azure Function HTTP trigger—creating a debug point
  8. You can also view the data of your variables, as shown in Figure 4.9:
    Figure 4.9: The Azure Function HTTP trigger—viewing variable values
  9. Once you complete debugging, you can press the F5 key to complete the execution process, after which, you'll see the output response in the browser, as shown in Figure 4.10:
    Figure 4.10: The HTTP trigger output
  10. The function execution log will be seen in the job host console, as shown in Figure 4.11:
Figure 4.11: The HTTP trigger execution log

You can add more Azure Functions to the function application, if required. In the next recipe, we'll look at how to connect to the Azure Storage cloud from the local environment.

How it works…

The job host works as a server that listens to a specific port. If there are any requests to that particular port, it automatically takes care of executing the requests and sends a response.

The job host console provides us with the following details:

  • The status of the execution, along with the request and response data.
  • The details of all the functions available in the function application.

There's more...

Using Visual Studio, we can directly create precompiled functions, which means that when we build our functions, Visual Studio creates a .dll file that can be referenced in other applications, just as we do for our regular classes. The following are two of the advantages of using precompiled functions:

  • Precompiled functions have better performance, as they aren't required to be compiled on the fly.
  • We can convert our traditional classes into Azure Functions easily, and refer to them in other applications seamlessly.

In this recipe, you have learned how to debug Azure Functions in the local development workstation. In the next recipe, you'll learn how to connect to a storage account available in Azure.