首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有Autofac的FluentScheduler

带有Autofac的FluentScheduler
EN

Stack Overflow用户
提问于 2016-12-13 12:38:28
回答 2查看 2.1K关注 0票数 1

我试图在IMessageBus中的作业中使用注入对象( injected,FluentScheduler )。

我的autofac设置如下所示:

代码语言:javascript
复制
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();

我有一份安排好的工作看起来是这样的

代码语言:javascript
复制
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")));
    }
}

当我的作业启动/安装时,我会得到以下错误:

System.AggregateException:发生了一个或多个错误。-> System.MissingMethodException:没有为该对象定义无参数构造函数。在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型下,布尔publicOnly、布尔noCheck、布尔& canBeCached、RuntimeMethodHandleInternal& ctor、布尔& bNeedSecurityCheck)在System.RuntimeType.CreateInstanceSlow(布尔publicOnly、布尔skipCheckThis、布尔fillCache )FluentScheduler.JobFactory.FluentScheduler.IJobFactory.GetJobInstanceT at 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__121.b__12_0() in A:\GitHub\FluentScheduler\Library\JobManager.cs:line 66 at System.Threading.Tasks.Task.Execute()<--

看上去没有正确注册吗?我以前没有做过很多DI/IoC课程,所以我有点迷路了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-13 12:49:44

您需要设置FluentScheduler才能使用DI容器,如这里所记载的

FluentScheduler可以方便地使用您选择的IoC工具来创建作业实例。只需实现IJobFactory。 使用StructureMap的示例:

代码语言:javascript
复制
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();
    }
} 

向JobManager注册新的作业工厂:

代码语言:javascript
复制
protected void Application_Start()
{
    JobManager.JobFactory = new StructureMapJobFactory();
    JobManager.Initialize(new MyRegistry()); 
}
票数 1
EN

Stack Overflow用户

发布于 2018-05-28 12:36:29

目前最好的解决办法是:

代码语言:javascript
复制
Schedule(myDIContainer.Resolve<MyJob>()).ToRunEvery(1).Days().At(13, 55);

这是个封闭的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41121351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档