在 OnOpenAsync 方法中调用 GetOrAddAsync

Call GetOrAddAsync in the OnOpenAsync method

我使用 StatefulServiceIReliableDictionary

目前,我在任何需要这本词典的地方都调用 StateManager.GetOrAddAsync<IReliableDictionary>

StatefulServiceOnOpenAsync 方法中仅调用一次 StateManager.GetOrAddAsync<IReliableDictionary> 并将 return 存储在成员中是最佳做法吗?

没关系。 I've asked it 给产品团队并得到这样的回复:

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).

我插入的斜体文字。

根据官方文档here,不建议存储可靠的集合引用。

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.