diff --git a/src/DotXxlJob.Core/DefaultJobHandlerFactory.cs b/src/DotXxlJob.Core/DefaultJobHandlerFactory.cs
index c8ee3d7..c8c670a 100644
--- a/src/DotXxlJob.Core/DefaultJobHandlerFactory.cs
+++ b/src/DotXxlJob.Core/DefaultJobHandlerFactory.cs
@@ -1,10 +1,9 @@
using System;
-using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
namespace DotXxlJob.Core
{
- public class DefaultJobHandlerFactory : IJobHandlerFactory
+ public class DefaultJobHandlerFactory : IJobHandlerFactory
{
private readonly JobHandlerCache _handlerCache;
@@ -22,7 +21,7 @@ namespace DotXxlJob.Core
_handlerCache.AddJobHandler(handler);
}
- if (_handlerCache.HandlersCache.Count < 1)
+ if (_handlerCache.IsEmpty)
{
throw new TypeLoadException("IJobHandlers are not found in IServiceCollection");
}
@@ -40,7 +39,7 @@ namespace DotXxlJob.Core
serviceScope = scopeFactory.CreateScope();
- return (IJobHandler)ActivatorUtilities.CreateInstance(serviceScope.ServiceProvider, jobHandler.JobHandlerType, jobHandler.JobHandlerConstructorParameters);
+ return (IJobHandler)serviceScope.ServiceProvider.GetRequiredService(jobHandler.JobHandlerType);
}
}
}
\ No newline at end of file
diff --git a/src/DotXxlJob.Core/Extensions/ServiceCollectionExtensions.cs b/src/DotXxlJob.Core/Extensions/ServiceCollectionExtensions.cs
index 2f90816..21520e4 100644
--- a/src/DotXxlJob.Core/Extensions/ServiceCollectionExtensions.cs
+++ b/src/DotXxlJob.Core/Extensions/ServiceCollectionExtensions.cs
@@ -56,33 +56,45 @@ namespace DotXxlJob.Core
}
/// 允许创建Scoped实例
- ///
- ///
- /// 用于创建实例的额外参数,比如字符串
- ///
+ public static IServiceCollection AddJobHandler(this IServiceCollection services)
+ where TJob : class, IJobHandler
+ {
+ services.GetJobHandlerCache().AddJobHandler();
+
+ return services.AddScoped();
+ }
+
+ /// 允许创建Scoped实例
public static IServiceCollection AddJobHandler(this IServiceCollection services,
- params object[] constructorParameters) where TJob : IJobHandler
+ Func implementationFactory)
+ where TJob : class, IJobHandler
{
- services.GetJobHandlerCache().AddJobHandler(constructorParameters);
+ services.GetJobHandlerCache().AddJobHandler();
- return services;
+ return services.AddScoped(implementationFactory);
}
/// 允许创建Scoped实例
- ///
- ///
- ///
- /// 用于创建实例的额外参数,比如字符串
- ///
public static IServiceCollection AddJobHandler(this IServiceCollection services,
- string handlerName, params object[] constructorParameters) where TJob : IJobHandler
+ string handlerName)
+ where TJob : class, IJobHandler
{
- services.GetJobHandlerCache().AddJobHandler(handlerName, constructorParameters);
+ services.GetJobHandlerCache().AddJobHandler(handlerName);
- return services;
+ return services.AddScoped();
+ }
+
+ /// 允许创建Scoped实例
+ public static IServiceCollection AddJobHandler(this IServiceCollection services,
+ string handlerName, Func implementationFactory)
+ where TJob : class, IJobHandler
+ {
+ services.GetJobHandlerCache().AddJobHandler(handlerName);
+
+ return services.AddScoped(implementationFactory);
}
- private static JobHandlerCache GetJobHandlerCache(this IServiceCollection services)
+ private static JobHandlerCache GetJobHandlerCache(this IServiceCollection services)
{
var sd = services.FirstOrDefault(x => x.ImplementationInstance is JobHandlerCache);
if (sd != null) return (JobHandlerCache)sd.ImplementationInstance;
diff --git a/src/DotXxlJob.Core/JobHandlerCache.cs b/src/DotXxlJob.Core/JobHandlerCache.cs
index 3af60d1..009f004 100644
--- a/src/DotXxlJob.Core/JobHandlerCache.cs
+++ b/src/DotXxlJob.Core/JobHandlerCache.cs
@@ -1,26 +1,21 @@
using System;
using System.Collections.Generic;
using System.Reflection;
-using System.Runtime.InteropServices;
-using DotXxlJob.Core.DefaultHandlers;
namespace DotXxlJob.Core
{
public class JobHandlerCache
{
- internal Dictionary HandlersCache { get; } = new Dictionary();
+ private readonly Dictionary _handlersCache = new Dictionary();
- public void AddJobHandler(params object[] constructorParameters)
- where TJob : IJobHandler =>
+ public bool IsEmpty => _handlersCache.Count < 1;
+
+ public void AddJobHandler() where TJob : IJobHandler =>
AddJobHandler(typeof(TJob).GetCustomAttribute()?.Name ??
- typeof(TJob).Name, constructorParameters);
+ typeof(TJob).Name);
- public void AddJobHandler(string handlerName, params object[] constructorParameters)
- where TJob : IJobHandler =>
- AddJobHandler(handlerName, new JobHandlerItem {
- JobHandlerType = typeof(TJob),
- JobHandlerConstructorParameters = constructorParameters,
- });
+ public void AddJobHandler(string handlerName) where TJob : IJobHandler =>
+ AddJobHandler(handlerName, new JobHandlerItem { JobHandlerType = typeof(TJob) });
public void AddJobHandler(IJobHandler jobHandler)
{
@@ -39,24 +34,22 @@ namespace DotXxlJob.Core
private void AddJobHandler(string handlerName, JobHandlerItem jobHandler)
{
- if (HandlersCache.ContainsKey(handlerName))
+ if (_handlersCache.ContainsKey(handlerName))
{
throw new ArgumentException($"Same IJobHandler' name: [{handlerName}]", nameof(handlerName));
}
- HandlersCache.Add(handlerName, jobHandler);
+ _handlersCache.Add(handlerName, jobHandler);
}
public JobHandlerItem Get(string handlerName) =>
- HandlersCache.TryGetValue(handlerName, out var item) ? item : null;
+ _handlersCache.TryGetValue(handlerName, out var item) ? item : null;
public class JobHandlerItem
{
public IJobHandler JobHandler { get; set; }
public Type JobHandlerType { get; set; }
-
- public object[] JobHandlerConstructorParameters { get; set; }
}
}
}
diff --git a/tests/DotXxlJob.Core.Tests/BeanTaskExecutorTest.cs b/tests/DotXxlJob.Core.Tests/BeanTaskExecutorTest.cs
index 85502ab..8fc15df 100644
--- a/tests/DotXxlJob.Core.Tests/BeanTaskExecutorTest.cs
+++ b/tests/DotXxlJob.Core.Tests/BeanTaskExecutorTest.cs
@@ -1,14 +1,12 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
using System.Threading.Tasks;
-using DotXxlJob.Core.DefaultHandlers;
using DotXxlJob.Core.Model;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace DotXxlJob.Core.Tests
{
- public class BeanTaskExecutorTest
+ public class BeanTaskExecutorTest
{
[Fact]
public async Task Repeated_Job_Handler()
@@ -21,7 +19,7 @@ namespace DotXxlJob.Core.Tests
var list = new List