You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improve the testability of your jobs without static factories!
4
+
[Ninject](http://www.ninject.org/) support for [Hangfire](http://hangfire.io). 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.
34
5
35
6
Installation
36
7
--------------
37
8
38
-
Hangfire.Ninject is available as a NuGet Package. Type the following
39
-
command into NuGet Package Manager Console window to install it:
9
+
*Hangfire.Ninject* is available as a NuGet Package. Type the following command into NuGet Package Manager Console window to install it:
The package provides an extension method for `IGlobalConfiguration` interface:
18
+
The package provides an extension method for the `IGlobalConfiguration` interface, so you can enable Ninject integration using the `GlobalConfiguration` class.
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.
28
+
29
+
### Re-using Dependencies
30
+
31
+
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](https://github.com/ninject/Ninject/wiki/Object-Scopes) feature of Ninject, you are able to do this without having to implement anything via code.
32
+
33
+
*Hangfire.Ninject* provides a [custom scope](https://github.com/ninject/Ninject/wiki/Object-Scopes#custom-scopes) to allow you to limit the object scope to the **current background job processing**, just call the `InBackgroundJobScope` extension method in your binding logic:
It is probably that you want to define multiple scopes for your unit-of-work dependencies, one for HTTP request. If you want to use one binding for both HTTP request and background job, please use the following method:
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:
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.
54
+
55
+
All the `IDisposable` instances of dependencies registered within `JobActivatorScope.Current` will be disposed at the end of background job processing, as written in the *Deterministic Disposal* article.
56
+
57
+
### Deterministic Disposal
58
+
59
+
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](https://github.com/ninject/ninject/wiki/Changes-in-Ninject-2).
60
+
61
+
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:
Services registered with `InRequestScope()` directive **will be unavailable**
59
-
during job activation, you should re-register these services without this
60
-
hint.
83
+
Services registered with `InRequestScope()` directive **will be unavailable** during job activation, you should re-register these services without this hint.
61
84
62
-
`HttpContext.Current` is also **not available** during the job performance.
63
-
Don't use it!
85
+
**`HttpContext.Current` is also not available during the job performance. Don't use it!**
0 commit comments