@@ -98,103 +98,126 @@ private async ValueTask<IWriteResult> AppendToStreamInternal(
98
98
EventStoreClientOperationOptions operationOptions ,
99
99
TimeSpan ? deadline ,
100
100
UserCredentials ? userCredentials ,
101
- CancellationToken cancellationToken ) {
101
+ CancellationToken cancellationToken
102
+ ) {
103
+ using var call = new Streams . Streams . StreamsClient ( callInvoker ) . Append (
104
+ EventStoreCallOptions . CreateNonStreaming ( Settings , deadline , userCredentials , cancellationToken )
105
+ ) ;
102
106
103
- using var call = new Streams . Streams . StreamsClient (
104
- callInvoker ) . Append ( EventStoreCallOptions . CreateNonStreaming (
105
- Settings , deadline , userCredentials , cancellationToken ) ) ;
107
+ await call . RequestStream . WriteAsync ( header ) . ConfigureAwait ( false ) ;
106
108
107
- IWriteResult writeResult ;
108
- try {
109
- await call . RequestStream . WriteAsync ( header ) . ConfigureAwait ( false ) ;
110
-
111
- foreach ( var e in eventData ) {
112
- _log . LogTrace ( "Appending event to stream - {streamName}@{eventId} {eventType}." ,
113
- header . Options . StreamIdentifier , e . EventId , e . Type ) ;
114
- await call . RequestStream . WriteAsync ( new AppendReq {
109
+ foreach ( var e in eventData ) {
110
+ await call . RequestStream . WriteAsync (
111
+ new AppendReq {
115
112
ProposedMessage = new AppendReq . Types . ProposedMessage {
116
113
Id = e . EventId . ToDto ( ) ,
117
114
Data = ByteString . CopyFrom ( e . Data . Span ) ,
118
115
CustomMetadata = ByteString . CopyFrom ( e . Metadata . Span ) ,
119
116
Metadata = {
120
- { Constants . Metadata . Type , e . Type } ,
121
- { Constants . Metadata . ContentType , e . ContentType }
117
+ { Constants . Metadata . Type , e . Type } ,
118
+ { Constants . Metadata . ContentType , e . ContentType }
122
119
}
123
- }
124
- } ) . ConfigureAwait ( false ) ;
125
- }
126
- } finally {
127
- await call . RequestStream . CompleteAsync ( ) . ConfigureAwait ( false ) ;
120
+ } ,
121
+ }
122
+ ) . ConfigureAwait ( false ) ;
123
+ }
128
124
129
- var response = await call . ResponseAsync . ConfigureAwait ( false ) ;
130
-
131
- if ( response . Success != null ) {
132
- writeResult = new SuccessResult ( response . Success . CurrentRevisionOptionCase ==
133
- AppendResp . Types . Success . CurrentRevisionOptionOneofCase . NoStream
134
- ? StreamRevision . None
135
- : new StreamRevision ( response . Success . CurrentRevision ) ,
136
- response . Success . PositionOptionCase == AppendResp . Types . Success . PositionOptionOneofCase . Position
137
- ? new Position ( response . Success . Position . CommitPosition ,
138
- response . Success . Position . PreparePosition )
139
- : default ) ;
140
- _log . LogDebug ( "Append to stream succeeded - {streamName}@{logPosition}/{nextExpectedVersion}." ,
141
- header . Options . StreamIdentifier , writeResult . LogPosition , writeResult . NextExpectedStreamRevision ) ;
142
- } else {
143
- if ( response . WrongExpectedVersion != null ) {
144
- var actualStreamRevision = response . WrongExpectedVersion . CurrentRevisionOptionCase switch {
145
- AppendResp . Types . WrongExpectedVersion . CurrentRevisionOptionOneofCase . CurrentNoStream =>
146
- StreamRevision . None ,
147
- _ => new StreamRevision ( response . WrongExpectedVersion . CurrentRevision )
148
- } ;
125
+ await call . RequestStream . CompleteAsync ( ) . ConfigureAwait ( false ) ;
149
126
150
- _log . LogDebug (
151
- "Append to stream failed with Wrong Expected Version - {streamName}/{expectedRevision}/{currentRevision}" ,
152
- header . Options . StreamIdentifier , new StreamRevision ( header . Options . Revision ) ,
153
- actualStreamRevision ) ;
154
-
155
- if ( operationOptions . ThrowOnAppendFailure ) {
156
- if ( response . WrongExpectedVersion . ExpectedRevisionOptionCase == AppendResp . Types
157
- . WrongExpectedVersion . ExpectedRevisionOptionOneofCase . ExpectedRevision ) {
158
- throw new WrongExpectedVersionException ( header . Options . StreamIdentifier ! ,
159
- new StreamRevision ( response . WrongExpectedVersion . ExpectedRevision ) ,
160
- actualStreamRevision ) ;
161
- }
127
+ var response = await call . ResponseAsync . ConfigureAwait ( false ) ;
162
128
163
- var expectedStreamState = response . WrongExpectedVersion . ExpectedRevisionOptionCase switch {
164
- AppendResp . Types . WrongExpectedVersion . ExpectedRevisionOptionOneofCase . ExpectedAny =>
165
- StreamState . Any ,
166
- AppendResp . Types . WrongExpectedVersion . ExpectedRevisionOptionOneofCase . ExpectedNoStream =>
167
- StreamState . NoStream ,
168
- AppendResp . Types . WrongExpectedVersion . ExpectedRevisionOptionOneofCase . ExpectedStreamExists =>
169
- StreamState . StreamExists ,
170
- _ => StreamState . Any
171
- } ;
172
-
173
- throw new WrongExpectedVersionException ( header . Options . StreamIdentifier ! ,
174
- expectedStreamState , actualStreamRevision ) ;
175
- }
129
+ if ( response . Success != null )
130
+ return HandleSuccessAppend ( response , header ) ;
176
131
177
- if ( response . WrongExpectedVersion . ExpectedRevisionOptionCase == AppendResp . Types
178
- . WrongExpectedVersion . ExpectedRevisionOptionOneofCase . ExpectedRevision ) {
179
- writeResult = new WrongExpectedVersionResult ( header . Options . StreamIdentifier ! ,
180
- new StreamRevision ( response . WrongExpectedVersion . ExpectedRevision ) ,
181
- actualStreamRevision ) ;
182
- } else {
183
- writeResult = new WrongExpectedVersionResult ( header . Options . StreamIdentifier ! ,
184
- StreamRevision . None ,
185
- actualStreamRevision ) ;
186
- }
132
+ if ( response . WrongExpectedVersion == null )
133
+ throw new InvalidOperationException ( "The operation completed with an unexpected result." ) ;
187
134
188
- } else {
189
- throw new InvalidOperationException ( "The operation completed with an unexpected result." ) ;
190
- }
135
+ return HandleWrongExpectedRevision ( response , header , operationOptions ) ;
136
+ }
137
+
138
+ private IWriteResult HandleSuccessAppend ( AppendResp response , AppendReq header ) {
139
+ var currentRevision = response . Success . CurrentRevisionOptionCase ==
140
+ AppendResp . Types . Success . CurrentRevisionOptionOneofCase . NoStream
141
+ ? StreamRevision . None
142
+ : new StreamRevision ( response . Success . CurrentRevision ) ;
143
+
144
+ var position = response . Success . PositionOptionCase ==
145
+ AppendResp . Types . Success . PositionOptionOneofCase . Position
146
+ ? new Position ( response . Success . Position . CommitPosition , response . Success . Position . PreparePosition )
147
+ : default ;
148
+
149
+ _log . LogDebug (
150
+ "Append to stream succeeded - {streamName}@{logPosition}/{nextExpectedVersion}." ,
151
+ header . Options . StreamIdentifier ,
152
+ position ,
153
+ currentRevision ) ;
154
+
155
+ return new SuccessResult ( currentRevision , position ) ;
156
+ }
157
+
158
+ private IWriteResult HandleWrongExpectedRevision (
159
+ AppendResp response , AppendReq header , EventStoreClientOperationOptions operationOptions
160
+ ) {
161
+ var actualStreamRevision =
162
+ response . WrongExpectedVersion . CurrentRevisionOptionCase switch {
163
+ AppendResp . Types . WrongExpectedVersion . CurrentRevisionOptionOneofCase
164
+ . CurrentNoStream =>
165
+ StreamRevision . None ,
166
+ _ => new StreamRevision ( response . WrongExpectedVersion . CurrentRevision )
167
+ } ;
168
+
169
+ _log . LogDebug (
170
+ "Append to stream failed with Wrong Expected Version - {streamName}/{expectedRevision}/{currentRevision}" ,
171
+ header . Options . StreamIdentifier ,
172
+ new StreamRevision ( header . Options . Revision ) ,
173
+ actualStreamRevision
174
+ ) ;
175
+
176
+ if ( operationOptions . ThrowOnAppendFailure ) {
177
+ if ( response . WrongExpectedVersion . ExpectedRevisionOptionCase == AppendResp . Types
178
+ . WrongExpectedVersion . ExpectedRevisionOptionOneofCase
179
+ . ExpectedRevision ) {
180
+ throw new WrongExpectedVersionException (
181
+ header . Options . StreamIdentifier ! ,
182
+ new StreamRevision ( response . WrongExpectedVersion . ExpectedRevision ) ,
183
+ actualStreamRevision
184
+ ) ;
191
185
}
186
+
187
+ var expectedStreamState =
188
+ response . WrongExpectedVersion . ExpectedRevisionOptionCase switch {
189
+ AppendResp . Types . WrongExpectedVersion . ExpectedRevisionOptionOneofCase
190
+ . ExpectedAny =>
191
+ StreamState . Any ,
192
+ AppendResp . Types . WrongExpectedVersion . ExpectedRevisionOptionOneofCase
193
+ . ExpectedNoStream =>
194
+ StreamState . NoStream ,
195
+ AppendResp . Types . WrongExpectedVersion . ExpectedRevisionOptionOneofCase
196
+ . ExpectedStreamExists =>
197
+ StreamState . StreamExists ,
198
+ _ => StreamState . Any
199
+ } ;
200
+
201
+ throw new WrongExpectedVersionException (
202
+ header . Options . StreamIdentifier ! ,
203
+ expectedStreamState ,
204
+ actualStreamRevision
205
+ ) ;
192
206
}
193
207
194
- return writeResult ;
208
+ var expectedRevision = response . WrongExpectedVersion . ExpectedRevisionOptionCase
209
+ == AppendResp . Types . WrongExpectedVersion . ExpectedRevisionOptionOneofCase
210
+ . ExpectedRevision
211
+ ? new StreamRevision ( response . WrongExpectedVersion . ExpectedRevision )
212
+ : StreamRevision . None ;
213
+
214
+ return new WrongExpectedVersionResult (
215
+ header . Options . StreamIdentifier ! ,
216
+ expectedRevision ,
217
+ actualStreamRevision
218
+ ) ;
195
219
}
196
220
197
-
198
221
private class StreamAppender : IDisposable {
199
222
private readonly EventStoreClientSettings _settings ;
200
223
private readonly CancellationToken _cancellationToken ;
@@ -355,4 +378,4 @@ private static async IAsyncEnumerable<T> ReadAllAsync<T>(ChannelReader<T> reader
355
378
#endif
356
379
}
357
380
}
358
- }
381
+ }
0 commit comments