I have a trouble marking my BrokeredMessage as Complete.
Simple code that works like expected:
private void OnMessageArrived(BrokeredMessage message)
{
var myObj= message.GetBody<MyObject>();
//do things with myObj
message.Complete();
}
When I try to wait for user to finish with myObj I get exception:
brokeredmessage has been disposed
Code below:
private Dictionary<long, BrokeredMessage> ReceivedMessages;
ReceivedMessages = new Dictionary<long, BrokeredMessage>();
private void OnMessageArrived(BrokeredMessage message)
{
var myObj= message.GetBody<MyObject>();
ReceivedMessages.Add(myObj.Id, message);
//do things with myObj
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// get myObj on which user clicked
ReceivedMessages[myObj.Id].Complete();
ReceivedMessages.Remove(myObj.Id);
}
To me it looks like ServiceBus somehow lose connection to actual object in c#
Something similar to detached object in EF, just in this case object is detached from ServiceBus
EDIT:
It is important to me to mark message as complete only after user click button. In case AC goes down (or similar things) I want messages still remain on Service Bus Topic so that next time user start app he will again receive messages that he did not processed.
You should not store the brokeredmessage itself. When you call the .Complete() method, this is what happens according to the documentation:
Completes the receive operation of a message and indicates that the message should be marked as processed and deleted
What you should do instead is to store MyObject type of objects in your dictionary as brokered messages will be destroyed upon completion.
private Dictionary<long, MyObject> ReceivedMessages;
ReceivedMessages = new Dictionary<long, MyObject>();
And in the relevant bit of code:
var myObj= message.GetBody<MyObject>();
ReceivedMessages.Add(myObj.Id, myObj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With