Hangfire.Ninject 2.0.0

Prefix Reserved
dotnet add package Hangfire.Ninject --version 2.0.0                
NuGet\Install-Package Hangfire.Ninject -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hangfire.Ninject" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hangfire.Ninject --version 2.0.0                
#r "nuget: Hangfire.Ninject, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hangfire.Ninject as a Cake Addin
#addin nuget:?package=Hangfire.Ninject&version=2.0.0

// Install Hangfire.Ninject as a Cake Tool
#tool nuget:?package=Hangfire.Ninject&version=2.0.0                

Hangfire.Ninject Build status

Ninject support for Hangfire. Provides an implementation of the JobActivator class and binding extensions, allowing you to use Ninject IoC container to resolve job type instances as well as control the lifetime of the related dependencies.

Installation

Hangfire.Ninject is available as a NuGet Package. Type the following command into NuGet Package Manager Console window to install it:

Install-Package Hangfire.Ninject

Usage

The package provides an extension method for the IGlobalConfiguration interface, so you can enable Ninject integration using the GlobalConfiguration class.

var kernel = new StandardKernel();
// kernel.Bind...

GlobalConfiguration.Configuration.UseNinjectActivator(kernel);

After invoking the methods above, Ninject-based implementation of the JobActivator class will be used to resolve job type instances and all their dependencies during the background processing.

Re-using Dependencies

Sometimes it is necessary to re-use instances that are already created, such as database connection, unit of work, etc. Thanks to the custom object scopes feature of Ninject, you are able to do this without having to implement anything via code.

Hangfire.Ninject provides a custom scope to allow you to limit the object scope to the current background job processing, just call the InBackgroundJobScope extension method in your binding logic:

kernel.Bind<Database>().ToSelf().InBackgroundJobScope();

Multiple Scopes/Bindings

It's likely that you want to define multiple scopes for your unit-of-work dependencies, one for HTTP request, etc. If you want to use one binding for both these objects and background jobs, please use the following method:

kernel.Bind<JobClass>().ToSelf().InNamedOrBackgroundJobScope(context=> scopeObject);

If you are using InRequestScope and want to use one binding for both HTTP request and background job you need to add your own callback to determine if the HttpContext is still valid.

kernel.Bind<JobClass>().ToSelf().InNamedOrBackgroundJobScope(context => context.Kernel.Components.GetAll<INinjectHttpApplicationPlugin>().Select(c => c.GetRequestScope(context)).FirstOrDefault(s => s != null));

If you are using other scopes in your application, you can construct your own scopes. For example, if you want to define a binding in a background job scope with fallback to thread scope, please use the Ninject's InScope method:

kernel.Bind<JobClass>().ToSelf().InScope(ctx => NinjectJobActivatorScope.Current ?? StandardScopeCallbacks.Thread(ctx));

In this case, the instance of the JobClass class will be re-used within the HTTP request processing, as well as within the background job processing.

All the IDisposable instances of dependencies registered within NinjectJobActivatorScope.Current will be disposed at the end of background job processing, as written in the Deterministic Disposal article.

Deterministic Disposal

All the dependencies that implement the IDisposable interface are disposed as soon as current background job is performed, but only when they were registered using the InBackgroundJobScope method. For other cases, Ninject itself is responsible for disposing instances, so please read the implications of the Cache and Collect system.

For most typical cases, you can call the InBackgroundJobScope method on a job type binding and implement the Dispose method that will dispose all the dependencies manually:

public class JobClass : IDisposable
{
    public JobClass(Dependency dependency) { /* ... */ }

    public Dispose()
    {
        _dependency.Dispose();
    }
}
kernel.Bind<JobClass>().ToSelf().InBackgroundJobScope();
kernel.Bind<Dependency>().ToSelf();

HTTP Request warnings

Services registered with InRequestScope() directive will be unavailable during job activation, you should re-register these services without this hint.

HttpContext.Current is also not available during the job performance. Don't use it!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Hangfire.Ninject:

Package Downloads
Revo.AspNet

Event Sourcing, CQRS and DDD framework for modern C#/.NET applications. ASP.NET platform integration package (user context, authentication using Identity, etc.).

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 33,533 2/19/2024
1.2.0 1,343,674 10/1/2015
1.1.0 16,216 4/13/2015
1.0.0 9,489 6/30/2014
0.1.2 1,992 5/2/2014
0.1.0 1,878 4/1/2014

https://github.com/HangfireIO/Hangfire.Ninject/releases
2.0.0
• Breaking – Use the `NinjectJobActivatorScope.Current` scope instead of the `JobActivatorScope.Current` for deterministic disposal.
• Added – Add `netstandard2.0` and `net46` targets for newer applications.
• Fixed – Correct deterministic disposal for async contexts in `net46` and `netstandard2.0` platforms.
• Project – Add readme and icon for the NuGet package.
• Project – Sign NuGet and assembly artifacts during the build process with a company's own certificate.
• Project – Full source link support with embedded symbols and repository-based sources.
• Project – Require NuGet package signature validation on restore.
• Project – Use deterministic and locked package restore for dependencies.
• Project – Enable static analysis via the `Microsoft.CodeAnalysis.NetAnalyzers` package.
• Project – Modernize the build system and project files to use newest features.