FluentScheduler 与 Autofac
FluentScheduler with Autofac
我正在尝试在 FluentScheduler 的作业中使用注入对象 (IMessageBus
)。
我的 autofac 设置如下所示:
var thisAssembly = Assembly.GetExecutingAssembly();
var umbracoAssembly = typeof(UmbracoApplication).Assembly;
var builder = new ContainerBuilder();
// Controllers
builder.RegisterControllers(thisAssembly);
builder.RegisterApiControllers(thisAssembly);
// Umbraco related stuff (http://issues.umbraco.org/issue/U4-4181)
builder.RegisterControllers(umbracoAssembly);
builder.RegisterApiControllers(umbracoAssembly);
builder.RegisterType<MessageBus>().As<IMessageBus>();
Container = builder.Build();
我有一个看起来像这样的预定工作:
public class CourseAgentJob : IJob
{
private IMessageBus _bus;
public CourseAgentJob(IMessageBus bus)
{
_bus = bus;
}
public async void Execute()
{
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new Exception("Fire course agent")));
}
}
设置作业 fires/is 时,出现以下错误:
System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstanceT
at FluentScheduler.JobFactory.FluentScheduler.IJobFactory.GetJobInstanceT in A:\GitHub\FluentScheduler\Library\JobFactory.cs:line 25
at FluentScheduler.JobManager.<>c__121.<GetJobAction>b__12_0() in A:\GitHub\FluentScheduler\Library\JobManager.cs:line 66
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at FluentScheduler.JobManager.<>c__DisplayClass43_0.<RunJob>b__0() in A:\GitHub\FluentScheduler\Library\JobManager.cs:line 447
at System.Threading.Tasks.Task.Execute()
---> (Inner Exception #0) System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance[T]()
at FluentScheduler.JobFactory.FluentScheduler.IJobFactory.GetJobInstance[T]() in A:\GitHub\FluentScheduler\Library\JobFactory.cs:line 25
at FluentScheduler.JobManager.<>c__12
1.b__12_0() in A:\GitHub\FluentScheduler\Library\JobManager.cs:line 66
at System.Threading.Tasks.Task.Execute()<---
哪个好像没有正确注册?
DI/IoC没做过多少,有点迷茫
您需要设置 FluentScheduler 才能使用您的 DI 容器,as documented here:
FluentScheduler makes it easy to use your IoC tool of choice to create job instances. Simply implement IJobFactory
.
An example using StructureMap:
using FluentScheduler;
using StructureMap;
public class StructureMapJobFactory : IJobFactory
{
public IJob GetJobInstance<T>() where T : IJob
{
return ObjectFactory.Container.GetInstance<T>();
}
}
public class MyRegistry : Registry
{
public MyRegistry()
{
// Schedule an IJob to run at an interval
Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds();
}
}
Register the new job factory with the JobManager:
protected void Application_Start()
{
JobManager.JobFactory = new StructureMapJobFactory();
JobManager.Initialize(new MyRegistry());
}
目前最好的解决方案是:
Schedule(myDIContainer.Resolve<MyJob>()).ToRunEvery(1).Days().At(13, 55);
我正在尝试在 FluentScheduler 的作业中使用注入对象 (IMessageBus
)。
我的 autofac 设置如下所示:
var thisAssembly = Assembly.GetExecutingAssembly();
var umbracoAssembly = typeof(UmbracoApplication).Assembly;
var builder = new ContainerBuilder();
// Controllers
builder.RegisterControllers(thisAssembly);
builder.RegisterApiControllers(thisAssembly);
// Umbraco related stuff (http://issues.umbraco.org/issue/U4-4181)
builder.RegisterControllers(umbracoAssembly);
builder.RegisterApiControllers(umbracoAssembly);
builder.RegisterType<MessageBus>().As<IMessageBus>();
Container = builder.Build();
我有一个看起来像这样的预定工作:
public class CourseAgentJob : IJob
{
private IMessageBus _bus;
public CourseAgentJob(IMessageBus bus)
{
_bus = bus;
}
public async void Execute()
{
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new Exception("Fire course agent")));
}
}
设置作业 fires/is 时,出现以下错误:
System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstanceT at FluentScheduler.JobFactory.FluentScheduler.IJobFactory.GetJobInstanceT in A:\GitHub\FluentScheduler\Library\JobFactory.cs:line 25 at FluentScheduler.JobManager.<>c__12
1.<GetJobAction>b__12_0() in A:\GitHub\FluentScheduler\Library\JobManager.cs:line 66 at System.Threading.Tasks.Task.Execute() --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) at FluentScheduler.JobManager.<>c__DisplayClass43_0.<RunJob>b__0() in A:\GitHub\FluentScheduler\Library\JobManager.cs:line 447 at System.Threading.Tasks.Task.Execute() ---> (Inner Exception #0) System.MissingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance[T]() at FluentScheduler.JobFactory.FluentScheduler.IJobFactory.GetJobInstance[T]() in A:\GitHub\FluentScheduler\Library\JobFactory.cs:line 25 at FluentScheduler.JobManager.<>c__12
1.b__12_0() in A:\GitHub\FluentScheduler\Library\JobManager.cs:line 66 at System.Threading.Tasks.Task.Execute()<---
哪个好像没有正确注册? DI/IoC没做过多少,有点迷茫
您需要设置 FluentScheduler 才能使用您的 DI 容器,as documented here:
FluentScheduler makes it easy to use your IoC tool of choice to create job instances. Simply implement
IJobFactory
.An example using StructureMap:
using FluentScheduler;
using StructureMap;
public class StructureMapJobFactory : IJobFactory
{
public IJob GetJobInstance<T>() where T : IJob
{
return ObjectFactory.Container.GetInstance<T>();
}
}
public class MyRegistry : Registry
{
public MyRegistry()
{
// Schedule an IJob to run at an interval
Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds();
}
}
Register the new job factory with the JobManager:
protected void Application_Start()
{
JobManager.JobFactory = new StructureMapJobFactory();
JobManager.Initialize(new MyRegistry());
}
目前最好的解决方案是:
Schedule(myDIContainer.Resolve<MyJob>()).ToRunEvery(1).Days().At(13, 55);