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

How to do it…

Perform the following steps:

  1. Navigate to the Integrate tab of the RegisterUser HTTP trigger function.
  2. Click on the New Output button, select Azure Queue Storage, and then click on the Select button.
  3. Provide the following parameters in the Azure Queue Storage output settings:
    • Message parameter name: Set the name of the parameter to objUserProfileQueueItemwhich will be used in the Run method
    • Queue name: Set the value of the Queue name to userprofileimagesqueue
    • Storage account connection: Make sure that you select the right storage account in the Storage account connection field
  4. Click on Save to create the new output binding.
  5. Navigate back to the code editor by clicking on the function name (RegisterUser, in this example) or the run.csx file and make the changes marked bold that are given in the following code:
public static async Task<IActionResult> Run(
HttpRequest req,
CloudTable objUserProfileTable,
IAsyncCollector<string> objUserProfileQueueItem,
ILogger log)
{

....

string firstname= inputJson.firstname;
string profilePicUrl = inputJson.ProfilePicUrl;
await objUserProfileQueueItem.AddAsync(profilePicUrl);

....
objUserProfileTable.Execute(objTblOperationInsert);
}
  1. In the preceding code, we added Queue output bindings by adding the IAsyncCollecter parameter to the Run method and just passing the required message to the AddAsync method. The output bindings will take care of saving the ProfilePicUrl into the Queue. Now, Click on Save to save the code changes in the code editor of the run.csx file.
  2. Let's test the code by adding another parameter, ProfilePicUrl, in the Request body and then click on the Run button in the Test tab of the Azure Function code editor window. The image that's used in the following JSON might not exist when you are reading this book. So, make sure that you provide a valid URL of the image:
{
"firstname": "Bill",
"lastname": "Gates",
"ProfilePicUrl":"https://upload.wikimedia.org/wikipedia/ commons/1/19/Bill_Gates_June_2015.jpg"
}
  1. If everything goes well you will see a Status : 200 OK message. The image URL that you have passed as an input parameter in the Request body will be created as a Queue message in the Azure Storage Queue service. Let's navigate to Azure Storage Explorer and view the Queue named userprofileimagesqueue, which is the Queue name that we provided in step 3. The following is a screenshot of the Queue message that was created: