title: Azure webjobs - Unity - how to inject scoped dependencies to other classes
body: <p>I am trying to inject dependencies in a Azure webJob. I am currently using <code>IJobActivator</code> to create and resolve the unity container.</p>
  
  <pre><code>// Job setup
  static void Main()
  {
      var config = new JobHostConfiguration();
      config.JobActivator = new JobActivationHandler();
  
      var host = new JobHost(config);
      host.RunAndBlock();
  }
  </code></pre>
  
  <p>I am currenlty passing the container itself, to create child container, and resolve the dependencies in the job scope.</p>
  
  <pre><code>internal class JobActivationHandler : IJobActivator
  {
      private IUnityContainer container;
  
      public JobActivationHandler()
      {
          container = new UnityContainer();
  
          container.RegisterType&lt;IDependency, Dependency&gt;();
          // I am passing the container itself, to register job specific 
          // dependencies, pls see the webjob implementation.
          container.RegisterInstance&lt;IUnityContainer&gt;(container);
      }
  
      public T CreateInstance&lt;T&gt;()
      {
          return container.Resolve&lt;T&gt;();
      }
  }
  
  // WebJob
   private readonly IUnityContainer jobContainerInstance;
  
      public ProcessDataJob(IUnityContainer unityContainer)
      {
        // Create a child container for this job instance, 
        // so that the depencies are scoped for current job.
          this.jobContainerInstance = unityContainer.CreateChildContainer();
      }
  
       public async Task ProcessDataJob(
          [QueueTrigger("%Queue:DataQueue%")] DataObject dataObject ,
          [Blob("%Blob:LogOutput%/{TenantId}/{JobId}.txt")] TextWriter blobLogOutput)
      {
            /* Here i am creating a log writer or other service, 
            that are job instance specific, and this dependency 
            should be injected to other classes.
            This service is instance specific, so i cannot instantiate it in 
            the root container*/
           logWriter = CreateLogger(dataObject);
           jobContainerInstance.RegisterInstance&lt;ILogWriter&gt;(logWriter);
  
           /* The only way i can inject this dependency is to resolve a class 
           out of this child container itself. */
              var classB = jobContainerInstance.Resolve&lt;ClassB&gt;()
      }
  
  
  // The dependency ILogWriter is properly resolved. 
  // But i need to pass on the container as well, if i need this dependency to 
  // be injected in class C
  ClassB
  {
     public ClassB(IUnityContainer unityContainerInstance, ILogWriter logWriter)
      {
         var classC = unityContainerInstance.Resolve&lt;ClassC&gt;();
      }
  }
  
  ClassC
  {
     public ClassC(IUnityContainer unityContainerInstance, ILogWriter logWriter)
      {
  
      }
  }
  </code></pre>
  
  <p>Is there a better way to solve this problem instead of cascading the child container to all classes where the resolved depencency has to be used?</p>