简体   繁体   English

如何在异步Azure功能中使用Azure Notification Hub?

[英]How do I use an Azure Notification Hub in an asynchronous Azure Function?

I'm attempting to create a Precompiled Azure Function that makes use of an Azure Notification Hub, and I'm having some difficulty getting it to run. 我试图创建一个利用Azure通知中心的预编译Azure函数,但是让它运行起来有些困难。

My issue is outlined below. 我的问题概述如下。

My steps: 1. Start local development of an Azure Function in Visual Studio. 我的步骤:1.在Visual Studio中开始本地开发Azure函数。 2. Create an async Function that uses the NotificationHub attribute. 2.创建一个使用NotificationHub属性的异步函数。 For example, here's a simplified version of mine: 例如,这是我的简化版本:

[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.
            }
        }
    }

I get the following error message when I attempt to run locally: 尝试在本地运行时收到以下错误消息:

Microsoft.Azure.WebJobs.Host: Error indexing method 'MyFunction.Run'. Microsoft.Azure.WebJobs.Host:错误索引方法“ MyFunction.Run”。

Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'notification' to type IAsyncCollector`1. Microsoft.Azure.WebJobs.Host:无法将参数“ notification”绑定到类型IAsyncCollector`1。 Make sure the parameter Type is supported by the binding. 确保绑定支持参数类型。 If you're using binding extensions (eg ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (eg config.UseServiceBus(), config.UseTimers(), etc.). 如果您正在使用绑定扩展(例如ServiceBus,Timer等),请确保已在启动代码中调用了扩展的注册方法(例如config.UseServiceBus(),config.UseTimers()等)。 )。

The documentation has several synchronous examples using out parameters, but no asynchronous examples, and I'm not quite certain what the error message is trying to tell me. 该文档有几个使用out参数的同步示例,但没有异步示例,并且我不确定该错误消息试图告诉我什么。 Does my NotificationHub attribute need additional configuration? 我的NotificationHub属性是否需要其他配置? Do I need a functions.json file even though I'm attemtping to use the Precompile Attribute? 即使我打算使用Precompile属性,我仍需要一个functions.json文件吗?

As Roman Kiss mentioned that we need to configure notification attribute properties such as ConnectionStringSetting, HubName. 如Roman Kiss所述,我们需要配置通知属性属性,例如ConnectionStringSetting,HubName。 For precompiled C# functions, use the NotificationHub attribute , which is defined in NuGet package Microsoft.Azure.WebJobs.Extensions.NotificationHubs . 对于预编译的C#函数,请使用NotificationHub属性 ,该属性在NuGet包Microsoft.Azure.WebJobs.Extensions.NotificationHubs中定义。

To configure the binding, add the NotificationHubs namespace connection string as an app setting or environment variable using the setting name AzureWebJobsNotificationHubsConnectionString 若要配置绑定,请使用设置名称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 local.settings.json

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

Test result: 测试结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM