@@ -38,8 +38,15 @@ public async Task<string> Store(string? expectedETag, TransactionalStateMetaData |
38 | 38 | { |
39 | 39 | if (this.StateStorage.Etag != expectedETag) |
40 | 40 | throw new ArgumentException(nameof(expectedETag), "Etag does not match"); |
41 | | - var state = stateStorage!.State!; // StateStorage access above initializes the field and ReadStateAsync has populated its state. |
42 | | - state.Metadata = metadata; |
| 41 | + var storedState = stateStorage!.State!; // StateStorage access above initializes the field and ReadStateAsync has populated its state. |
| 42 | + var storedETag = stateStorage.Etag; |
| 43 | + var state = new TransactionalStateRecord<TState> |
| 44 | + { |
| 45 | + CommittedState = storedState.CommittedState, |
| 46 | + CommittedSequenceId = storedState.CommittedSequenceId, |
| 47 | + Metadata = metadata, |
| 48 | + PendingStates = new List<PendingTransactionState<TState>>(storedState.PendingStates), |
| 49 | + }; |
43 | 50 |
|
44 | 51 | var pendinglist = state.PendingStates; |
45 | 52 |
|
@@ -91,8 +98,23 @@ public async Task<string> Store(string? expectedETag, TransactionalStateMetaData |
91 | 98 | } |
92 | 99 | } |
93 | 100 |
|
94 | | - await stateStorage.WriteStateAsync(); |
95 | | - return stateStorage.Etag ?? throw new InvalidOperationException("The grain storage provider did not supply an ETag after writing transactional state."); |
| 101 | + stateStorage.State = state; |
| 102 | + var writeSucceeded = false; |
| 103 | + try |
| 104 | + { |
| 105 | + await stateStorage.WriteStateAsync(); |
| 106 | + var result = stateStorage.Etag ?? throw new InvalidOperationException("The grain storage provider did not supply an ETag after writing transactional state."); |
| 107 | + writeSucceeded = true; |
| 108 | + return result; |
| 109 | + } |
| 110 | + finally |
| 111 | + { |
| 112 | + if (!writeSucceeded) |
| 113 | + { |
| 114 | + stateStorage.State = storedState; |
| 115 | + stateStorage.Etag = storedETag; |
| 116 | + } |
| 117 | + } |
96 | 118 | } |
97 | 119 |
|
98 | 120 | private StateStorageBridge<TransactionalStateRecord<TState>> GetStateStorage() |
|