title: Hangfire .net core console Dependency Injection
body: <p>I have a project with this structure </p>
  
  <ul>
  <li><code>AdventureWorks.Web</code> &lt;- .NET Core web project (Services, Viewmodels, DataContext, Controller Views, etc )</li>
  <li><code>AdventureWorks.Console</code> &lt;- .NET Core console app (hangfire config server)</li>
  <li><code>AdventureWorks.Model</code> &lt;- just pure POCO model classes</li>
  </ul>
  
  <p>I am trying to run the Hangfire server in a separate process (<code>AdventureWorks.Console</code>)
  [<a href="https://docs.hangfire.io/en/latest/background-processing/processing-jobs-in-console-app.html]" rel="nofollow noreferrer">https://docs.hangfire.io/en/latest/background-processing/processing-jobs-in-console-app.html]</a> </p>
  
  <p>Processing jobs in a console application.</p>
  
  <p>I added a reference to <code>AdventureWorks.Web</code> to the <code>AdventureWorks.Console</code> - this way the Hangfire server would execute the code</p>
  
  <pre><code>static void Main(string[] args)
  {
      GlobalConfiguration.Configuration.UseSqlServerStorage("Connection");
  
      using (var server = new BackgroundJobServer())
      {
          Console.WriteLine("Hangfire Server started. Press any key to exit...");
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>In the web project I'm registering my services in the <code>Startup</code> class</p>
  
  <pre><code>services.AddTransient&lt;IDepartmentServices, DepartmentServices&gt;();
  </code></pre>
  
  <p>The constructor has a parameter witch is auto injected </p>
  
  <pre><code>private readonly AdventureWorksDataContext db;
  
      public DepartmentServices(AdventureWorksDataContext _db)
      {
          db = _db;
      }
  </code></pre>
  
  <p>In the controller the services are injected </p>
  
  <pre><code>private readonly IDepartmentServices Service;
  
      public DepartmentsController(IDepartmentServices service)
      {
          Service = service;
      }
  </code></pre>
  
  <p>In one method in the controller when the user click the button "create report" the method enqueue a job to create a report</p>
  
  <pre><code>BackgroundJob.Enqueue&lt;**IDepartmentService**&gt;((serv) =&gt; serv.CrearReport(data));
  </code></pre>
  
  <p>So far so good.</p>
  
  <p>I can see in the dashboard the job created but it fails to execute </p>
  
  <blockquote>
    <p>System.MissingMethodException Cannot create an instance of an interface.</p>
  </blockquote>
  
  <p>which is obvious so my first question is</p>
  
  <ul>
  <li>How create a job respecting the dependency injection? I'm trying to follow the instruction here [<a href="https://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html]" rel="nofollow noreferrer">https://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html]</a> but I don't see nothing about the dependency which is injected my best guest hangfire doesn't work with dependency injecting</li>
  </ul>
  
  <p>So to continue I change the interface with the class</p>
  
  <pre><code>BackgroundJob.Enqueue&lt;DepartmentService&gt;((serv) =&gt; serv.CrearReport(data));
  </code></pre>
  
  <p>so far so good the job is created. But it fails with the error</p>
  
  <blockquote>
    <p>System.MissingMethodException<br>
    No parameterless constructor defined for this object</p>
  </blockquote>
  
  <p>which is obvious so my second question is</p>
  
  <ul>
  <li>how do I pass the parameter <code>datacontext</code> to hangfire server? My best guess is I can't because hangfire server is using an Activator which wants an empty constructor</li>
  </ul>
  
  <p>So to continue I followed the instructions here [<a href="https://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html]" rel="nofollow noreferrer">https://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html]</a></p>
  
  <p>I created an empty constructor</p>
  
  <pre><code>public EmployeeService()
          :this(new AdventureWorksDataContext())
      {
  
      }
  </code></pre>
  
  <p>and i need add an empty constructor to the datacontext</p>
  
  <pre><code>public AdventureWorksDataContext()
      {
  
      }
  </code></pre>
  
  <p>but now i need to override the "OnConfigure" method</p>
  
  <pre><code>protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          base.OnConfiguring(optionsBuilder);
          optionsBuilder.UseSqlServer("????");&lt;--connectionstring???
      }
  </code></pre>
  
  <p>so my question is how do i get the connectionstring from the appsettings.json my best guest i cant 
  ignoring this problem i add the direct connection </p>
  
  <pre><code>optionsBuilder.UseSqlServer("Server=.;Database=AdventureWorks2017;Trusted_Connection=True;");
  </code></pre>
  
  <p>this way the job is completed correct</p>
  
  <p>but this solution i think is bad maybe the project structure is bad or how the dependency injection works with hangfire if is running in a separete process, how IoC works en hangfire server console</p>
  
  <p>some one point me a better solution how structure the project </p>
  
  <p>sorry for my English i dont't speak</p>