asp.net-mvc-3 – 如何创建Quartz.NET的工作,需要使用autofac进行注入

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 如何创建Quartz.NET的工作,需要使用autofac进行注入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让Quartz.net(2.1.2)使用IoC容器(autofac),因为我需要在计划的作业中使用服务.我已经找到了关于这个主题的类似帖子,但是我似乎找不到具有自动填充的特定注册示例.

以下帖子涉及我遇到的相同问题:

How to schedule task using Quartz.net 2.0?

但是,我相信我失踪的部分是当答案说“不要忘记在IoC容器中注册这个工作”.我不确定如何做到这一点,因为迄今为止所尝试的一切都没有奏效.

在下面的示例中,“HelloJob”将会运行,但是当我尝试将releaseService注入到“ReleaseJob”中时,它将拒绝运行.

更新:
我在DependencyRegistration.cs部分中标记了我认为问题的代码.

更新2:
一些与我需要做的和可能有帮助的相关链接(我已经通过它们,但仍然无法弄清楚如何使用autofac)

如何在PRO中使用Quartz.NET?
http://blog.goyello.com/2009/09/21/how-to-use-quartz-net-in-pro-way/

Autofac和Quartz.NET
http://blog.humann.info/post/2013/01/30/Autofac-and-QuartzNET.aspx

Quartz.NET和简单注射器的构造器注入
Constructor injection with Quartz.NET and Simple Injector

ASP.Net MVC 3,Ninject和Quartz.Net – 如何?
ASP.Net MVC 3,Ninject and Quartz.Net – How to?

以下是相关代码

Global.asax中

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        var dependencyRegistration = new DependencyRegistration();
        dependencyRegistration.Register();

        ModelValidatorProviders.Providers.Clear();
        ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new ValidatorFactory()));

        DataAnnotationsModelValidatorProvider.AddImplicitrequiredAttributeForValueTypes = false;
    }

DependencyRegistration.cs

public class DependencyRegistration
{
    public void Register()
    {
        var builder = new ContainerBuilder();

        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());

        // Validation
        builder.RegisterType<ValidatorFactory>()
            .As<IValidatorFactory>()
            .InstancePerHttpRequest();

        AssemblyScanner findValidatorsInAssembly = AssemblyScanner.FindValidatorsInAssembly(Assembly.GetExecutingAssembly());
        foreach (AssemblyScanner.AssemblyScanResult item in findValidatorsInAssembly)
        {
            builder.RegisterType(item.ValidatorType)
                .As(item.InterfaceType)
                .InstancePerHttpRequest();
        }

        // Schedule
        builder.Register(x => new StdSchedulerFactory().GetScheduler()).As<IScheduler>();

        // Schedule jobs
        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(x => typeof(IJob).IsAssignableFrom(x));

        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        //Schedule
        IScheduler sched = container.Resolve<IScheduler>();
        sched.JobFactory = new AutofacJobFactory(container);
        sched.Start();

        IJobDetail job = JobBuilder.Create<ReleaseJob>()
                .WithIdentity("1Job")
                .Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("1JobTrigger")
            .WithSimpleSchedule(x => x
                .RepeatForever()
                .WithIntervalInSeconds(5)
            )
            .StartNow()
            .Build();

        sched.ScheduleJob(job,trigger);

        job = JobBuilder.Create<HelloJob>()
               .WithIdentity("2Job")
               .Build();

        trigger = TriggerBuilder.Create()
            .WithIdentity("2JobTrigger")
            .WithSimpleSchedule(x => x
                .RepeatForever()
                .WithIntervalInSeconds(5)
            )
            .StartNow()
            .Build();

        sched.ScheduleJob(job,trigger);
    }
}

JobFactory.cs

public class AutofacJobFactory : IJobFactory
{
    private readonly IContainer _container;

    public AutofacJobFactory(IContainer container)
    {
        _container = container;
    }

    public IJob NewJob(TriggerFiredBundle bundle,IScheduler scheduler)
    {
        return (IJob)_container.Resolve(bundle.JobDetail.JobType);
    }

    public void ReturnJob(IJob job)
    {
    }
}

ReleaseJob.cs

public class ReleaseJob : IJob
{
    private readonly IReleaseService _releaseService;

    public ReleaseJob(IReleaseService releaseService)
    {
        this._releaseService = releaseService;
    }

    public void Execute(IJobExecutionContext context)
    {
        Debug.WriteLine("Release running at " + DateTime.Now.ToString());
    }
}

public class HelloJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Debug.WriteLine("Hello job at " + DateTime.Now.ToString());
    }
}

ReleaseServiceModel.cs

public class ReleaseServiceModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<ReleaseService>()
            .As<IReleaseService>()
            .InstancePerLifetimeScope();
    }
}

解决方法

我终于找到了问题.

我的发布服务正在使用正在使用不同范围创建的数据存储库.

我发现这是通过创建一个新的测试服务,除了返回一个字符串之外什么都没有,而且工作正在注入一个石英作业.

在发现这一点后,我更改了发布服务所要求的存储库的范围,然后发布服务开始在石英作业中工作.

我向所有看过这个问题的人道歉,试图帮助我.因为发布服务的代码没有列出,所以很难弄清楚出了什么问题.

我已经更新了代码与最后的工作绑定我用于石英与自动填充.

原文链接:https://www.f2er.com/aspnet/249602.html

猜你在找的asp.Net相关文章