Azure功能与服务总线:无法“完成”和访问代理消息的属性

时间:2018-01-19 18:15:14

标签: azure azure-functions azureservicebus azure-servicebus-queues

这是我在控制台应用中的工作代码:

作家(工作代码):

static void Main(string[] args)

    {
        Console.WriteLine("Starting..");

        string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

        QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "xxx");

        // Create message, passing a string message for the body.
        BrokeredMessage message = new BrokeredMessage("");

        // Set some addtional custom app-specific properties.
        message.Properties["UserCode"] = "HELLOOO22353";
        message.Properties["UserId"] = "4511";

        try
        {
            // Send message to the queue.
            Client.Send(message);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }


        Console.WriteLine("Complete..");
        //Console.ReadKey();
    }

读者:(工作代码)

static void Main(string[] args)
    {
        Console.WriteLine("Starting");

        string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

        QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "xxx");

        OnMessageOptions options = new OnMessageOptions();
        options.AutoComplete = false;
        options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

        Client.OnMessage((message) =>
        {
            try
            {
                string sMessage = " UserCode: " + message.Properties["UserCode"];

                Console.WriteLine("Found new User - " + sMessage);


                // Remove message from queue.
                message.Complete();


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                // Indicates a problem, unlock message in queue.
                message.Abandon();
            }
        }, options);

        Console.ReadKey();
    }

现在,这是我在Azure功能中无法使用的代码:

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

    log.Info("1111");
    log.Info(myMessage.MessageId);  // this works
    myMessage.Complete(); // fails: Microsoft.ServiceBus: Operation is not valid due to the current state of the object.

    log.Info(myMessage.Properties["UserCode"].ToString());  // fails: myMessage.Properties is empty for some reason

}

我无法理解为什么Reader控制台应用程序能够正确读取和完成消息,但Azure功能一(基本上基于相同的想法)不是。这两个代码都使用相同版本的Windows.ServiceBus包。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)