简体   繁体   中英

Azure functions with service bus: Unable to “complete” and access properties of a brokered message

This is my working code in a console app:

Writer (working code):

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();
    }

Reader: (working code)

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();
    }

Now, this is the code I have in Azure functions which isn't working:

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
   
}

What I'm failing to understand is why the Reader console app is able to read and complete message correctly but Azure function one (which is essentially based on the same idea) isn't. Both the codes are using the same version of Windows.ServiceBus package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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