Hangfire with .net core framework 4.6

Tags: #<Tag:0x00007f2356a28200>

Hello,

I’am using Hangfire with Asp.net framework project 4.6, I get this error ;
‘IServiceCollection’ does not contain a definition for ‘AddHangfire’ and no extension method ‘AddHangfire’ accepting a first argument of type ‘IServiceCollection’ could be found (are you missing a using directive or an assembly reference?)

‘IApplicationBuilder’ does not contain a definition for ‘UseHangfireServer’ and the best extension method overload ‘AppBuilderExtensions.UseHangfireServer(IAppBuilder)’ requires a receiver of type ‘IAppBuilder’ SMCModule.API…NET Framework 4.6 E:\MVC 6\SMCModule Solution\SMCModule\src\SMCModule.API\Startup.cs

Severity Code Description Project File Line Suppression State
Error CS1929 ‘IApplicationBuilder’ does not contain a definition for ‘UseHangfireDashboard’ and the best extension method overload ‘AppBuilderExtensions.UseHangfireDashboard(IAppBuilder)’ requires a receiver of type ‘IAppBuilder’ SMCModule.API…NET Framework 4.6

SMCModule.API is the name of my project.

my project.json file is :
{
“dependencies”: {
“Microsoft.ApplicationInsights.AspNetCore”: “1.0.0”,
“Microsoft.AspNetCore.Mvc”: “1.0.0”,
“Microsoft.AspNetCore.Server.IISIntegration”: “1.0.0”,
“Microsoft.AspNetCore.Server.Kestrel”: “1.0.0”,
“Microsoft.Extensions.Configuration.EnvironmentVariables”: “1.0.0”,
“Microsoft.Extensions.Configuration.FileExtensions”: “1.0.0”,
“Microsoft.Extensions.Configuration.Json”: “1.0.0”,
“Microsoft.Extensions.Logging”: “1.0.0”,
“Microsoft.Extensions.Logging.Console”: “1.0.0”,
“Microsoft.Extensions.Logging.Debug”: “1.0.0”,
“Microsoft.Extensions.Options.ConfigurationExtensions”: “1.0.0”,
“SMCModule.Core”: “1.0.0-*”,
“Newtonsoft.Json”: “9.0.1”,
“SimpleInjector”: “3.2.0”,
“Microsoft.AspNetCore.Owin”: “1.0.0”,
“Hangfire”: “1.6.0”
},

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

“frameworks”: {
“net46”: { }
},

“buildOptions”: {
“emitEntryPoint”: true,
“preserveCompilationContext”: true
},

“publishOptions”: {
“include”: [
“wwwroot”,
“Views”,
“Areas/**/Views”,
“appsettings.json”,
“web.config”
]
},

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

Thanks.

1 Like

Have you included the using Hangfire; statement?

I experience almost the same problem. Steps to reproduce in VS 2015:

  1. File -> New -> Project
  2. Choose "ASP.Net Core Web Application (.Net Framework)
  3. Select “Empty” template
  4. In Package Management Console: “Install-Package Hangfire”
  5. In Startup.cs, add:

using Hangfire;

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

       app.UseHangfireServer(); // --->  Expects IAppBuilder but there is IApplicationBuilder 

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    }

Compiler Output:

Error CS1929 ‘IApplicationBuilder’ does not contain a definition for ‘UseHangfireServer’ and the best extension method overload ‘AppBuilderExtensions.UseHangfireServer(IAppBuilder)’ requires a receiver of type ‘IAppBuilder’

Thank you.

But what does your ConfigureServices method look like?
Do you have a services.AddHangfire(…); call in it?
Also, do you have either “Hangfire”: “1.6.0” or “Hangfire”: “1.6.*” in your project.json?
That IAppBuilder bit is suspicious - like it’s using an older version not designed to run in the latest .NET
Are you also using the “net46” framework? What about targeting “netcoreapp1.0” instead?

There is no ConfigureServices because it’s it’s a simplest project type. But I tried to add services.AddHangfire(…) and result is same - compile error.

Here is project.json.

{
  "dependencies": {
    "Hangfire": "1.6.2",
    "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"
  },

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

  "frameworks": {
    "net461": { }
  },

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

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

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

There are three possibility to create web application:

  1. ASP.NET Web Application (.Net Framework):
    UseHangfireServer is an extension to IAppBuilder, No problem to configure Hangfire
  2. ASP.NET Core Web Application (.Net Core):
    UseHangfireServer is an extension to IApplicationBuilder, No problem to configure Hangfire
  3. ASP.NET Core Web Application (.Net Framework):
    UseHangfireServer is an extension to IAppBuilder, but there is IApplicationBuilder only and it is not in the same assembly as in p.2
    In this case the project doesn’t compile

My colleague advised to install package Hangfire.AspNetCore instead of Hangfire and it started to work for “ASP.NET Core Web Application (.Net Framework)”

3 Likes

I am having problem installing hangfire on asp.net core project with .net framework 4.6.1.
Hangfire.ASPNetCore tries to install Hangfire.Core which tries to install Owin which is not resolved. Can any body guide me what I am doing wrong here?
gmatv, can u share your final solution?
This is my project.json

{
  "dependencies": {
    "Hangfire.AspNetCore": "1.6.3",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": {
      "version": "1.0.0",
      "type": "build"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    }
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "frameworks": {
    "net461": { }
  },

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

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

I running on net461 with aspcore without issues. I have:

"Hangfire": "1.6.2",
"Hangfire.AspNetCore": "1.6.2",

In my dependencies. In startup.cs I then added:

using Hangfire;

and under ConfigureServices:

        services.AddHangfire(configuration => {
            configuration.UseSqlServerStorage(Configuration.GetConnectionString("Hangfire"));
        });

and under Configure:

        app.UseHangfireDashboard();
        app.UseHangfireServer();

Is that not working for you?

1 Like

Okay. This is little strange. I am not sure why it happened. With the project.json that I mentioned earlier, I was getting an issue with the OWIN restore As a dependency of Hangfire.Core. I didn’t try to explicitly load it on my work pc and for the whole day, I wasnt able to make it work.

If I changed my app to target netcoreapp1.0, Hangfire.AspNetCore use to work fine but I have some dependencies on the full framework and cant use it.

When I tried to restore the packages, at home pc, the Hangfire.AspNetCore restored fine with the OWIN dependency resolved.

I suspect, it has something to do with my Nuget Configurations maybe & Not with hangfire. I had messed around with them earlier.

Are you using Visual Studio? I’ve noticed VS/Nuget occasionally get confused with netcore projects. Running “dotnet restore” from the command line seems to resolve everything for me.

What about this issue? I have the same problem and want to know, if there is a solution until now?

I am using ASP.NET Core WebApplication (.NET Framework) and I need to use this project-type instead of ASP.NET Core WebApplication (.NET Core), where Hangfire is working as expected.

I just experienced the same problem when I installed NUGET package Hangfire.

Actually what I found that there are 2 projects AspNetCore and aspNetcore.NEtStandard

I didn’t investigate deep inside by It is looks like when you download from Nuget to asp.net core (net461) VS load wrong references.
Because AddHangfire() is just extension method of static class HangfireServiceCollectionExtensions which is located at Hangfire namespace. So if you load HangFire.AspNetCore, normally your VS should see this extension method.

So to do this work, I download one by one:

  1. Hangfire.AspNetCore
  2. HandFire.Core
    3)HangFire.SQLServer

Currently I’m using 1.6.17 version for all of them. And it is work perfectly.