IoC and startup.cs order

Hi guys,

Trying to figure out a way to fix a startup.cs vs. container registration race condition.

I added Hangfire to my project so it’s running in Startup.cs (via OWIN) with something like this:

GlobalConfiguration.Configuration.UseSqlServerStorage("connectionstring"); app.UseHangfireDashboard(); app.UseHangfireServer()

Then I had a background job that needed an interface to be injected into it (we use Autofac) and in our Global.asax.cs file we call a static class to build our container where we setup things to use Autofac:

var builder = new ContainerBuilder(); // bunch of RegisterXXX methods var container = builder.Build() GlobalConfiguration.Configuration.UseAutofacActivator(container); DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

Trying to figure out how to do these in the right order? What have others done where you’re doing both an OWIN startup and a container startup?


We fill our container first then the following


        GlobalConfiguration.Configuration.UseActivator(new MEFJobActivator(container));


        app.UseHangfireDashboard("/hangfire", new DashboardOptions
            AppPath = VirtualPathUtility.ToAbsolute("~"),
            AuthorizationFilters = new[] { new MyRestrictiveAuthorizationFilter() }

        //var options = new BackgroundJobServerOptions
        //    Queues = new[] { "yyy", "xxx", "default" }


I have created Plugging components with having single interface like IPlugin and implemented Interface in different pluggable components. I have created web API 2 where I can backgroundjob.Enqueue like BackgroundJob.Enqueue(()=> processPlugin.ExecutePlugin(inputData)) and have created windows service to execute background job in different machines so here I can configure 2 server where two separate windows service is running on each box.
So problem here, windows service is not recognized pluggable components from custom folders like c:\plugins<SubDir1>.dll c:\plugins<SubDir2>.dll.
Hang fire background job always look component from root folder and if does not found then throw “System.IO.FileNotFoundException: Could not load file or assembly”.
Kindly help me here what are necessary steps apply in windows service in order to dynamically load assembly from pluggable components. I tried using Hangfire.MEF but looks like Job Is Not Activate based on IOC container. Or if I’m missing as part of configuration part?
Have used below on Startup.cs (via OWIN) but no luck.
GlobalConfiguration.Configuration.UseActivator(new MEFJobActivator(container));
Any workable sample using Hangfire + MEF, that would be a great help.

Here is what I have done:

[assembly: OwinStartup(typeof(Poc.QueueApp.Web.Startup))]
namespace Poc.QueueApp.Web
    public partial class Startup
        public void Configuration(IAppBuilder app)

If you need a peek into any of the “Register” methods, let me know.

Hi @KeithBarrows , I’m having no luck setting up Unity… would be awesome if you could post the Register methods below


I will highly appreciate your cooperation.