Skip to content

Commit 9e66f69

Browse files
committed
Update README.md
1 parent 6b1f8e3 commit 9e66f69

File tree

1 file changed

+61
-39
lines changed

1 file changed

+61
-39
lines changed

README.md

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,12 @@
1-
Hangfire.Ninject
1+
Hangfire.Ninject [![Build status](https://ci.appveyor.com/api/projects/status/79opt6sesdam48yq)](https://ci.appveyor.com/project/odinserj/hangfire-ninject)
22
================
33

4-
[![Build status](https://ci.appveyor.com/api/projects/status/79opt6sesdam48yq)](https://ci.appveyor.com/project/odinserj/hangfire-ninject)
5-
6-
[Hangfire](http://hangfire.io) background job activator based on
7-
[Ninject](http://ninject.org) IoC Container. It allows you to use instance
8-
methods of classes that define parametrized constructors:
9-
10-
```csharp
11-
public class EmailService
12-
{
13-
private DbContext _context;
14-
private IEmailSender _sender;
15-
16-
public EmailService(DbContext context, IEmailSender sender)
17-
{
18-
_context = context;
19-
_sender = sender;
20-
}
21-
22-
public void Send(int userId, string message)
23-
{
24-
var user = _context.Users.Get(userId);
25-
_sender.Send(user.Email, message);
26-
}
27-
}
28-
29-
// Somewhere in the code
30-
BackgroundJob.Enqueue<EmailService>(x => x.Send(1, "Hello, world!"));
31-
```
32-
33-
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.
345

356
Installation
367
--------------
378

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:
4010

4111
```
4212
Install-Package Hangfire.Ninject
@@ -45,19 +15,71 @@ Install-Package Hangfire.Ninject
4515
Usage
4616
------
4717

48-
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.
4919

5020
```csharp
5121
var kernel = new StandardKernel();
22+
// kernel.Bind...
23+
5224
GlobalConfiguration.Configuration.UseNinjectActivator(kernel);
5325
```
5426

27+
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:
34+
35+
```csharp
36+
kernel.Bind<Database>().ToSelf().InBackgroundJobScope();
37+
```
38+
39+
### Multiple Scopes/Bindings
40+
41+
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:
42+
43+
```csharp
44+
kernel.Bind<JobClass>().ToSelf().InRequestOrBackgroundJobScope();
45+
```
46+
47+
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:
48+
49+
```csharp
50+
kernel.Bind<JobClass>().ToSelf().InScope(ctx => JobActivatorScope.Current ?? StandardScopeCallbacks.Thread(ctx));
51+
```
52+
53+
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:
62+
63+
```csharp
64+
public class JobClass : IDisposable
65+
{
66+
public JobClass(Dependency dependency) { /* ... */ }
67+
68+
public Dispose()
69+
{
70+
_dependency.Dispose();
71+
}
72+
}
73+
```
74+
75+
```csharp
76+
kernel.Bind<JobClass>().ToSelf().InBackgroundJobScope();
77+
kernel.Bind<Dependency>().ToSelf();
78+
```
79+
5580
HTTP Request warnings
5681
-----------------------
5782

58-
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.
6184

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

Comments
 (0)