Bird
Raised Fist0
Azurecloud~10 mins

Input and output bindings in Azure - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of input and output bindings in Azure Functions?
easy
A. To create user interfaces for Azure Functions
B. To write complex code for cloud service connections
C. To connect your function easily to cloud services without extra code
D. To manage virtual machines in Azure

Solution

  1. Step 1: Understand the role of bindings

    Bindings let your function connect to cloud services like queues or blobs without writing extra code.
  2. Step 2: Compare options

    Only To connect your function easily to cloud services without extra code describes this easy connection purpose. Others mention unrelated tasks.
  3. Final Answer:

    To connect your function easily to cloud services without extra code -> Option C
  4. Quick Check:

    Bindings simplify cloud connections = A [OK]
Hint: Bindings simplify cloud connections by reducing code [OK]
Common Mistakes:
  • Thinking bindings create user interfaces
  • Confusing bindings with VM management
  • Assuming bindings require complex code
2. Which of the following is the correct syntax to declare an output binding in an Azure Function JSON configuration?
easy
A. { "type": "blob", "direction": "out", "name": "outputBlob" }
B. { "type": "table", "direction": "in", "name": "inputTable" }
C. { "type": "httpTrigger", "direction": "out", "name": "req" }
D. { "type": "queue", "direction": "in", "name": "myQueueItem" }

Solution

  1. Step 1: Identify output binding syntax

    Output bindings use "direction": "out" and specify the resource type and name.
  2. Step 2: Check each option

    { "type": "blob", "direction": "out", "name": "outputBlob" } correctly uses "direction": "out" with a blob type. Others either have "in" direction or wrong types for output.
  3. Final Answer:

    { "type": "blob", "direction": "out", "name": "outputBlob" } -> Option A
  4. Quick Check:

    Output binding = direction: out [OK]
Hint: Output bindings always use "direction": "out" in config [OK]
Common Mistakes:
  • Using "in" direction for output bindings
  • Confusing trigger types with bindings
  • Wrong resource type for output
3. Given this Azure Function code snippet with bindings:
{
  "bindings": [
    { "name": "inputQueueItem", "type": "queueTrigger", "direction": "in", "queueName": "myqueue" },
    { "name": "outputBlob", "type": "blob", "direction": "out", "path": "samples-output/{rand-guid}.txt" }
  ]
}

What happens when a new message arrives in the queue?
medium
A. The function triggers but cannot write to blob because output binding is missing
B. The function triggers and deletes the queue message without output
C. The function does not trigger because queueTrigger is invalid
D. The function triggers, reads the queue message, and writes a new blob file with a random name

Solution

  1. Step 1: Understand the bindings

    The queueTrigger input binding triggers the function when a message arrives. The output binding writes a blob file with a random GUID name.
  2. Step 2: Analyze the behavior

    When a message arrives, the function runs, reads the message, and writes a blob file as specified.
  3. Final Answer:

    The function triggers, reads the queue message, and writes a new blob file with a random name -> Option D
  4. Quick Check:

    QueueTrigger input + blob output = trigger and write blob [OK]
Hint: QueueTrigger input starts function; output binding writes data [OK]
Common Mistakes:
  • Thinking output binding is missing
  • Assuming function does not trigger
  • Confusing queueTrigger with other triggers
4. You have this binding configuration in your Azure Function:
{
  "bindings": [
    { "name": "inputBlob", "type": "blob", "direction": "in", "path": "samples/input.txt" },
    { "name": "outputQueueItem", "type": "queue", "direction": "out", "queueName": "outputqueue" }
  ]
}

The function code tries to assign a string to outputQueue, but you get an error. What is the likely cause?
medium
A. The input binding type "blob" cannot be used as input
B. The output binding name does not match the function parameter name
C. The output binding type "queue" requires a special object, not a string
D. The function code must not assign values to output bindings

Solution

  1. Step 1: Check binding names and code parameters

    The binding name "outputQueueItem" must exactly match the function parameter name used in code to assign output.
  2. Step 2: Identify mismatch cause

    If names differ, the function cannot bind the output correctly, causing errors when assigning values.
  3. Final Answer:

    The output binding name does not match the function parameter name -> Option B
  4. Quick Check:

    Binding names must match code parameters [OK]
Hint: Binding names must exactly match function parameters [OK]
Common Mistakes:
  • Assuming blob input is invalid
  • Thinking output queue needs special object
  • Believing outputs cannot be assigned in code
5. You want an Azure Function to read messages from a Service Bus queue and write processed results to a Cosmos DB collection. Which binding configuration correctly connects these services?
hard
A. { "name": "msg", "type": "serviceBusTrigger", "direction": "in", "queueName": "inputqueue" }, { "name": "outputDocument", "type": "cosmosDB", "direction": "out", "databaseName": "mydb", "collectionName": "results" }
B. { "name": "msg", "type": "queueTrigger", "direction": "in", "queueName": "inputqueue" }, { "name": "outputDocument", "type": "blob", "direction": "out", "path": "results/{id}.json" }
C. { "name": "msg", "type": "serviceBusTrigger", "direction": "out", "queueName": "inputqueue" }, { "name": "outputDocument", "type": "cosmosDB", "direction": "in", "databaseName": "mydb", "collectionName": "results" }
D. { "name": "msg", "type": "serviceBusTrigger", "direction": "in", "queueName": "inputqueue" }, { "name": "outputDocument", "type": "table", "direction": "out", "tableName": "results" }

Solution

  1. Step 1: Identify correct trigger and output types

    Service Bus messages use "serviceBusTrigger" with "direction": "in". Cosmos DB output uses "cosmosDB" with "direction": "out" and database/collection names.
  2. Step 2: Check each option

    { "name": "msg", "type": "serviceBusTrigger", "direction": "in", "queueName": "inputqueue" }, { "name": "outputDocument", "type": "cosmosDB", "direction": "out", "databaseName": "mydb", "collectionName": "results" } correctly sets input as Service Bus trigger and output as Cosmos DB. Others have wrong directions or types.
  3. Final Answer:

    { "name": "msg", "type": "serviceBusTrigger", "direction": "in", "queueName": "inputqueue" }, { "name": "outputDocument", "type": "cosmosDB", "direction": "out", "databaseName": "mydb", "collectionName": "results" } -> Option A
  4. Quick Check:

    ServiceBusTrigger in + CosmosDB out = C [OK]
Hint: Service Bus triggers are input; Cosmos DB bindings are output [OK]
Common Mistakes:
  • Swapping input/output directions
  • Using wrong binding types for services
  • Confusing queueTrigger with serviceBusTrigger