1
votes

Using MessageReceiver only first time getting messages from topic using Azure Function after that next time when execute Function though I have messages into service bus topic still getting null into variable messages.

Using below code to get messages from Service Bus Topic , whats the cause?

string serviceBusConnectionString = Environment.GetEnvironmentVariable("ConnectionString");
var messageReceiver = new MessageReceiver(serviceBusConnectionString, Environment.GetEnvironmentVariable("topicName/subscriptions/subscriptionName"), ReceiveMode.PeekLock, null, 500);

var messages = await messageReceiver.ReceiveAsync(500, TimeSpan.FromSeconds(1));

i can't understand this - after every 15 minutes when LA call FA then FA should able to read new available messages but it is coming null

enter image description here

is there anything related with lock duration ?

3
Why are you not using Service Bus Triggers when you already use Azure Functions? Thats exactly what they are for. docs.microsoft.com/en-us/azure/azure-functions/… - silent
because my use-case is different i'm calling this http trigger FA from Logic app. - Neo
Have you tried your function code outside of the function? Also, I would verify the settings are pointing to the correct environment. - Sean Feldman
yup settings are also correct .. not sure why this kind of behavior - Neo

3 Answers

1
votes
var messages = await messageReceiver.ReceiveAsync(500, TimeSpan.FromSeconds(1));

will wait until it receives one or more messages and will then return. Any messages that come in later will not be read. If you want to do that (and I want to doubt that this is a good idea inside an Azure Function), put this in a loop:

while(/* some condition */)
{
     messages = await messageReceiver.ReceiveAsync(500, TimeSpan.FromSeconds(1));
     // do something with the received messages
}
1
votes

If i didn't understand it wrong, you use a timer trigger logic app to call the function to get the service bus message and you always get the old message and you don't want it.

You always get the old message because your ReceiveMode is PeekLock, about this ReceiveMode you could check this description:Receive mode. After complete the message, the message wouldn't be deleted, that's why you always get the old message.

First way is you could change it to ReceiveAndDelete when you create MessageReceiver like the below code.

            string serviceBusConnectionString = Environment.GetEnvironmentVariable("servicebuscon");
            var messageReceiver = new MessageReceiver(serviceBusConnectionString, "myqueue", ReceiveMode.ReceiveAndDelete, null, 500);


            var tempMessages = await messageReceiver.ReceiveAsync(500, TimeSpan.FromSeconds(1));

Note: if your function go exception or other errors and the message doesn't get processed completely, this message will be deleted with this mode.

The other way is after process the message call the CompleteAsync method, after process the message will be deleted.

You could refer to the below code.

            var messageReceiver = new MessageReceiver(serviceBusConnectionString, "myqueue", ReceiveMode.PeekLock, null, 500);

            var tempMessages = await messageReceiver.ReceiveAsync(500, TimeSpan.FromSeconds(1));

            foreach (Message m1 in tempMessages)
            {

                log.LogInformation($"C# HTTP trigger function processed message: {Encoding.UTF8.GetString(m1.Body)}");
                await messageReceiver.CompleteAsync(m1.SystemProperties.LockToken);

            }
0
votes

Enable Batch Operation should be false.

But this is also working for random times :( not a well proof solution for the problem.

check the below screenshot - enter image description here