Debug an Azure Function

In this section, we'll run an Azure Function locally and debug it. We'll  be developing new functions and test the functionality before deploying to public cloud. And to ensure that it happens correctly, we'll require the single function created directly from the HTTP trigger with parameters template. 

Currently, your machine does not have the correct runtime to run an Azure Function, so we need to download it:

  1. Click the Play button in Visual Studio, and a dialog box should ask you if you want to download Azure Functions Core Tools—click on Yes. A Windows CMD window will open, with the lightning bolt logo of Azure Functions:

It will bootstrap the environment and attach the debugger from Visual Studio. It will then list the endpoints the Function App is listening on.
  1. Open up Postman app and copy and paste the endpoint into it, selecting either a POST or GET verb.
You should get the response Hello {name}. Try changing the {name} in the path to your name, and you will see a different response.
You can download Postman at https://www.getpostman.com/.

  1. Create a debug point in the Run method by clicking in the margin to the left of the code:
               
Refer to the code for this example placed at Code/Serverless-Architectures-with-Azure/Lesson 1/BeginningAzureServerlessArchitecture/PostTransactionsExA.cs.

Go to https://goo.gl/iCt7dG to access the code.
  1. Use Postman to send the request:
  1. You are now able to use standard Visual Studio debugging features and inspect the different objects as shown in the following screenshot:

  1. Set your verb to POST , and add a message in the payload. See if you can find the verb in the HttpRequestMessage object in debug mode. It should be in the method property.
If you need to download Azure-functions-core-tools separately, you can use npm command to download it—npm install -g azure-functions-core-tools for version 1 (fully supported) and npm install -g azure-functions-core-tools@core for version 2 (beta). We will go into the differences in versions later in this chapter. You can then use the debug setup to set Visual Studio to call an external program with the command func host start when you click on the Debug button.

Outcome

You have debugged an Azure Function and tested it using Postman.

As you can see from running the function locally, you, the developer, do not need to write any of the usual boilerplate code for message handling or routing. You don't even need to use ASP.NET controllers, or set up middleware. The Azure Functions container handles absolutely everything, leaving your code to simply do the business logic.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.133.233