diff --git a/backend/src/Squidex.Infrastructure.MongoDb/Assets/MongoGridFsAssetStore.cs b/backend/src/Squidex.Infrastructure.MongoDb/Assets/MongoGridFsAssetStore.cs index 55246eeca..79f45275a 100644 --- a/backend/src/Squidex.Infrastructure.MongoDb/Assets/MongoGridFsAssetStore.cs +++ b/backend/src/Squidex.Infrastructure.MongoDb/Assets/MongoGridFsAssetStore.cs @@ -114,7 +114,7 @@ namespace Squidex.Infrastructure.Assets await bucket.UploadFromStreamAsync(name, name, stream, cancellationToken: ct); } - catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) + catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey) { throw new AssetAlreadyExistsException(fileName); } diff --git a/backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEventStore.cs b/backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEventStore.cs index 92581e357..1cd632a10 100644 --- a/backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEventStore.cs +++ b/backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEventStore.cs @@ -41,7 +41,7 @@ namespace Squidex.Infrastructure.EventSourcing protected override MongoCollectionSettings CollectionSettings() { - return new MongoCollectionSettings { ReadPreference = ReadPreference.Primary, WriteConcern = WriteConcern.WMajority }; + return new MongoCollectionSettings { WriteConcern = WriteConcern.WMajority }; } protected override Task SetupCollectionAsync(IMongoCollection collection, CancellationToken ct = default) diff --git a/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs b/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs index 580843db0..f6e45c1fb 100644 --- a/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs +++ b/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs @@ -40,14 +40,9 @@ namespace Squidex.Infrastructure.MongoDb { await collection.InsertOneAsync(document, null, ct); } - catch (MongoWriteException ex) + catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey) { - if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) - { - return false; - } - - throw; + return false; } return true; @@ -108,24 +103,21 @@ namespace Squidex.Infrastructure.MongoDb await collection.UpdateOneAsync(x => x.Id.Equals(key), update, Upsert); } } - catch (MongoWriteException ex) + catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey) { - if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) - { - var existingVersion = - await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version) - .FirstOrDefaultAsync(); + var existingVersion = + await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version) + .FirstOrDefaultAsync(); - if (existingVersion != null) - { - var versionField = GetVersionField(); + if (existingVersion != null) + { + var versionField = GetVersionField(); - throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex); - } + throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex); } else { - throw; + throw new InconsistentStateException(EtagVersion.Any, oldVersion, ex); } } } @@ -145,24 +137,21 @@ namespace Squidex.Infrastructure.MongoDb await collection.ReplaceOneAsync(x => x.Id.Equals(key), doc, UpsertReplace); } } - catch (MongoWriteException ex) + catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey) { - if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) - { - var existingVersion = - await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version) - .FirstOrDefaultAsync(); + var existingVersion = + await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version) + .FirstOrDefaultAsync(); - if (existingVersion != null) - { - var versionField = GetVersionField(); + if (existingVersion != null) + { + var versionField = GetVersionField(); - throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex); - } + throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex); } else { - throw; + throw new InconsistentStateException(EtagVersion.Any, oldVersion, ex); } } }