diff --git a/samples/ConsoleSample/ConsoleSample.csproj b/samples/ConsoleSample/ConsoleSample.csproj
index fe30de4..104ecf4 100644
--- a/samples/ConsoleSample/ConsoleSample.csproj
+++ b/samples/ConsoleSample/ConsoleSample.csproj
@@ -2,7 +2,7 @@
Exe
- netcoreapp2.0
+ net6.0
diff --git a/samples/MvcSample/MvcSample.csproj b/samples/MvcSample/MvcSample.csproj
index 205a81c..e0d318d 100644
--- a/samples/MvcSample/MvcSample.csproj
+++ b/samples/MvcSample/MvcSample.csproj
@@ -1,20 +1,19 @@
- netcoreapp2.2
+ net6.0
-
-
+
+
-
+
-
-
+
diff --git a/src/Hangfire.LiteDB/Hangfire.LiteDB.csproj b/src/Hangfire.LiteDB/Hangfire.LiteDB.csproj
index 4315492..e296828 100644
--- a/src/Hangfire.LiteDB/Hangfire.LiteDB.csproj
+++ b/src/Hangfire.LiteDB/Hangfire.LiteDB.csproj
@@ -1,16 +1,11 @@
-
- netstandard2.0
-
-
- netstandard2.0;net45
-
0.4.1
codeyu
Hangfire.LiteDB
Hangfire.LiteDB
$(NoWarn);CS0618
+ net6.0
true
true
false
@@ -39,30 +34,9 @@
-
-
-
- $(DefineConstants);NetCore
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
+
\ No newline at end of file
diff --git a/src/Hangfire.LiteDB/HangfireDbContext.cs b/src/Hangfire.LiteDB/HangfireDbContext.cs
index 786e87a..c6efa45 100644
--- a/src/Hangfire.LiteDB/HangfireDbContext.cs
+++ b/src/Hangfire.LiteDB/HangfireDbContext.cs
@@ -36,7 +36,16 @@ public sealed class HangfireDbContext
///
/// Connection string for LiteDB database
/// Collections prefix
- private HangfireDbContext(string connectionString, string prefix = "hangfire")
+ private HangfireDbContext(string connectionString, string prefix = "hangfire") : this(new LiteRepository(connectionString),prefix)
+ {
+ }
+
+ ///
+ /// Starts LiteDB database using a connection string for file system database
+ ///
+ /// Connection string for LiteDB database
+ /// Collections prefix
+ private HangfireDbContext(LiteRepository repository, string prefix = "hangfire")
{
_prefix = prefix;
@@ -58,8 +67,7 @@ private HangfireDbContext(string connectionString, string prefix = "hangfire")
DateFormatString = "yyyy-MM-dd HH:mm:ss.fff"
});
- Repository = new LiteRepository(connectionString);
-
+ Repository = repository;
Database = Repository.Database;
ConnectionId = Guid.NewGuid().ToString();
@@ -82,6 +90,7 @@ private HangfireDbContext(string connectionString, string prefix = "hangfire")
JobQueue.EnsureIndex("Queue");
JobQueue.EnsureIndex("FetchedAt");
}
+
///
///
///
@@ -101,6 +110,25 @@ public static HangfireDbContext Instance(string connectionString, string prefix
return _instance;
}
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static HangfireDbContext Instance(LiteRepository repository, string prefix = "hangfire")
+ {
+ if (_instance != null) return _instance;
+ lock (Locker)
+ {
+ if (_instance == null)
+ {
+ _instance = new HangfireDbContext(repository, prefix);
+ }
+ }
+
+ return _instance;
+ }
///
/// LiteDB database connection identifier
diff --git a/src/Hangfire.LiteDB/LiteDbDistributedLock.cs b/src/Hangfire.LiteDB/LiteDbDistributedLock.cs
index 1a52b40..9a1b144 100644
--- a/src/Hangfire.LiteDB/LiteDbDistributedLock.cs
+++ b/src/Hangfire.LiteDB/LiteDbDistributedLock.cs
@@ -210,10 +210,12 @@ private void Release()
{
try
{
+#pragma warning disable CA1416 // Validate platform compatibility
if (EventWaitHandle.TryOpenExisting(EventWaitHandleName, out EventWaitHandle eventWaitHandler))
{
eventWaitHandler.Set();
}
+#pragma warning restore CA1416 // Validate platform compatibility
}
catch (PlatformNotSupportedException)
{
diff --git a/src/Hangfire.LiteDB/LiteDbStorage.cs b/src/Hangfire.LiteDB/LiteDbStorage.cs
index ed5816e..95f57d3 100644
--- a/src/Hangfire.LiteDB/LiteDbStorage.cs
+++ b/src/Hangfire.LiteDB/LiteDbStorage.cs
@@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using Hangfire.Annotations;
using Hangfire.Logging;
using Hangfire.Server;
using Hangfire.States;
using Hangfire.Storage;
+using LiteDB;
+
namespace Hangfire.LiteDB
{
///
@@ -11,17 +15,39 @@ namespace Hangfire.LiteDB
///
public class LiteDbStorage : JobStorage
{
- private readonly string _connectionString;
-
private readonly LiteDbStorageOptions _storageOptions;
///
/// Constructs Job Storage by database connection string
///
/// LiteDB connection string
- public LiteDbStorage(string connectionString)
- : this(connectionString, new LiteDbStorageOptions())
+ public LiteDbStorage(string connectionString) : this(connectionString, new LiteDbStorageOptions())
+ {
+ }
+
+ ///
+ /// Constructs Job Storage by database connection string
+ ///
+ /// LiteDB connection string
+ public LiteDbStorage(LiteRepository liteDatabase) : this(HangfireDbContext.Instance(liteDatabase), new LiteDbStorageOptions())
+ {
+ if (liteDatabase == null)
+ {
+ throw new ArgumentNullException(nameof(liteDatabase));
+ }
+ }
+
+ ///
+ /// Constructs Job Storage by database connection string
+ ///
+ /// LiteDB connection string
+ /// /// LiteDB connection string
+ public LiteDbStorage(LiteRepository liteDatabase, LiteDbStorageOptions options) : this(HangfireDbContext.Instance(liteDatabase), options)
{
+ if (liteDatabase == null)
+ {
+ throw new ArgumentNullException(nameof(liteDatabase));
+ }
}
///
@@ -29,17 +55,28 @@ public LiteDbStorage(string connectionString)
///
/// LiteDB connection string
/// Storage options
- public LiteDbStorage(string connectionString, LiteDbStorageOptions storageOptions)
+ public LiteDbStorage(string connectionString, [AllowNull] LiteDbStorageOptions storageOptions) : this(HangfireDbContext.Instance(connectionString, storageOptions?.Prefix ?? new LiteDbStorageOptions().Prefix),storageOptions)
{
if (string.IsNullOrWhiteSpace(connectionString))
{
throw new ArgumentNullException(nameof(connectionString));
}
+ }
+
+
+ ///
+ /// Constructs Job Storage by database connection string and options
+ ///
+ /// LiteDB connection string
+ /// Storage options
+ private LiteDbStorage(HangfireDbContext connection, LiteDbStorageOptions storageOptions)
+ {
+ if (storageOptions == null)
+ storageOptions = new LiteDbStorageOptions();
- _connectionString = connectionString;
- _storageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));
+ _storageOptions = storageOptions;
- Connection = HangfireDbContext.Instance(connectionString, storageOptions.Prefix);
+ Connection = connection;
Connection.Init(_storageOptions);
var defaultQueueProvider = new LiteDbJobQueueProvider(_storageOptions);
QueueProviders = new PersistentJobQueueProviderCollection(defaultQueueProvider);
@@ -89,9 +126,7 @@ public override void WriteOptionsToLog(ILog logger)
/// Database context
public HangfireDbContext CreateAndOpenConnection()
{
- return _connectionString != null
- ? HangfireDbContext.Instance(_connectionString, _storageOptions.Prefix)
- : null;
+ return Connection;
}
///
@@ -99,7 +134,7 @@ public HangfireDbContext CreateAndOpenConnection()
///
public override string ToString()
{
- return $"Connection string: {_connectionString}, prefix: {_storageOptions.Prefix}";
+ return $"prefix: {_storageOptions.Prefix}";
}
///
diff --git a/src/Hangfire.LiteDB/LiteDbStorageExtensions.cs b/src/Hangfire.LiteDB/LiteDbStorageExtensions.cs
index 2142975..af696c6 100644
--- a/src/Hangfire.LiteDB/LiteDbStorageExtensions.cs
+++ b/src/Hangfire.LiteDB/LiteDbStorageExtensions.cs
@@ -1,5 +1,6 @@
using System;
using Hangfire.Annotations;
+using LiteDB;
namespace Hangfire.LiteDB
{
@@ -41,5 +42,24 @@ public static IGlobalConfiguration UseLiteDbStorage(
var storage = new LiteDbStorage(nameOrConnectionString, options);
return configuration.UseStorage(storage);
}
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IGlobalConfiguration UseLiteDbStorage(
+ [NotNull] this IGlobalConfiguration configuration,
+ [NotNull] LiteDatabase database)
+ {
+ if (configuration == null) throw new ArgumentNullException(nameof(configuration));
+ if (database == null) throw new ArgumentNullException(nameof(database));
+
+ var repo = new LiteRepository(database);
+ var storage = new LiteDbStorage(repo);
+ return configuration.UseStorage(storage);
+ }
}
}
\ No newline at end of file
diff --git a/test/Hangfire.LiteDB.Test/Hangfire.LiteDB.Test.csproj b/test/Hangfire.LiteDB.Test/Hangfire.LiteDB.Test.csproj
index 28906ac..be0235e 100644
--- a/test/Hangfire.LiteDB.Test/Hangfire.LiteDB.Test.csproj
+++ b/test/Hangfire.LiteDB.Test/Hangfire.LiteDB.Test.csproj
@@ -1,16 +1,16 @@
- netcoreapp2.0
+ net6.0
false
bin\Debug\netcoreapp2.0\Hangfire.LiteDB.Test.xml
-
-
-
-
+
+
+
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
diff --git a/test/Hangfire.LiteDB.Test/LiteDbStorageFacts.cs b/test/Hangfire.LiteDB.Test/LiteDbStorageFacts.cs
index 5613c15..40b79d4 100644
--- a/test/Hangfire.LiteDB.Test/LiteDbStorageFacts.cs
+++ b/test/Hangfire.LiteDB.Test/LiteDbStorageFacts.cs
@@ -25,14 +25,14 @@ public void Ctor_ThrowsAnException_WhenConnectionStringIsEmpty()
//
// Assert.Equal("databaseName", exception.ParamName);
//}
-
+ /*
[Fact]
public void Ctor_ThrowsAnException_WhenStorageOptionsValueIsNull()
{
var exception = Assert.Throws(() => new LiteDbStorage("lite.db", null));
Assert.Equal("storageOptions", exception.ParamName);
- }
+ }*/
[Fact, CleanDatabase]
public void GetMonitoringApi_ReturnsNonNullInstance()