I have created a servicebus topic subscription using SAS token. The SAS token expires after 5mins.
I have registered a function to be called on message and another one on exception.var messageOptions = new OnMessageOptions();
messageOptions.ExceptionReceived += onMessageException;
After SAS expiry the exception function is calling repeatedly. I want this to be called only once for recreating the subscription using new SAS token. Please help in resolving this issue.
I did not find any documentation on net that talks about using this event driven model for receiving messages when using SAS tokens. Any help is appreciated.
OnMessage receive approacv (event driven as you named it), ha no any dependency to SAS.
It internally receives messages on multiple threads, .., catches exceptions and in an error case simply invoke 'ExceptionReceived' callback. There is no any other magic behind that, which is not documented.
You issue is conceptual one. You have decided to use short living SAS tokens, but in the same time you have chosen to use long term receiving approach which is 'OnMessage'. This does not fit together.
To work around this there are two ways (in general):
1. You use client.Receive for less than 5 min.
2. Yo use OnMessage with very long token time. For example 'forever'.
Assuming that you still want to use OnMessage with short living tokens (for some unknown reason), you will have to explicitly catch Token Expiration error, recreate token and then restart receiving. This is also a valid solution, but it introduces new problems. For example, which is permitted to recreate token? If the application is permitted for this, then you can use root connection string without of need to use SAS at all.
If this is not the case, the you will need an additional service called SAS Token service, which will crete tokens fro your receiver.
Hope this helps.