- Azure Serverless Computing Cookbook
- Praveen Kumar Sreeram
- 507字
- 2021-07-02 20:23:03
How to do it…
- Navigate to the Integrate tab of the RegisterUser HTTP trigger function.
- Click on the New Output button and select Azure Queue Storage then click on the Select button.
- Provide the following parameters in the Azure Queue Storage output settings:
- Queue name: Set the value of the Queue name as userprofileimagesqueue
- Storage account connection: Please make sure that you select the right storage account in the Storage account connection field
- Message parameter name: Set the name of the parameter to objUserProfileQueueItem which will be used in the Run method
- Click on Save to the create the new output binding.
- In this recipe, we will look at another approach of grabbing the request parameters for which we will use the Newtonsoft.JSON library to parse the JSON data. Let's navigate to the View files tab as shown in the following screenshot:

- As shown in the preceding screenshot, click on Add to add a new file. Please make sure that you name it as project.json as shown in the preceding screenshot.
- Once the file is created, add the following code to the project.json file. The following code adds the reference of the Newtonsoft.Json library.
{
"frameworks" : {
"net46": {
"dependencies":{
"Newtonsoft.Json" : "10.0.2"
}
}
}
}
- Navigate back to the code editor by clicking on the function name (RegisterUser in this example) and paste the following code:
#r "Microsoft.WindowsAzure.Storage"
using System.Net;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
public static void Run(HttpRequestMessage req,
TraceWriter log,
CloudTable
objUserProfileTable,
out string
objUserProfileQueueItem
)
{
var inputs = req.Content.ReadAsStringAsync().Result;
dynamic inputJson = JsonConvert.DeserializeObject<dynamic>
(inputs);
string firstname= inputJson.firstname;
string lastname=inputJson.lastname;
string profilePicUrl = inputJson.ProfilePicUrl;
objUserProfileQueueItem = profilePicUrl;
UserProfile objUserProfile = new UserProfile(firstname,
lastname, profilePicUrl);
TableOperation objTblOperationInsert =
TableOperation.Insert(objUserProfile);
objUserProfileTable.Execute(objTblOperationInsert);
}
public class UserProfile : TableEntity
{
public UserProfile(string lastname, string firstname,
string profilePicUrl)
{
this.PartitionKey = "p1";
this.RowKey = Guid.NewGuid().ToString();
this.FirstName = firstname;
this.LastName = lastname;
this.ProfilePicUrl = profilePicUrl;
}
public UserProfile() { }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ProfilePicUrl {get; set;}
}
- Click on Save to save the code changes in the code editor of the run.csx file.
- Let's test the code by adding another parameter ProfilePicUrl to the Request body shown as follows then click on the Run button in the Test tab of the Azure Function code editor window: The image used in the below JSON might not exist when you are reading this book. So, Please 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"
}
- If everything goes fine you will see the Status : 200 OK message, then 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 have provided in the Step 3. Following is the screenshot of the Queue message that was created:

推薦閱讀
- Node.js 10實戰
- Android和PHP開發最佳實踐(第2版)
- MongoDB for Java Developers
- UI智能化與前端智能化:工程技術、實現方法與編程思想
- 名師講壇:Java微服務架構實戰(SpringBoot+SpringCloud+Docker+RabbitMQ)
- Java:High-Performance Apps with Java 9
- C語言程序設計實驗指導 (第2版)
- Getting Started with Eclipse Juno
- Statistical Application Development with R and Python(Second Edition)
- Flowable流程引擎實戰
- 代碼閱讀
- IoT Projects with Bluetooth Low Energy
- Qt 4開發實踐
- 小學生C++趣味編程從入門到精通
- 3D Printing Designs:Design an SD Card Holder