title: Converting Hangfire into modular startup
body: <p>I am converting my startup code into new <a href="https://docs.servicestack.net/modular-startup" rel="nofollow noreferrer">ServiceStack Modular Startup</a> approach and have hit a snag.</p>
  
  <p>I have this code in old startup</p>
  
  <pre><code>public void Configure(IApplicationBuilder app)
  {
      app.UseHangfireDashboard(hangfirePath, new DashboardOptions
      {
          Authorization = new[] { new HangFireAuthorizationFilter() }
      });
  
      var appHost = new AppHost
      {
          AppSettings = settings
      };
  
      app.UseServiceStack(appHost);
  
      var container = appHost.Resolve&lt;Container&gt;();
  
      GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(container));
  
      app.UseHangfireServer();
  }
  </code></pre>
  
  <p>It's important that <code>app.UseHangfireDashboard</code> is registered before <code>app.UseServiceStack</code> or the dashboard wont work.</p>
  
  <p>I got it working all fine except for the part where it links the IoC container to hangfire:</p>
  
  <pre><code>GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(container));
  </code></pre>
  
  <p>This is the working code without linking container:</p>
  
  <pre><code>[Priority(-1)]
  public class ConfigureHangfirePostgreSql : IConfigureServices, IConfigureApp
  {
      IConfiguration Configuration { get; }
      public ConfigureHangfirePostgreSql(IConfiguration configuration) =&gt; Configuration = configuration;
      public void Configure(IServiceCollection services)
      {
          var conn = Configuration.GetValue&lt;string&gt;("database:connectionString");
          services.AddHangfire((isp, config) =&gt;
          {
              config.UsePostgreSqlStorage(conn, new PostgreSqlStorageOptions
              {
                  InvisibilityTimeout = TimeSpan.FromDays(1)
              });
  
              config.UseConsole();
  
          });
      }
  
      public void Configure(IApplicationBuilder app)
      {
  
          app.UseHangfireDashboard("/hangfire", new DashboardOptions
          {
              Authorization = new[] { new HangFireAuthorizationFilter() }
          });
  
          // commented out because I dont think it's possible to get container yet and also code doesn't work
          //var container = app.Resolve&lt;Container&gt;();
          //GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(container));         
  
          app.UseHangfireServer();
  
      }
  }
  </code></pre>
  
  <p>I am setting the priority to -1 so it runs before servicestack is registered.  Because of that I guess the container isn't yet created so I need to make another module to run after like this:</p>
  
  <pre><code>[Priority(2)]
  public class ConfigureHangfirePostgreSqlPost : IConfigureApp
  {
      public void Configure(IApplicationBuilder app)
      {
          //how do I resolve container here?
      }
  }
  </code></pre>
  
  <p>The container isn't registered as a service (as I am asking the container for the service) so im not sure how I am able to access it.</p>
  
  <p>What is the right way of getting hold of the container in a startup module?</p>