Azure函数使用输入和输出绑定在本地开发

时间:2017-10-11 12:12:14

标签: c# azure visual-studio-2017 azure-functions azure-webjobs

查看在Visual Studio 2017中开发Azure功能的示例,可以看到可以使用触发器设置新的功能模板。

因此,对于队列,模板将如下:

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}");
        }
    }
} 

您是否能够在本地添加和运行其他输入和输出绑定,例如:

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

        }
    }
} 

是否需要在local.settings.json中配置它们?

3 个答案:

答案 0 :(得分:3)

你当然可以。您还需要使用属性来装饰它们:

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

本地环境的配置值确实取自local.settings.json,因此您需要在那里添加它们(连接字符串等)。

答案 1 :(得分:2)

对于任何寻找有关函数绑定属性的信息的人:

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

从我的问题中得到一个完整的例子:

<强> 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"
  }
}

答案 2 :(得分:0)

@Chris:这很奇怪,&#34;我的&#34; EventHubAttribute的版本没有Connection属性。我正在使用 Microsoft.Azure.WebJobs.ServiceBus 2.0.0 。 你用的是什么版本?据我所知,最新版本是2.0.0。