4
votes

Looking at the examples for developing Azure Functions in Visual Studio 2017 and can see that a new function template can be set up with a trigger.

So for a queue, the template would be the following:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("QueueTriggerCSharp")]        
        public static void Run([QueueTrigger("myqueue-items", Connection = "QueueStorage")]string myQueueItem, TraceWriter log)
        {
            log.Info($"C# Queue trigger function processed: {myQueueItem}");
        }
    }
} 

Are you able to add and run other input and output bindings locally such as:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("QueueTriggerCSharp")]        
        public static async Task Run([QueueTrigger("myqueue-items", Connection = "QueueStorage")]string myQueueItem, CloudTable inputTable, IAsyncCollector<string> outputEventHubMessages, TraceWriter log)
        {
            log.Info($"C# Queue trigger function processed: {myQueueItem}");

            TableQuery<TableEntity> query = new TableQuery<FailedEventEntity>().Where(
            TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "helloWorld"));

            List<TableEntity> entities = inputTable.ExecuteQuery(query).ToList();

            await outputEventHubMessages.AddAsync(myQueueItem);

        }
    }
} 

Do they need to be configured in local.settings.json?

3

3 Answers

3
votes

Sure thing you are. You need to decorate them with attributes too:

[Table("table-name")] CloudTable inputTable, 
[EventHub("event-hub-name")] IAsyncCollector<string> outputEventHubMessages

The config values for local environment will be taken from local.settings.json indeed, so you need to add them there (connection strings etc).

2
votes

For anyone looking for information about function binding attributes:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library

And a completed example from my question:

Function1.cs

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.ServiceBus; // INCLUDE THIS FOR EVENT HUB ATTRIBUTE

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("QueueTriggerCSharp")]        
        public static async Task Run([QueueTrigger("myqueue-items", Connection = "QueueStorageConnectionString")]string myQueueItem, [Table("tableName", Connection = "StorageAccountConnectionString")]CloudTable inputTable, [EventHub("eventHubName", Connection = "EventHubConnectionString")]IAsyncCollector<string> outputEventHubMessages, TraceWriter log)
        {
            log.Info($"C# Queue trigger function processed: {myQueueItem}");

            TableQuery<TableEntity> query = new TableQuery<FailedEventEntity>().Where(
            TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "helloWorld"));

            List<TableEntity> entities = inputTable.ExecuteQuery(query).ToList();

            await outputEventHubMessages.AddAsync(myQueueItem);

        }
    }
} 

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "your_storage_account_connection_string",
    "AzureWebJobsDashboard": "your_storage_account_connection_string",
    "QueueStorageConnectionString": "your_queue_storage_connection_string"
    "StorageAccountConnectionString": "your_storage_account_connection_string"
    "EventHubConnectionString": "your_event_hub_connection_string"
  }
}
0
votes

@Chris: This is strange, "my" version of EventHubAttribute doesn't have a Connection property. I am using Microsoft.Azure.WebJobs.ServiceBus 2.0.0. What version are you using? As far as I can see, the latest available version is 2.0.0.