|
23 | 23 | using MongoDB.Bson.Serialization;
|
24 | 24 | using MongoDB.Driver.Core.Bindings;
|
25 | 25 | using MongoDB.Driver.Core.Operations;
|
26 |
| -using MongoDB.Shared; |
27 | 26 |
|
28 | 27 | namespace MongoDB.Driver.GridFS
|
29 | 28 | {
|
@@ -374,6 +373,32 @@ protected override void Dispose(bool disposing)
|
374 | 373 | base.Dispose(disposing);
|
375 | 374 | }
|
376 | 375 |
|
| 376 | + private void ExecuteOrSetAbortedOnException(Action action) |
| 377 | + { |
| 378 | + try |
| 379 | + { |
| 380 | + action(); |
| 381 | + } |
| 382 | + catch |
| 383 | + { |
| 384 | + _aborted = true; |
| 385 | + throw; |
| 386 | + } |
| 387 | + } |
| 388 | + |
| 389 | + private async Task ExecuteOrSetAbortedOnExceptionAsync(Func<Task> action) |
| 390 | + { |
| 391 | + try |
| 392 | + { |
| 393 | + await action().ConfigureAwait(false); |
| 394 | + } |
| 395 | + catch |
| 396 | + { |
| 397 | + _aborted = true; |
| 398 | + throw; |
| 399 | + } |
| 400 | + } |
| 401 | + |
377 | 402 | private IMongoCollection<BsonDocument> GetChunksCollection()
|
378 | 403 | {
|
379 | 404 | return GetCollection("chunks");
|
@@ -476,34 +501,38 @@ private void TruncateFinalChunk()
|
476 | 501 | }
|
477 | 502 |
|
478 | 503 | private void WriteBatch(CancellationToken cancellationToken)
|
479 |
| - { |
480 |
| - var chunksCollection = GetChunksCollection(); |
481 |
| - var chunkDocuments = CreateWriteBatchChunkDocuments(); |
482 |
| - chunksCollection.InsertMany(chunkDocuments, cancellationToken: cancellationToken); |
483 |
| - _batch.Clear(); |
484 |
| - } |
| 504 | + => ExecuteOrSetAbortedOnException(() => |
| 505 | + { |
| 506 | + var chunksCollection = GetChunksCollection(); |
| 507 | + var chunkDocuments = CreateWriteBatchChunkDocuments(); |
| 508 | + chunksCollection.InsertMany(chunkDocuments, cancellationToken: cancellationToken); |
| 509 | + _batch.Clear(); |
| 510 | + }); |
485 | 511 |
|
486 |
| - private async Task WriteBatchAsync(CancellationToken cancellationToken) |
487 |
| - { |
488 |
| - var chunksCollection = GetChunksCollection(); |
489 |
| - var chunkDocuments = CreateWriteBatchChunkDocuments(); |
490 |
| - await chunksCollection.InsertManyAsync(chunkDocuments, cancellationToken: cancellationToken).ConfigureAwait(false); |
491 |
| - _batch.Clear(); |
492 |
| - } |
| 512 | + private Task WriteBatchAsync(CancellationToken cancellationToken) |
| 513 | + => ExecuteOrSetAbortedOnExceptionAsync(async () => |
| 514 | + { |
| 515 | + var chunksCollection = GetChunksCollection(); |
| 516 | + var chunkDocuments = CreateWriteBatchChunkDocuments(); |
| 517 | + await chunksCollection.InsertManyAsync(chunkDocuments, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 518 | + _batch.Clear(); |
| 519 | + }); |
493 | 520 |
|
494 | 521 | private void WriteFilesCollectionDocument(CancellationToken cancellationToken)
|
495 |
| - { |
496 |
| - var filesCollection = GetFilesCollection(); |
497 |
| - var filesCollectionDocument = CreateFilesCollectionDocument(); |
498 |
| - filesCollection.InsertOne(filesCollectionDocument, cancellationToken: cancellationToken); |
499 |
| - } |
| 522 | + => ExecuteOrSetAbortedOnException(() => |
| 523 | + { |
| 524 | + var filesCollection = GetFilesCollection(); |
| 525 | + var filesCollectionDocument = CreateFilesCollectionDocument(); |
| 526 | + filesCollection.InsertOne(filesCollectionDocument, cancellationToken: cancellationToken); |
| 527 | + }); |
500 | 528 |
|
501 |
| - private async Task WriteFilesCollectionDocumentAsync(CancellationToken cancellationToken) |
502 |
| - { |
503 |
| - var filesCollection = GetFilesCollection(); |
504 |
| - var filesCollectionDocument = CreateFilesCollectionDocument(); |
505 |
| - await filesCollection.InsertOneAsync(filesCollectionDocument, cancellationToken: cancellationToken).ConfigureAwait(false); |
506 |
| - } |
| 529 | + private Task WriteFilesCollectionDocumentAsync(CancellationToken cancellationToken) |
| 530 | + => ExecuteOrSetAbortedOnExceptionAsync(() => |
| 531 | + { |
| 532 | + var filesCollection = GetFilesCollection(); |
| 533 | + var filesCollectionDocument = CreateFilesCollectionDocument(); |
| 534 | + return filesCollection.InsertOneAsync(filesCollectionDocument, cancellationToken: cancellationToken); |
| 535 | + }); |
507 | 536 |
|
508 | 537 | private void WriteFinalBatch(CancellationToken cancellationToken)
|
509 | 538 | {
|
|
0 commit comments