0
0
Azurecloud~10 mins

Input and output bindings in Azure - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an input binding for an Azure Function that triggers on a Blob storage event.

Azure
public static void Run([BlobTrigger("[1]/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, ILogger log) {
    log.LogInformation($"Blob trigger function processed blob: {name}");
}
Drag options to blanks, or click blank then click option'
Ainput-blobs
Binput-data
Cinput-files
Dinput-container
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong container name that does not exist in the storage account.
Omitting the container name in the BlobTrigger path.
2fill in blank
medium

Complete the code to define an output binding that writes a message to an Azure Queue.

Azure
public static void Run(string input, [Queue("[1]", Connection = "AzureWebJobsStorage")] out string outputMessage, ILogger log) {
    outputMessage = input.ToUpper();
    log.LogInformation("Message sent to queue.");
}
Drag options to blanks, or click blank then click option'
Aoutput-logs
Boutput-queue
Coutput-events
Doutput-messages
Attempts:
3 left
💡 Hint
Common Mistakes
Using a queue name that does not exist or is misspelled.
Confusing input and output queue names.
3fill in blank
hard

Fix the error in the output binding attribute to correctly write to a Cosmos DB collection.

Azure
public static void Run(string input, [CosmosDB(databaseName: "MyDb", collectionName: "[1]", ConnectionStringSetting = "CosmosDBConnection")] out dynamic document, ILogger log) {
    document = new { id = Guid.NewGuid().ToString(), content = input };
    log.LogInformation("Document written to Cosmos DB.");
}
Drag options to blanks, or click blank then click option'
AMyItems
BMyContainer
CMyDocuments
DMyCollection
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect or outdated collection names.
Confusing collectionName with databaseName.
4fill in blank
hard

Fill both blanks to create an Azure Function that reads from a Service Bus queue and writes to a Blob storage output.

Azure
public static void Run([ServiceBusTrigger("[1]", Connection = "ServiceBusConnection")] string myQueueItem, [Blob("[2]/{rand-guid}.txt", FileAccess.Write, Connection = "AzureWebJobsStorage")] out string outputBlob, ILogger log) {
    outputBlob = myQueueItem;
    log.LogInformation("Message processed and saved to blob.");
}
Drag options to blanks, or click blank then click option'
Ainput-queue
Boutput-container
Cinput-container
Doutput-queue
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing input and output names.
Using queue names for blob containers or vice versa.
5fill in blank
hard

Fill the blanks to define an Azure Function with an HTTP trigger input, a Cosmos DB output, and a Queue output binding.

Azure
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
    [CosmosDB(databaseName: "MyDb", collectionName: "[1]", ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector<dynamic> documentsOut,
    [Queue("[2]", Connection = "AzureWebJobsStorage")] IAsyncCollector<string> queueOut,
    ILogger log) {
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    await documentsOut.AddAsync(new { id = Guid.NewGuid().ToString(), content = requestBody });
    await queueOut.AddAsync(requestBody);
    log.LogInformation("Data saved to Cosmos DB and queue.");
    return new OkResult();
}
Drag options to blanks, or click blank then click option'
AMyCollection
Boutput-queue
Coutput-container
DMyDocuments
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing container names with queue names.
Using wrong names that do not match the Azure resources.