Method TryCreateStateAsync
- Namespace
- IntentSharp.Documents
- Assembly
- IntentSharp.dll
TryCreateStateAsync(string, JsonNode, CancellationToken)
Creates a state value only when the key has none: of several writers, in one process or several, that create the same key at once, exactly one stores its value. Returns the entry as written, with its etag; null when the key already has a value, and then nothing was written. Read the key to see the value that is there.
public ValueTask<StateEntry?> TryCreateStateAsync(string key, JsonNode value, CancellationToken ct = default)
Parameters
keystringThe state key.
valueJsonNodeThe value to store; the store keeps a copy.
ctCancellationTokenCancels the call.
Returns
- ValueTask<StateEntry>
Remarks
The default implementation reads the key with ReadStateAsync(string, CancellationToken) and, when it has no value, writes it with WriteStateAsync(string, JsonNode, string?, CancellationToken) and no expected etag. That is not atomic: two writers that read at once can both create, and the later write replaces the earlier one. A store that several clients or processes share overrides this member with a create that is atomic in its storage; InMemoryDocumentStore and FolderDocumentStore do.
Exceptions
- ArgumentException
keyis null or blank.- ArgumentNullException
valueis null.- ObjectDisposedException
The store is disposed.
- IOException
The state could not be written, or the lock file stayed held by another instance for LockTimeout.
- InvalidOperationException
A document validator called it while this store was checking an edit (see the class remarks).