c# - 在 Azure Function 中将 BrokeredMessage 与 ServiceBus 队列触发器结合使用

标签 c# azure azure-functions azureservicebus azure-servicebus-queues

我创建了一个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}");
}

我发现我也可以将消息绑定(bind)到 POCO,如下所示:

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

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

最佳答案

编辑新 SDK 使用 #r 指令支持 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 包的引用(请参阅 SO Post ):

{
    "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 !!!");
}

关于c# - 在 Azure Function 中将 BrokeredMessage 与 ServiceBus 队列触发器结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707066/

相关文章:

c# - 我应该在哪里获取 ServiceDefinition.csdef 文件?

c# - 将函数定义作为 CI 流程的一部分导入 API 管理

c# - 在 Azure Function 中使用 Azure Fluent SDK 时,如何使用托管服务标识创建 azure 对象?

c# - InvalidOperationException 当我尝试从数据库中检索密码时, "Invalid attempt to read when no data is present."

c# - 正则表达式 (.NET) - 如何匹配字符串末尾包含可变数字的模式?

wordpress - 增加 Azure VM 中托管的 Wordpress 的最大文件上传大小

azure - 如何在 Azure 机器学习服务中启用 ACI Web 服务的身份验证?

c# - Azure Function - 在 Visual Studio 2017 中为不同环境添加不同的设置文件

c# - Linq Foreach 跳线

C# 扩展 Log4net ILog 接口(interface)的现有方法