Base solution for your next web application
Open Closed

Azure Web Jobs and dependency injection using IocManager #1861


User avatar
0
phoenix created

I am trying to configure dependency injection for azure web job using IoCManager. For example, I want to inject IRepository<Currency>.

I have done the following:

public class JobActivator : IJobActivator
{
   private readonly IWindsorContainer _container;
   public JobActivator()
   {
      _container = IocManager.Instance.IocContainer;
   }
   public T CreateInstance<T>()
   {
      return _container.Resolve<T>();
   }
}
public class Program
{
   public static void Main()
   {
      var config = new JobHostConfiguration()
      {
         JobActivator = new JobActivator()
      };
      JobHost host = new JobHost(config);
      host.RunAndBlock();
   }
}
public class Functions
{
   private readonly IRepository<Currency> _currencyRepository;
   public Functions(IRepository<Currency> currencyRepository)
   {
      _currencyRepository = currencyRepository;
   }
   public void ProcessQueueMessage([QueueTrigger("BuyerProfileQueue")] JobMessage message)
   {
      Currency currency = _currencyRepository.Get(1);
   }
}

When job triggers, I have the following exception: Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service was found Any help would be greatly appreciated Pavel


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Probably the Functions class is not registered for the dependency injection. You can implement ITransientDependency or ISingletonDependency for it, so it will be registered to DI. for more information please check <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Dependency-Injection">http://aspnetboilerplate.com/Pages/Docu ... -Injection</a>

  • User Avatar
    0
    phoenix created

    Thanks to Halil, the problem was solved:

    [DependsOn(typeof(REPDataModule))]
    public class MyConsoleModule : AbpModule
    {
       public override void Initialize()
       {
          IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
       }
    }
    public class Program
    {
       public static void Main()
       {
          using(var bootstrapper = new AbpBootstrapper())
          {
             bootstrapper.Initialize();
             var config = new JobHostConfiguration()
             {
                JobActivator = new JobActivator(bootstrapper.IocManager.IocContainer)
             };
             if (config.IsDevelopment)
             {
                config.UseDevelopmentSettings();
             }
             JobHost host = new JobHost(config);
             host.RunAndBlock();
          }
       }
    }
    
  • User Avatar
    0
    hikalkan created
    Support Team

    You're welcome :)