在 Azure 函数中将 BrokeredMessage 与 ServiceBus 队列触发器结合使用

Using BrokeredMessage with ServiceBus Queue Trigger in Azure Function

我创建了一个 Azure Function,它会在新消息添加到 Azure ServiceBus 队列时触发。 使用此代码可以正常工作:

#r "Newtonsoft.Json"
#load "..\shared\person.csx"

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

public static void Run(string message, TraceWriter log)
{
    var person = JsonConvert.DeserializeObject<Person>(message, 
            new JsonSerializerSettings() {ContractResolver = new CamelCasePropertyNamesContractResolver()});
    log.Verbose($"From DeserializeObject: {person.FirstName} {person.LastName}");
}

我看到我也可以像这样将消息绑定到 POCO:

public static void Run(Person message, TraceWriter log)
{
    log.Verbose($"From DeserializeObject: {message.FirstName} {message.LastName}");
}

现在我想将消息绑定到 BrokeredMessage 因为我需要访问消息的属性。

编辑 新SDK支持使用#r directive

的servicebus sdk
#r "Microsoft.ServiceBus"
using Microsoft.ServiceBus.Messaging;

public static void Run(BrokeredMessage msg, TraceWriter log)
{
    log.Info($"C# ServiceBus queue trigger function processed message: {msg}");
}

旧版本

只需两步:

我创建了一个 project.json 文件来添加对 WindowsAzure.ServiceBus Nuget 包的引用(参见 ):

{
    "frameworks": {
        "net46":{
            "dependencies": {
                "WindowsAzure.ServiceBus": "2.7.6"
            }
        }
    }
}

我添加了对代理消息的引用:

using Microsoft.ServiceBus.Messaging;

public static void Run(BrokeredMessage  message, TraceWriter log)
{
    log.Verbose("Function has been triggered !!!");
}

我尝试了 Thomas 的解决方案,但似乎不再有效。

documentation 状态:

In addition, the following assemblies are special cased and may be referenced by simplename (e.g. #r "AssemblyName"):

  • ...
  • Microsoft.ServiceBus

所以在不触及 project.json 文件的情况下,以下工作:

#r "Microsoft.ServiceBus"

using Microsoft.ServiceBus.Messaging;

public static void Run(BrokeredMessage msg, TraceWriter log)
{
    log.Info($"C# ServiceBus queue trigger function processed message: {msg}");
}