简体   繁体   English

Azure函数输出服务总线绑定从计时器触发器

[英]Azure Function Output Service Bus Binding From Timer trigger

I am running Visual Studio 2017 Preview and running the function code locally and I am using the out of the box Azure Function project template. 我正在运行Visual Studio 2017 Preview并在本地运行功能代码,我正在使用开箱即用的Azure Function项目模板。 I'm trying to have an Azure Function triggered by a timer send a message to a Service Bus queue using an output binding but it looks like the WebJob SDK can't bind the output to a string type. 我正在尝试使用定时器触发的Azure功能使用输出绑定将消息发送到服务总线队列,但看起来WebJob SDK无法将输出绑定到字符串类型。

Binding 捆绑

 "bindings": [
    {
      "type": "serviceBus",
      "name": "msg",
      "queueName": "myqueue",
      "connection": "ServiceBusQueue",
      "accessRights": "manage",
      "direction": "out"
    }
  ]

Timer Function 定时器功能

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;

namespace MyFunctionApp
{
    public static class TimerTrigger
    {
        [FunctionName("TimerTriggerCSharp")]
        public static void Run([TimerTrigger("1 * * * * *", RunOnStartup = true)]TimerInfo myTimer, TraceWriter log, out string msg)
        {
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

            msg = "Hello!";
        }
    }
}

Error Message 错误信息

TimerTriggerCSharp: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.TimerTriggerCSharp'. TimerTriggerCSharp:Microsoft.Azure.WebJobs.Host:错误索引方法'Functions.TimerTriggerCSharp'。 Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'msg' to type String&. Microsoft.Azure.WebJobs.Host:无法将参数'msg'绑定到String&。 Make sure the parameter Type is supported by the binding. 确保绑定支持参数Type。 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,Timers等),请确保您已在启动代码中调用扩展的注册方法(例如config.UseServiceBus(),config.UseTimers()等)。

Am I missing a step in the setup, or does the Service Bus binding really not support a string for an out parameter 我错过了设置中的一个步骤,或者Service Bus绑定是否真的不支持out参数的字符串

It looks like you're missing the binding attributes for ServiceBus . 看起来你错过了ServiceBus的绑定属性。 I've only used the ICollector<T> types rather than an out string but it shouldn't matter either way. 我只使用ICollector<T>类型而不是out string但无论如何都应该无关紧要。

[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,
                       TraceWriter log,
                       [ServiceBus("%QueueName%", Connection = "ServiceBusConnection", EntityType = Microsoft.Azure.WebJobs.ServiceBus.EntityType.Queue)] out string msg)
{
   msg = "My message";
}

To run locally with the VS2017 Preview tooling you will also need the following local settings defined in local.settings.json to match up with your ServiceBus attribute. 要使用VS2017预览工具在本地运行,您还需要在local.settings.json定义以下本地设置以匹配您的ServiceBus属性。

{
  "Values": {
     "ServiceBusConnection" : "Endpoint=sb://.....your connection",
     "QueueName": "my-service-bus-queue
   }
}

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

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