4

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?

Chris
  • 3,113
  • 5
  • 24
  • 46

3 Answers3

3

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).

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
2

For anyone looking for information about function binding attributes:

https://learn.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"
  }
}
Chris
  • 3,113
  • 5
  • 24
  • 46
  • Hi, This is currently not working for me when the funciton is deploied... Do you have an idea ? – OrcusZ Aug 29 '18 at 15:38
  • @OrcusZ do you have any errors? Have you set your variables inside the function app settings? – Chris Aug 29 '18 at 16:28
  • yes, I have Invalid storage account. And i correcly set the vaiable in my function app. I open a ticket in Azure GitHub : https://github.com/Azure/azure-functions-vs-build-sdk/issues/225 – OrcusZ Aug 30 '18 at 08:28
0

@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.

MAhipal Singh
  • 4,745
  • 1
  • 42
  • 57
Fredrik Gunne
  • 35
  • 1
  • 6
  • Hi Fredrik. When I first asked the question I was using version **2.1.0-beta1** which is what came when I created the function from an Event Hub trigger template. I have just created a new function and the latest version that comes with the template is **2.1.0-beta4**. Are you able to update to either of these versions or do you have to use the latest stable? – Chris Jan 03 '18 at 11:22
  • Just been playing around with different versions and it looks like **2.1.0-beta1** is the lowest version where `Connection` is a property on the EventHubAttribute. – Chris Jan 03 '18 at 11:41
  • Yes, it works with 2.1.0-beta1, thanks! I added the assembly "manually" using NuGet manager, and it suggested 2.0.0. The assembly was not added automatically when I created the function in VS 2017. I am using an HTTP Trigger template, so that isn't surprising. – Fredrik Gunne Jan 03 '18 at 15:34