查看在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中配置它们?
答案 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。