Quartz.net with Structuremap.

438 views
Skip to first unread message

Dhananjay Suryawanshi

unread,
Apr 8, 2014, 9:53:00 AM4/8/14
to structure...@googlegroups.com
Hi,

I am struggling to inject Qurazt.net into structuremap. 

Could you please provide any sample.

Thanks,
Dhananjay

Kevin Miller

unread,
Apr 8, 2014, 10:23:42 AM4/8/14
to structure...@googlegroups.com
What exactly are you trying to do? 

Likely you want the typical:

For<ISomeInterface>().Use<SomeConcreteType>();

KevM 


--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/a22e07c3-2c50-48d8-9c94-0559f860f170%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gleb Chermennov

unread,
Apr 8, 2014, 11:44:39 AM4/8/14
to structure...@googlegroups.com
I can provide you with complete configuration example which sets up Quartz.net and StructureMap for console application. Will that do?

вторник, 8 апреля 2014 г., 18:23:42 UTC+4 пользователь KevM написал:
What exactly are you trying to do? 

Likely you want the typical:

For<ISomeInterface>().Use<SomeConcreteType>();

KevM 


On Tue, Apr 8, 2014 at 8:53 AM, Dhananjay Suryawanshi <dsurya...@gmail.com> wrote:
Hi,

I am struggling to inject Qurazt.net into structuremap. 

Could you please provide any sample.

Thanks,
Dhananjay

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-users+unsub...@googlegroups.com.

Gleb Chermennov

unread,
Apr 8, 2014, 1:40:55 PM4/8/14
to structure...@googlegroups.com
First step - you need to tell StructureMap how to handle Quartz.net-related types.
Here's how I did it:
public class QuartzRegistry : Registry
    {
        public QuartzRegistry()
        {
            Scan(s =>
            {
                s.TheCallingAssembly();
                s.WithDefaultConventions();
                s.AddAllTypesOf<IJob>().NameBy(c => c.Name);
            });

            SelectConstructor(() => new StdSchedulerFactory());
            For<ISchedulerFactory>().Use<StdSchedulerFactory>();
            For<IScheduler>().Use(ctx => ctx.GetInstance<ISchedulerFactory>().GetScheduler());
        }
    }

    and then pass that to the container:

     public class MyContainer : Container
    {
        public MyContainer()
        {
            Configure(x => x.AddRegistry<CommonRegistry>());
        }
    }
This config should get all your IJob implementations. 
Second - you need to implement IJobFactory via SM and tell your application to find it:
public class StructureMapJobFactory : IJobFactory
    {
        private IContainer container;

        public StructureMapJobFactory()
        {
            container = new MyContainer();
        }

        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            try
            {
                return (IJob)container.GetInstance(bundle.JobDetail.JobType);
            }
            catch (Exception e)
            {
                throw new SchedulerException("Problem instantiating class", e);
            }
        }

        public void ReturnJob(IJob job)
        {
            throw new NotImplementedException();
        }
    }

then you add following lines to your Web or App.config:

<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<quartz>
<add key="quartz.scheduler.jobFactory.type" value="QuartzFacilities.StructureMapJobFactory, QuartzFacilities" />
</quartz>
And you're almost set. Now to the final part.
You add this code to try things out:
class SchedulerProgram
    {
        public static void Main(string[] args)
        {
            var container = new MyContainer();
            var scheduler = container.GetInstance<IScheduler>();
            scheduler.Start();

            ITrigger trigger = TriggerBuilder.Create()
                        .WithDescription("Every 5 seconds")
                        .WithSimpleSchedule(x => x
                            .WithIntervalInSeconds(5)
                            .RepeatForever()
                        )
                        .Build();

            scheduler.ScheduleJob(new JobDetailImpl("myJobDetail", typeof(ConsoleJob)), trigger);

            Console.ReadKey();
        }
    }
ConsoleJob is just trivial class used for testing:
public class ConsoleJob: IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine(DateTime.Now);
        }
    }
Hope that answers your question.

вторник, 8 апреля 2014 г., 17:53:00 UTC+4 пользователь Dhananjay Suryawanshi написал:
Reply all
Reply to author
Forward
0 new messages