Call GetOrAddAsync in the OnOpenAsync method

265 Views Asked by At

I use a StatefulService with a IReliableDictionary.

Currently, I call StateManager.GetOrAddAsync<IReliableDictionary> everywhere I need this dictionary.

Is it best practice to call one time only StateManager.GetOrAddAsync<IReliableDictionary> in the OnOpenAsync method of StatefulService and to store the return in a member ?

2

There are 2 best solutions below

0
On BEST ANSWER

It does not matter much. I've asked it to the product team an got this response:

You can cache the result of GetOrAddAsync locally but it doesn't matter since the statemanager does that for you automatically. Some folks think it's easier to keep a local, but I never do because now you have some lifecycle things to deal with (you have a ref to the thing not protected by state manger acquisition locks so you can see some different exceptions, but nothing you wouldn't have to deal with anyway).

Italic text inserted by me.

0
On

As per the official documentation here, it's not recommended to store the reliable collection references.

We don't recommended that you save references to reliable collection instances in class member variables or properties. Special care must be taken to ensure that the reference is set to an instance at all times in the service lifecycle. The Reliable State Manager handles this work for you, and it's optimized for repeat visits.