![]() |
VOOZH | about |
dotnet add package Hangfire.Ninject --version 2.0.2
NuGet\Install-Package Hangfire.Ninject -Version 2.0.2
<PackageReference Include="Hangfire.Ninject" Version="2.0.2" />
<PackageVersion Include="Hangfire.Ninject" Version="2.0.2" />Directory.Packages.props
<PackageReference Include="Hangfire.Ninject" />Project file
paket add Hangfire.Ninject --version 2.0.2
#r "nuget: Hangfire.Ninject, 2.0.2"
#:package Hangfire.Ninject@2.0.2
#addin nuget:?package=Hangfire.Ninject&version=2.0.2Install as a Cake Addin
#tool nuget:?package=Hangfire.Ninject&version=2.0.2Install as a Cake Tool
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.
Hangfire.Ninject is available as a NuGet Package. Type the following command into NuGet Package Manager Console window to install it:
dotnet add package Hangfire.Ninject
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.
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();
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.
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();
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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net45 net45 is compatible. net451 net451 was computed. net452 net452 was computed. net46 net46 is compatible. net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
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.). |
This package is not used by any popular GitHub repositories.
https://github.com/HangfireIO/Hangfire.Ninject/releases
2.0.2
• Fixed – Add workaround for a possible deadlock in Ninject when singletons and interceptors are used.
2.0.1
• Fixed – Don't use `ICache.Clear` for deterministic disposal due to possible deadlock problems.
• Project – Upgrade build scripts to the new PowerShell for faster builds.
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.