Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BrokeredMessage disposed

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.

like image 448
topolm Avatar asked Mar 07 '16 15:03

topolm


1 Answers

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);
like image 57
Pedro G. Dias Avatar answered Sep 29 '22 03:09

Pedro G. Dias