Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5932551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:50:59+00:00 2026-05-22T14:50:59+00:00

I need to call something in my application start to start my quartz.net scheduler.

  • 0

I need to call something in my application start to start my quartz.net scheduler.

The problem is that I need to pass in a repository into my service layer that normally is done with ninject and dependency injection.

//global.aspx

 public class MvcApplication : System.Web.HttpApplication
    {
        private readonly IScheduledRemindersService scheduledRemindersService;

        public MvcApplication(IScheduledRemindersService 
            scheduledRemindersService)
        {
            this.scheduledRemindersService = scheduledRemindersService;
        }

        protected void Application_Start()
        {
           //other default stuff here like mvc routers.
            scheduledRemindersService.RemindersSchedule();

        }
 }

    private readonly IReminderRepo reminderRepo;
    public ScheduledRemindersService(IReminderRepo reminderRepo)
    {
        this.reminderRepo  = reminderRepo;
    }


private readonly IReminderRepo reminderRepo;


    public ScheduledRemindersService(IReminderRepo reminderRepo)
    {
        this.reminderRepo = reminderRepo;
    }

I have NHibernate set to so when It seems IReminderRepo it shoudl bind it and in IReminderRepo I have

private readonly ISession session;

public ReminderRepo(ISession session)
{
    this.session = session;
}

This will also get automatically binded through nhibernate.

This won’t work though as the global.aspx only allows no argument constructors.

So how can inject the right classes to these interfaces? Especially the nhibernate session that is the most important thing I need.

Edit

public class NhibernateSessionFactoryProvider : Provider<ISessionFactory>
    {   
        protected override ISessionFactory CreateInstance(IContext context)
        {
            var sessionFactory = new NhibernateSessionFactory();
            return sessionFactory.GetSessionFactory();
        }
    }

  public class NhibernateModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ISessionFactory>().ToProvider<NhibernateSessionFactoryProvider>().InSingletonScope();
            Bind<ISession>().ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();
        }
    }

// in the global.aspx

   protected IKernel CreateKernel()
    {
        var modules = new INinjectModule[]
                          {
                             new NhibernateModule(),
                             new ServiceModule(),
                             new RepoModule(),
                          };

        return new StandardKernel(modules);
    }

// in RepoModule()

  Bind<IReminderRepo>().To<ReminderRepo>();

// in serviceModule

   Bind<IScheduledRemindersService>().To<ScheduledRemindersService>()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:51:00+00:00Added an answer on May 22, 2026 at 2:51 pm

    We use a method like this in our Global.asax.cs file for exactly this purpose (it gets called from Application_Start:

        private static void SetupScheduling(IKernel kernel)
        {
            var scheduler = SchedulerUtil.Scheduler;
            scheduler.JobFactory = kernel.Get<IJobFactory>();
            scheduler.Start();
        }
    

    And we have IJobFactory bound to the following class:

    public class QuartzJobFactory : IJobFactory
    {
        private readonly IObjectFactory<IJob> _jobFactory;
        private readonly LogService _logService;
    
        public QuartzJobFactory(IObjectFactory<IJob> jobFactory,
            LogService logService)
        {
            _jobFactory = jobFactory;
            _logService = logService;
        }
    
        /// <summary>
        /// Called by the scheduler at the time of the trigger firing, in order to
        ///             produce a <see cref="T:Quartz.IJob"/> instance on which to call Execute.
        /// </summary>
        /// <remarks>
        /// <p>It should be extremely rare for this method to throw an exception -
        ///             basically only the the case where there is no way at all to instantiate
        ///             and prepare the Job for execution.  When the exception is thrown, the
        ///             Scheduler will move all triggers associated with the Job into the
        ///             <see cref="F:Quartz.TriggerState.Error"/> state, which will require human
        ///             intervention (e.g. an application restart after fixing whatever 
        ///             configuration problem led to the issue wih instantiating the Job. 
        ///             </p>
        /// </remarks>
        /// <param name="bundle">The TriggerFiredBundle from which the <see cref="T:Quartz.JobDetail"/>
        ///             and other info relating to the trigger firing can be obtained.
        ///             </param><throws>SchedulerException if there is a problem instantiating the Job. </throws>
        /// <returns>
        /// the newly instantiated Job
        /// </returns>
        public IJob NewJob(TriggerFiredBundle bundle)
        {
            Type jobType;
            try
            {
                Require.ThatArgument(bundle != null);
                Require.ThatArgument(bundle.JobDetail != null);
                jobType = bundle.JobDetail.JobType;
            }
            catch (Exception e)
            {
                // This shouldn't ever happen, but if it does I want to know about it.
                _logService.LogCritical(() => e.ToString());
                throw;
            }
            try
            {
                return _jobFactory.GetObject(jobType);
            }
            catch (Exception e)
            {
                _logService.LogCritical(() => "An exception was thrown while creating job of type {0}: {1}"
                    .With(jobType, e));
                throw;
            }
        }
    }
    

    And IObjectFactory is a simple interface that just abstracts away the Kernel so we’re not depending on Ninject everywhere:

    /// <summary>
    /// Similar to IFactory, but this factory produces types based on a dynamic
    /// <see cref="Type"/> object. If the given Type object is not of the given
    /// "T" type, an exception will be thrown.
    /// </summary>
    /// <typeparam name="T">A parent-level type representing what sort of values
    /// are expected. If the type could be anything, try using <see cref="object"/>
    /// </typeparam>
    public interface IObjectFactory<out T>
    {
        T GetObject(Type type);
    }
    

    IObjectFactory is then bound to a class like this:

    /// <summary>
    /// This implementation of the generic <see cref="IFactory{T}"/> and 
    /// <see cref="IObjectFactory{T}"/> classes uses Ninject to supply instances of 
    /// the given type. It should not be used explicitly, but will rather be used 
    /// by the DI framework itself, to provide instances to services that depend on 
    /// IFactory objects.
    /// This implementation takes the injection context as a constructor argument, so that
    /// it can reuse elements of the context when it is asked to supply an instance
    /// of a type.
    /// In order for this to work, you will need to define bindings from <see cref="IFactory{T}"/>
    /// and <see cref="IObjectFactory{T}"/> to this class, as well as a binding from 
    /// <see cref="IContext"/> to a method or factory that returns the current binding 
    /// context.
    /// </summary>
    /// <typeparam name="T">The Type of the service to be produced by the Get method.</typeparam>
    public class InjectionFactory<T> : IFactory<T>, IObjectFactory<T>
    {
        private readonly IKernel _kernel;
        private readonly IParameter[] _contextParameters;
    
        /// <summary>
        /// Constructs an InjectionFactory
        /// </summary>
        /// <param name="injectionContext">The context in which this injection is taking place.</param>
        public InjectionFactory(IContext injectionContext)
        {
            _contextParameters = injectionContext.Parameters
                .Where(p => p.ShouldInherit).ToArray();
            _kernel = injectionContext.Kernel;
        }
    
        public T Get()
        {
            try
            {
                return _kernel.Get<T>(_contextParameters.ToArray());
            }
            catch (Exception e)
            {
                throw e.Wrap(() => "An error occurred while attempting to instantiate an object of type <{0}>".With(typeof(T)));
            }
        }
    
        public T GetObject(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!typeof (T).IsAssignableFrom(type))
            {
                throw new InvalidCastException(type.FullName + " is not a child type of " + typeof (T).FullName);
            }
            try
            {
                return (T)_kernel.Get(type, _contextParameters);
            }
            catch (Exception e)
            {
                throw e.Wrap(() => "An error occurred while attempting to instantiate an object of type <{0}>".With(typeof(T)));
            }
        }
    }
    

    … using a binding that looks like this:

        Bind(typeof (IObjectFactory<>)).To(typeof (InjectionFactory<>));
        // Injection factories ask for the injection context.
        Bind(typeof (IContext)).ToMethod(c => c.Request.ParentContext);
    

    So the overall effect is that we use the Ninject kernel to create the IJobFactory, which uses constructor injection to take an IObjectFactory<IJob>, which is invoked to produce any IJobs that Quartz requires. Those job classes can therefore use constructor-based injection, and Ninject is indirectly instantiating them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make something(i call it a scheduler) that checks the time of
I noticed that when you call a superclass's methods, you need to do something
I need to call a method and pass an object from my custom UITableViewClass
I need to call a library function that sometimes won't terminate within a given
I'm developing an web application using asp.net mvc, and i need to do a
In my Main class I call another class to do something, and I need
In addition to this quite old post , I need something that will use
I'm building an application and need a data structure of interconnected objects that can
I have an application that will only stop/start services if it is run as
I am developing a Windows Service application of some complexity, and need to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.