如何在异步Azure功能中使用Azure通知中心?

问题描述 投票:0回答:1

我正在尝试创建一个使用Azure通知中心的预编译Azure功能,我在运行时遇到了一些困难。

我的问题概述如下。

我的步骤:1。在Visual Studio中开始本地开发Azure功能。 2.创建使用NotificationHub属性的异步函数。例如,这是我的简化版本:

[FunctionName("MyFunction")]
    public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, 
                                 [NotificationHub]IAsyncCollector<Notification> notification,
                                 TraceWriter log)
    {                                    
        using (var httpClient = new HttpClient())
        {
            try
            {
                // Make several async calls, and create a Windows TileNotification here...
                    await notification.AddAsync(new WindowsNotification(tileContent.GetContent()));
                }                    
            }                
            catch (Exception ex)
            {
                // log failure, etc.
            }
        }
    }

我尝试在本地运行时收到以下错误消息:

Microsoft.Azure.WebJobs.Host:错误索引方法'MyFunction.Run'。

Microsoft.Azure.WebJobs.Host:无法将参数'notification'绑定到IAsyncCollector`1类型。确保绑定支持参数Type。如果您正在使用绑定扩展(例如ServiceBus,Timers等),请确保您已在启动代码中调用扩展的注册方法(例如config.UseServiceBus(),config.UseTimers()等)。

该文档有几个使用out参数的同步示例,但没有异步示例,我不太确定错误消息试图告诉我什么。我的NotificationHub属性是否需要其他配置?我是否需要一个functions.json文件,即使我正在使用预编译属性?

c# azure azure-functions azure-notificationhub
1个回答
0
投票

正如Roman Kiss所说,我们需要配置通知属性属性,例如ConnectionStringSetting,HubName。对于预编译的C#函数,请使用在NuGet包NotificationHub attribute中定义的Microsoft.Azure.WebJobs.Extensions.NotificationHubs

要配置绑定,请使用设置名称AzureWebJobsNotificationHubsConnectionString将NotificationHubs命名空间连接字符串添加为应用程序设置或环境变量

public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, [NotificationHub(HubName = "tomnotification", ConnectionStringSetting = "AzureWebJobsNotificationHubsConnectionString")]IAsyncCollector<Notification> notification, TraceWriter log)
    {
        //add you logic
    }

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "storage connection string",
    "AzureWebJobsDashboard": "storage connection string",
    "AzureWebJobsNotificationHubsConnectionString": "notifiaction hub connection string"
  }
}

测试结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.