BrokeredMessage is located

I have a problem with my BrokeredMessage complete.

Simple code that works as expected:

private void OnMessageArrived(BrokeredMessage message) { var myObj= message.GetBody<MyObject>(); //do things with myObj message.Complete(); } 

When I try to wait for the user to finish with myObj, I get an exception:

shown by broker placed

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

It seems to me that ServiceBus will somehow lose connection with the real object in C #

Something similar to a separate object in EF, only in this case the object is disconnected from the ServiceBus

EDIT:

It is important for me to mark the message as complete only after clicking the user button. In the event of a drop in AC (or similar things), I want the messages to remain in the Bus Bus Topic section so that the next time the user starts the application, he will again receive messages that he did not process.

+5
source share
4 answers

You should not store brokeredmessage itself. When you call the .Complete () method, this happens as per the documentation :

Ends the message receiving operation and indicates that the message should be marked as processed and deleted.

Instead, you should save the MyObject type of objects in the dictionary, as broker messages will be destroyed upon completion.

 private Dictionary<long, MyObject> ReceivedMessages; ReceivedMessages = new Dictionary<long, MyObject>(); 

And in the corresponding bit of code:

  var myObj= message.GetBody<MyObject>(); ReceivedMessages.Add(myObj.Id, myObj); 
+2
source

This is very implicit and not listed in the documentation as far as I know, but when you subscribe to an SB Topic / Subscription topic, you can only access the BrokeredMessage object in the OnMessage delegate callback OnMessage , you provide SubscriptionClient.OnMessage .

As soon as the callback returns, BrokeredMessage is located.

If you want to cache the message and mark it as completed / abandoned / invalid, you must cache BrokeredMessage.LockToken along with any other property that needs to be the body of the message and use

  • SubscriptionClient.Complete
  • SubscriptionClient.Abandon
  • SubscriptionClient.Deadletter

which receive a LockToken as a parameter when you want to mark them completed.

+2
source

I found that this is the same problem, if you use the async method, messages are sent, and by the time the wait occurs, the message has already been deleted. I'm sorry I did not find this message before ..

+1
source

Take a look at ServiceBus LockToken.

You can call Complete (lockToken)

0
source

Source: https://habr.com/ru/post/1244588/


All Articles