Hangfire with asp.net core

Hi Everyone,

I am working with hangfire 1.6.0 asp.net core.I can’t configure Hangfire to run correctly.
I have this exception;
An exception of type ‘System.InvalidOperationException’ occurred in Hangfire.AspNetCore.dll but was not handled in user code

Additional information: Unable to find the required services. Please add all the required services by calling ‘IServiceCollection.AddHangfire’ inside the call to ‘ConfigureServices(…)’ in the application startup code.

Any idea please ?

Please add the call to AddHangfire method in Startup.ConfigureServices:

public class Startup
{
    // ...

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("Hangfire")));
        // ...
    }

    // ....

A Great Thanks Sergey.

Another issue please : the dll Microsoft.Owin.Host.SystemWeb is it optional ?
What is its utility ? because the build fails when i add it in project.json
the exception :
Package Owin 1.0.0 (even i change the version to 3.0.0) is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0).

Thanks Again.

Please show me your project.json file.

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Kendo.Mvc": "2016.2.630-Preview",
    "Newtonsoft.Json": "9.0.1",
    "Hangfire": "1.6.0",
    "Microsoft.Owin.Host.SystemWeb" :  "3.0.1"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Just remove the following line (why did you add it?):

"Microsoft.Owin.Host.SystemWeb" :  "3.0.1"

And Hangfire 1.6.1 released yesterday, it’s better to update

Exactly what i am asking. Why “Microsoft.Owin.Host.SystemWeb” ? i found that it is fondamental for dashboard ? isn’t it ?

With Hangfire 1.6.1 build fails

I have added Hangfire to services but still get

“Unable to find the required services. Please add all the required services by calling ‘IServiceCollection.AddHangfire’ inside the call to ‘ConfigureServices(…)’ in the application startup code.”

when calling app.UseHangfireServer();
I’m using Hangfire 1.6.1 in Dotnet Core 1.0.

services.AddHangfire(config =>
{
    config.UseSqlServerStorage(connectionString);
    config.UseActivator(new AutofacJobActivator(container));
});

Any ideas what is wrong?

I was able to get Hangfire up and running by moving AddHangfire before setting up the Autofac container, but then i cannot activate Autofac using .UseActivator inside AddHangfire.

But calling …

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

… also gave me this error message …

An unhandled exception of type ‘System.Reflection.TargetInvocationException’ occurred in System.Private.CoreLib.ni.dll

Additional information: Exception has been thrown by the target of an invocation.

…in Program.Main(). Any ideas?
(Without AddHangfire my application starts and localhost/hangfire gives the Hangfire-dashboard)

My code:

services.AddHangfire(config =>
{
    config.UseSqlServerStorage(connectionString);
    //config.UseActivator(new AutofacJobActivator(container));
});

var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(services);
var container = containerBuilder.Build();
GlobalConfiguration.Configuration.UseActivator(new AutofacJobActivator(container));
//container.Resolve<IGlobalConfiguration>().UseActivator(new AutofacJobActivator(container));

It’s fundamental when hosting ASP.NET application under IIS. For ASP.NET Core applications it isn’t used at all, because aspnetcore doesn’t use the old aspnet pipeline.

@toralux, the problem is related to ASP.NET Core + Autofac integration, not to Hangfire + Autofac, because the Unable to find the required services. Please add all the required services by calling exception appears, when trying to resolve services through the ASP.NET Core’s IServiceProvider instance. Please show me the whole Startup class source code.

I was able to resolve it - you were correct - it was related to missing registration in Autofac. Hangfire now works like a charm! :smiley: Thanks! :grinning:

1 Like