From 679a868b424b172acd54adc41185cf197750dcef Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Wed, 19 May 2021 03:13:19 +0200 Subject: [PATCH 1/4] Fixed analyzer errors and messages --- .editorconfig | 8 ++++ .../AsyncLocal/AsyncLocalContext.cs | 2 +- .../Properties/AssemblyInfo.cs | 2 + .../Properties/AssemblyInfo.cs | 3 ++ .../Domain/Models/DbMigrationOperation.cs | 2 +- src/MongODM.Core/Domain/Models/ModelBase.cs | 2 + src/MongODM.Core/Migration/MigrationResult.cs | 4 +- src/MongODM.Core/MongODMConfiguration.cs | 20 ++++++++-- src/MongODM.Core/Properties/AssemblyInfo.cs | 3 ++ .../ProxyModels/AuditableInterceptor.cs | 2 +- .../ProxyModels/ProxyGenerator.cs | 38 +++++++++++++------ .../ProxyModels/ReferenceableInterceptor.cs | 2 +- src/MongODM.Core/ReflectionHelper.cs | 4 +- .../Serialization/Mapping/MemberDependency.cs | 2 +- .../Serialization/Mapping/SchemaRegister.cs | 8 +++- .../Mapping/Schemas/ModelMapsSchema.cs | 2 + .../Mapping/Schemas/ModelMapsSchemaBase.cs | 2 +- .../Schemas/ReferenceModelMapsSchema.cs | 2 + .../Serialization/SemanticVersion.cs | 4 +- .../Serializers/ReferenceSerializer.cs | 25 ++++++++++-- .../ReferenceSerializerConfiguration.cs | 2 + .../Tasks/UpdateDocDependenciesTask.cs | 2 +- src/MongODM.Core/Utility/DbDependencies.cs | 2 + src/MongODM.Core/Utility/FreezableConfig.cs | 16 +++++++- .../AsyncLocalContextHangfireFilter.cs | 2 +- .../Properties/AssemblyInfo.cs | 3 ++ .../ExecutionContextSelectorTests.cs | 4 +- .../MockHelpers/InterceptorMockHelper.cs | 4 +- 28 files changed, 131 insertions(+), 41 deletions(-) create mode 100644 src/MongODM.AspNetCore.UI/Properties/AssemblyInfo.cs create mode 100644 src/MongODM.Core/Properties/AssemblyInfo.cs create mode 100644 src/MongODM.Hangfire/Properties/AssemblyInfo.cs diff --git a/.editorconfig b/.editorconfig index d04ed153..1b9ef798 100644 --- a/.editorconfig +++ b/.editorconfig @@ -16,3 +16,11 @@ indent_size = 2 # C# files [*.cs] +# CA1031: Do not catch general exception types +dotnet_diagnostic.CA1031.severity = none + +# CA1040: Avoid empty interfaces +dotnet_diagnostic.CA1040.severity = none # Sometime they are needed + +# CA1812: Avoid uninstantiated internal classes +dotnet_diagnostic.CA1812.severity = none # Doing extensive use of dependency injection diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs index de765f4d..38b59568 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs @@ -31,7 +31,7 @@ namespace Etherna.ExecContext.AsyncLocal public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext { // Fields. - private static readonly AsyncLocal?> asyncLocalContext = new AsyncLocal?>(); + private static readonly AsyncLocal?> asyncLocalContext = new(); // Properties. public IDictionary? Items => asyncLocalContext.Value; diff --git a/src/ExecutionContext/Properties/AssemblyInfo.cs b/src/ExecutionContext/Properties/AssemblyInfo.cs index 17f09a54..3a8ab07a 100644 --- a/src/ExecutionContext/Properties/AssemblyInfo.cs +++ b/src/ExecutionContext/Properties/AssemblyInfo.cs @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; using System.Runtime.CompilerServices; +[assembly: CLSCompliant(false)] [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] [assembly: InternalsVisibleTo("ExecutionContext.Tests")] diff --git a/src/MongODM.AspNetCore.UI/Properties/AssemblyInfo.cs b/src/MongODM.AspNetCore.UI/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..83cc064e --- /dev/null +++ b/src/MongODM.AspNetCore.UI/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System; + +[assembly: CLSCompliant(false)] \ No newline at end of file diff --git a/src/MongODM.Core/Domain/Models/DbMigrationOperation.cs b/src/MongODM.Core/Domain/Models/DbMigrationOperation.cs index 8c87d821..5c931fba 100644 --- a/src/MongODM.Core/Domain/Models/DbMigrationOperation.cs +++ b/src/MongODM.Core/Domain/Models/DbMigrationOperation.cs @@ -31,7 +31,7 @@ public enum Status } // Fields. - private List _logs = new List(); + private List _logs = new(); // Constructors. public DbMigrationOperation(IDbContext dbContext) diff --git a/src/MongODM.Core/Domain/Models/ModelBase.cs b/src/MongODM.Core/Domain/Models/ModelBase.cs index ebc88ba9..e2e79afb 100644 --- a/src/MongODM.Core/Domain/Models/ModelBase.cs +++ b/src/MongODM.Core/Domain/Models/ModelBase.cs @@ -13,11 +13,13 @@ // limitations under the License. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Etherna.MongODM.Core.Domain.Models { public abstract class ModelBase : IModel { + [SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "This is needed by MongoDB drivers")] public virtual IDictionary? ExtraElements { get; protected set; } } } diff --git a/src/MongODM.Core/Migration/MigrationResult.cs b/src/MongODM.Core/Migration/MigrationResult.cs index 85cc25b8..962b135f 100644 --- a/src/MongODM.Core/Migration/MigrationResult.cs +++ b/src/MongODM.Core/Migration/MigrationResult.cs @@ -25,13 +25,13 @@ private MigrationResult() { } // Methods. public static MigrationResult Failed() => - new MigrationResult + new() { Succeded = false }; public static MigrationResult Succeeded(long migratedDocuments) => - new MigrationResult + new() { Succeded = true, MigratedDocuments = migratedDocuments diff --git a/src/MongODM.Core/MongODMConfiguration.cs b/src/MongODM.Core/MongODMConfiguration.cs index 8a979e76..13380df8 100644 --- a/src/MongODM.Core/MongODMConfiguration.cs +++ b/src/MongODM.Core/MongODMConfiguration.cs @@ -22,16 +22,28 @@ namespace Etherna.MongODM.Core public abstract class MongODMConfiguration : IMongODMConfiguration, IDisposable { // Fields. - private readonly ReaderWriterLockSlim configLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - private readonly List _dbContextTypes = new List(); + private readonly ReaderWriterLockSlim configLock = new(LockRecursionPolicy.SupportsRecursion); + private bool disposed; + private readonly List _dbContextTypes = new(); - // Constructor and dispose. + // Dispose. public void Dispose() { - configLock.Dispose(); + Dispose(true); GC.SuppressFinalize(this); } + protected virtual void Dispose(bool disposing) + { + if (disposed) return; + + // Dispose managed resources. + if (disposing) + configLock.Dispose(); + + disposed = true; + } + // Properties. public IEnumerable DbContextTypes { diff --git a/src/MongODM.Core/Properties/AssemblyInfo.cs b/src/MongODM.Core/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..83cc064e --- /dev/null +++ b/src/MongODM.Core/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System; + +[assembly: CLSCompliant(false)] \ No newline at end of file diff --git a/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs b/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs index 0d00b5ca..a5a6d223 100644 --- a/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs +++ b/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs @@ -25,7 +25,7 @@ public class AuditableInterceptor : ModelInterceptorBase { // Fields. private bool isAuditingEnabled; - private readonly HashSet changedMembers = new HashSet(); + private readonly HashSet changedMembers = new(); // Constructors. public AuditableInterceptor(IEnumerable additionalInterfaces) diff --git a/src/MongODM.Core/ProxyModels/ProxyGenerator.cs b/src/MongODM.Core/ProxyModels/ProxyGenerator.cs index b0316ffc..7cfd148d 100644 --- a/src/MongODM.Core/ProxyModels/ProxyGenerator.cs +++ b/src/MongODM.Core/ProxyModels/ProxyGenerator.cs @@ -24,15 +24,15 @@ namespace Etherna.MongODM.Core.ProxyModels public class ProxyGenerator : IProxyGenerator, IDisposable { // Fields. + private bool disposed; private readonly Castle.DynamicProxy.IProxyGenerator proxyGeneratorCore; private readonly Dictionary InterceptorInstancerSelector)> modelConfigurationDictionary = - new Dictionary InterceptorInstancerSelector)>(); - private readonly ReaderWriterLockSlim modelConfigurationDictionaryLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + (Type[] AdditionalInterfaces, Func InterceptorInstancerSelector)> modelConfigurationDictionary = new(); + private readonly ReaderWriterLockSlim modelConfigurationDictionaryLock = new(LockRecursionPolicy.SupportsRecursion); - private readonly Dictionary proxyTypeDictionary = new Dictionary(); - private readonly ReaderWriterLockSlim proxyTypeDictionaryLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + private readonly Dictionary proxyTypeDictionary = new(); + private readonly ReaderWriterLockSlim proxyTypeDictionaryLock = new(LockRecursionPolicy.SupportsRecursion); // Constructors. public ProxyGenerator( @@ -41,6 +41,27 @@ public ProxyGenerator( this.proxyGeneratorCore = proxyGeneratorCore; } + // Dispose. + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposed) return; + + // Dispose managed resources. + if (disposing) + { + modelConfigurationDictionaryLock.Dispose(); + proxyTypeDictionaryLock.Dispose(); + } + + disposed = true; + } + // Methods. public object CreateInstance( Type type, @@ -133,13 +154,6 @@ public object CreateInstance( public TModel CreateInstance(IDbContext dbContext, params object[] constructorArguments) => (TModel)CreateInstance(typeof(TModel), dbContext, constructorArguments); - public void Dispose() - { - modelConfigurationDictionaryLock.Dispose(); - proxyTypeDictionaryLock.Dispose(); - GC.SuppressFinalize(this); - } - public bool IsProxyType(Type type) { proxyTypeDictionaryLock.EnterReadLock(); diff --git a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs index 1d19e41f..3728f5d3 100644 --- a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs +++ b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs @@ -29,7 +29,7 @@ public class ReferenceableInterceptor : ModelInterceptorBase settedMemberNames = new Dictionary(); // + private readonly Dictionary settedMemberNames = new(); // private readonly IRepository repository; // Constructors. diff --git a/src/MongODM.Core/ReflectionHelper.cs b/src/MongODM.Core/ReflectionHelper.cs index b9869ea7..a20ad33e 100644 --- a/src/MongODM.Core/ReflectionHelper.cs +++ b/src/MongODM.Core/ReflectionHelper.cs @@ -23,8 +23,8 @@ namespace Etherna.MongODM.Core { public static class ReflectionHelper { - private static readonly Dictionary> propertyRegister = new Dictionary>(); - private static readonly ReaderWriterLockSlim propertyRegisterLock = new ReaderWriterLockSlim(); + private static readonly Dictionary> propertyRegister = new(); + private static readonly ReaderWriterLockSlim propertyRegisterLock = new(); public static MemberInfo FindProperty(LambdaExpression lambdaExpression) { diff --git a/src/MongODM.Core/Serialization/Mapping/MemberDependency.cs b/src/MongODM.Core/Serialization/Mapping/MemberDependency.cs index e2115ba7..9584739e 100644 --- a/src/MongODM.Core/Serialization/Mapping/MemberDependency.cs +++ b/src/MongODM.Core/Serialization/Mapping/MemberDependency.cs @@ -132,7 +132,7 @@ public string MemberPathToString() => public override string ToString() { - StringBuilder strBuilder = new StringBuilder(); + StringBuilder strBuilder = new(); strBuilder.AppendLine(FullPathToString()); strBuilder.AppendLine($" modelMapId: {RootModelMap.Id}"); diff --git a/src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs b/src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs index 8908c05b..c767e488 100644 --- a/src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs +++ b/src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs @@ -20,6 +20,7 @@ using MongoDB.Bson.Serialization; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Text; @@ -66,6 +67,7 @@ public ICustomSerializerSchemaBuilder AddCustomSerializerSchema( return modelSchemaConfiguration; }); + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The new model map instance can't be disposed")] public IModelMapsSchemaBuilder AddModelMapsSchema( string activeModelMapId, Action>? activeModelMapInitializer = null, @@ -158,7 +160,7 @@ public IModelMapsSchema GetModelMapsSchema(Type modelType) public override string ToString() { - StringBuilder strBuilder = new StringBuilder(); + StringBuilder strBuilder = new(); // Member dependencies. //memberInfoToMemberMapsDictionary @@ -308,7 +310,9 @@ private void CompileDependencyRegisters( //model maps schema serializers if (memberSerializer is IModelMapsContainerSerializer schemaSerializer) { - var useCascadeDelete = (memberSerializer as IReferenceContainerSerializer)?.UseCascadeDelete; +#pragma warning disable CA1508 // Avoid dead conditional code. Here code analyzer is wrong + bool? useCascadeDelete = (memberSerializer as IReferenceContainerSerializer)?.UseCascadeDelete; +#pragma warning restore CA1508 // Avoid dead conditional code foreach (var childClassMap in schemaSerializer.AllChildClassMaps) CompileDependencyRegisters(modelMap, childClassMap, lastEntityClassMap, currentMemberPath, useCascadeDelete); } diff --git a/src/MongODM.Core/Serialization/Mapping/Schemas/ModelMapsSchema.cs b/src/MongODM.Core/Serialization/Mapping/Schemas/ModelMapsSchema.cs index ff34238f..68a07b4c 100644 --- a/src/MongODM.Core/Serialization/Mapping/Schemas/ModelMapsSchema.cs +++ b/src/MongODM.Core/Serialization/Mapping/Schemas/ModelMapsSchema.cs @@ -14,6 +14,7 @@ using MongoDB.Bson.Serialization; using System; +using System.Diagnostics.CodeAnalysis; namespace Etherna.MongODM.Core.Serialization.Mapping.Schemas { @@ -34,6 +35,7 @@ public IModelMapsSchemaBuilder AddFallbackCustomSerializer(IBsonSerializ return this; } + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The new model map instance can't be disposed")] public IModelMapsSchemaBuilder AddSecondaryMap( string id, Action>? modelMapInitializer = null, diff --git a/src/MongODM.Core/Serialization/Mapping/Schemas/ModelMapsSchemaBase.cs b/src/MongODM.Core/Serialization/Mapping/Schemas/ModelMapsSchemaBase.cs index 96afc265..a208a8cf 100644 --- a/src/MongODM.Core/Serialization/Mapping/Schemas/ModelMapsSchemaBase.cs +++ b/src/MongODM.Core/Serialization/Mapping/Schemas/ModelMapsSchemaBase.cs @@ -23,7 +23,7 @@ abstract class ModelMapsSchemaBase : SchemaBase, IModelMapsSchema { // Fields. private Dictionary _allMapsDictionary = default!; - protected readonly List _secondaryMaps = new List(); + protected readonly List _secondaryMaps = new(); // Constructor. protected ModelMapsSchemaBase( diff --git a/src/MongODM.Core/Serialization/Mapping/Schemas/ReferenceModelMapsSchema.cs b/src/MongODM.Core/Serialization/Mapping/Schemas/ReferenceModelMapsSchema.cs index 225e81a8..3f925241 100644 --- a/src/MongODM.Core/Serialization/Mapping/Schemas/ReferenceModelMapsSchema.cs +++ b/src/MongODM.Core/Serialization/Mapping/Schemas/ReferenceModelMapsSchema.cs @@ -14,6 +14,7 @@ using MongoDB.Bson.Serialization; using System; +using System.Diagnostics.CodeAnalysis; namespace Etherna.MongODM.Core.Serialization.Mapping.Schemas { @@ -33,6 +34,7 @@ public IReferenceModelMapsSchemaBuilder AddFallbackCustomSerializer(IBso return this; } + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The new model map instance can't be disposed")] public IReferenceModelMapsSchemaBuilder AddSecondaryMap( string id, string? baseModelMapId = null, diff --git a/src/MongODM.Core/Serialization/SemanticVersion.cs b/src/MongODM.Core/Serialization/SemanticVersion.cs index 44bb2a04..488fea21 100644 --- a/src/MongODM.Core/Serialization/SemanticVersion.cs +++ b/src/MongODM.Core/Serialization/SemanticVersion.cs @@ -173,6 +173,8 @@ public override string ToString() public static bool operator >=(SemanticVersion x, SemanticVersion y) => y <= x; - public static implicit operator SemanticVersion(string version) => new SemanticVersion(version); + public static implicit operator SemanticVersion(string version) => new(version); + + public static SemanticVersion FromString(string version) => new(version); } } diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs index 07c61071..dd993c27 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs @@ -38,12 +38,13 @@ public class ReferenceSerializer : // Fields. private IDiscriminatorConvention _discriminatorConvention = default!; - private readonly ReaderWriterLockSlim configLockAdapters = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + private readonly ReaderWriterLockSlim configLockAdapters = new(LockRecursionPolicy.SupportsRecursion); private readonly IDbContext dbContext; + private bool disposed; private readonly ReferenceSerializerConfiguration configuration; private readonly IDictionary registeredAdapters = new Dictionary(); - // Constructor and dispose. + // Constructor. public ReferenceSerializer( IDbContext dbContext, Action configure) @@ -58,13 +59,27 @@ public ReferenceSerializer( configuration.Freeze(); } + // Dispose. public void Dispose() { - configLockAdapters.Dispose(); - configuration.Dispose(); + Dispose(true); GC.SuppressFinalize(this); } + protected virtual void Dispose(bool disposing) + { + if (disposed) return; + + // Dispose managed resources. + if (disposing) + { + configLockAdapters.Dispose(); + configuration.Dispose(); + } + + disposed = true; + } + // Properties. public IEnumerable AllChildClassMaps => configuration.Schemas.Values .SelectMany(schema => schema.AllMapsDictionary.Values @@ -138,8 +153,10 @@ public override TModelBase Deserialize(BsonDeserializationContext context, BsonD if (model != null) { var id = model.Id; +#pragma warning disable CA1508 // Avoid dead conditional code if (id == null) //ignore refered instances without id return null!; +#pragma warning restore CA1508 // Avoid dead conditional code // Check if model as been loaded in cache. if (dbContext.DbCache.LoadedModels.ContainsKey(id) && diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerConfiguration.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerConfiguration.cs index eb3a840c..d62fb096 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerConfiguration.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerConfiguration.cs @@ -19,6 +19,7 @@ using MongoDB.Bson.Serialization; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace Etherna.MongODM.Core.Serialization.Serializers @@ -48,6 +49,7 @@ public bool UseCascadeDelete } // Methods. + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The new model map instance can't be disposed")] public IReferenceModelMapsSchemaBuilder AddModelMapsSchema( string activeModelMapId, Action>? activeModelMapInitializer = null, diff --git a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs index 4405070b..da017e79 100644 --- a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs @@ -57,7 +57,7 @@ public async Task RunAsync( var repository = (ICollectionRepository)dbContext.RepositoryRegister.ModelCollectionRepositoryMap[typeof(TModel)]; // Update models. - HashSet upgradedDocumentsId = new HashSet(); + HashSet upgradedDocumentsId = new(); using (serializerModifierAccessor.EnableReferenceSerializerModifier(true)) using (serializerModifierAccessor.EnableCacheSerializerModifier(true)) { diff --git a/src/MongODM.Core/Utility/DbDependencies.cs b/src/MongODM.Core/Utility/DbDependencies.cs index 5ade06b5..ff8e3af5 100644 --- a/src/MongODM.Core/Utility/DbDependencies.cs +++ b/src/MongODM.Core/Utility/DbDependencies.cs @@ -30,7 +30,9 @@ public DbDependencies( ISchemaRegister schemaRegister, ISerializerModifierAccessor serializerModifierAccessor, #pragma warning disable IDE0060 // Remove unused parameter. It's needed for run static configurations +#pragma warning disable CA1801 // Review unused parameters IStaticConfigurationBuilder staticConfigurationBuilder) +#pragma warning restore CA1801 // Review unused parameters #pragma warning restore IDE0060 // Remove unused parameter { DbCache = dbCache; diff --git a/src/MongODM.Core/Utility/FreezableConfig.cs b/src/MongODM.Core/Utility/FreezableConfig.cs index b335a93b..5369c4dc 100644 --- a/src/MongODM.Core/Utility/FreezableConfig.cs +++ b/src/MongODM.Core/Utility/FreezableConfig.cs @@ -20,15 +20,27 @@ namespace Etherna.MongODM.Core.Utility public abstract class FreezableConfig : IFreezableConfig, IDisposable { // Fields. - private readonly ReaderWriterLockSlim configLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + private readonly ReaderWriterLockSlim configLock = new(LockRecursionPolicy.SupportsRecursion); + private bool disposed; // Dispose. public void Dispose() { - configLock.Dispose(); + Dispose(true); GC.SuppressFinalize(this); } + protected virtual void Dispose(bool disposing) + { + if (disposed) return; + + // Dispose managed resources. + if (disposing) + configLock.Dispose(); + + disposed = true; + } + // Properties. public bool IsFrozen { get; private set; } diff --git a/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs index 9191b995..0445c848 100644 --- a/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs +++ b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs @@ -22,7 +22,7 @@ namespace Etherna.MongODM.HF.Filters public class AsyncLocalContextHangfireFilter : IServerFilter { // Fields. - private readonly Dictionary contextHandlers = new Dictionary(); + private readonly Dictionary contextHandlers = new(); private readonly IAsyncLocalContext asyncLocalContext; // Constructors. diff --git a/src/MongODM.Hangfire/Properties/AssemblyInfo.cs b/src/MongODM.Hangfire/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..83cc064e --- /dev/null +++ b/src/MongODM.Hangfire/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System; + +[assembly: CLSCompliant(false)] \ No newline at end of file diff --git a/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs b/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs index 3a6699f3..5310ed92 100644 --- a/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs +++ b/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs @@ -32,8 +32,8 @@ public void ContextSelection( string expectedResult) { // Setup. - Mock context0 = new Mock(); - Mock context1 = new Mock(); + Mock context0 = new(); + Mock context1 = new(); context0.SetupGet(c => c.Items) .Returns(enableContext1 ? new Dictionary { { "val", "0" } } : null); context1.SetupGet(c => c.Items) diff --git a/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs b/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs index e3447bb9..6b5737ad 100644 --- a/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs +++ b/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs @@ -45,7 +45,7 @@ public static class InterceptorMockHelper public static Mock GetExternalPropertyGetInvocationMock( Expression> memberLambda, TProxy? proxyModel = null, - TMember returnValue = default) + TMember? returnValue = default) where TProxy : class { var invocation = GetInvocationMock(proxyModel); @@ -95,7 +95,7 @@ public static class InterceptorMockHelper public static Mock GetPropertyGetInvocationMock( Expression> memberLambda, TProxy? proxyModel = null, - TMember returnValue = default) + TMember? returnValue = default) where TProxy : class { return GetExternalPropertyGetInvocationMock(memberLambda, proxyModel, returnValue!); From 8fe1997f8c7f509083e423955cb006f06c8f0945 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 20 May 2021 00:52:05 +0200 Subject: [PATCH 2/4] Fixed MODM-77 --- src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs b/src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs index c767e488..52eebeb4 100644 --- a/src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs +++ b/src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs @@ -73,16 +73,12 @@ public IModelMapsSchemaBuilder AddModelMapsSchema( Action>? activeModelMapInitializer = null, IBsonSerializer? customSerializer = null) where TModel : class { - // Verify if needs a default serializer. - if (!typeof(TModel).IsAbstract) - customSerializer ??= ModelMap.GetDefaultSerializer(dbContext); - // Create model map. var modelMap = new ModelMap( activeModelMapId, new BsonClassMap(activeModelMapInitializer ?? (cm => cm.AutoMap())), null, - customSerializer); + customSerializer ?? ModelMap.GetDefaultSerializer(dbContext)); return AddModelMapsSchema(modelMap); } From da2110285f771d6a364259b8515fb3de0b9500e9 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 20 May 2021 01:01:27 +0200 Subject: [PATCH 3/4] Fixed MODM-80 --- src/ExecutionContext/ExecutionContext.csproj | 2 +- src/MongODM.AspNetCore.UI/MongODM.AspNetCore.UI.csproj | 2 +- src/MongODM.AspNetCore/MongODM.AspNetCore.csproj | 2 +- src/MongODM.Core/MongODM.Core.csproj | 2 +- src/MongODM.Hangfire/MongODM.Hangfire.csproj | 2 +- src/MongODM/MongODM.csproj | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index 9c483c21..930dd300 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -22,7 +22,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.AspNetCore.UI/MongODM.AspNetCore.UI.csproj b/src/MongODM.AspNetCore.UI/MongODM.AspNetCore.UI.csproj index 5bfc70d0..2289e5f8 100644 --- a/src/MongODM.AspNetCore.UI/MongODM.AspNetCore.UI.csproj +++ b/src/MongODM.AspNetCore.UI/MongODM.AspNetCore.UI.csproj @@ -32,7 +32,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index d7f35519..217d643f 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -30,7 +30,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index dca52aed..2397e128 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -23,7 +23,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index bf1885d4..6e4a6323 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -22,7 +22,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM/MongODM.csproj b/src/MongODM/MongODM.csproj index 4fd087bf..dfde78eb 100644 --- a/src/MongODM/MongODM.csproj +++ b/src/MongODM/MongODM.csproj @@ -26,7 +26,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive From cc73928ed075a5a702a57ad6203adaa1f77acc63 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Fri, 21 May 2021 02:10:18 +0200 Subject: [PATCH 4/4] Fixed MODM-76 --- .../Extensions/HangfireGlobalConfigExtension.cs | 17 +++++++++++++++++ .../Tasks/HangfireTaskRunner.cs | 4 ---- src/MongODM/ServiceCollectionExtensions.cs | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 src/MongODM.Hangfire/Extensions/HangfireGlobalConfigExtension.cs diff --git a/src/MongODM.Hangfire/Extensions/HangfireGlobalConfigExtension.cs b/src/MongODM.Hangfire/Extensions/HangfireGlobalConfigExtension.cs new file mode 100644 index 00000000..7a88ce78 --- /dev/null +++ b/src/MongODM.Hangfire/Extensions/HangfireGlobalConfigExtension.cs @@ -0,0 +1,17 @@ +using Etherna.ExecContext.AsyncLocal; +using Etherna.MongODM.HF.Filters; + +namespace Hangfire +{ + public static class HangfireGlobalConfigExtension + { +#pragma warning disable IDE0060 // Remove unused parameter + public static void UseMongODM(this IGlobalConfiguration config) +#pragma warning restore IDE0060 // Remove unused parameter + { + // Add a default execution context running with any Hangfire task. + // Added because with asyncronous task, unrelated to requestes, there is no an alternative context to use with MongODM. + GlobalJobFilters.Filters.Add(new AsyncLocalContextHangfireFilter(AsyncLocalContext.Instance)); + } + } +} diff --git a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs index dabae0cb..8cfddb88 100644 --- a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs +++ b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs @@ -36,10 +36,6 @@ public HangfireTaskRunner( { this.backgroundJobClient = backgroundJobClient; this.mongODMOptions = mongODMOptions; - - // Add a default execution context running with any Hangfire task. - // Added because with asyncronous task, unrelated to requestes, there is no an alternative context to use with MongODM. - GlobalJobFilters.Filters.Add(new AsyncLocalContextHangfireFilter(AsyncLocalContext.Instance)); } // Methods. diff --git a/src/MongODM/ServiceCollectionExtensions.cs b/src/MongODM/ServiceCollectionExtensions.cs index afa91e21..e55ba23b 100644 --- a/src/MongODM/ServiceCollectionExtensions.cs +++ b/src/MongODM/ServiceCollectionExtensions.cs @@ -70,6 +70,7 @@ private static void AddHangfire( // Add hangfire. services.AddHangfire(options => { + options.UseMongODM(); options.UseMongoStorage(hangfireOptions.ConnectionString, hangfireOptions.StorageOptions); }); }