Update README.md#80
Open
lukevp wants to merge 1 commit into
Open
Conversation
lukevp
commented
Jul 1, 2019
- Add notes based on my testing with Redis.StackExchange at https://github.com/marcoCasamento/Hangfire.Redis.StackExchange and MemoryStorage.
- Add gotcha about ASP.NET Core DI unable to inject PerformContext into the constructor.
Add notes based on my testing with Redis.StackExchange at https://github.com/marcoCasamento/Hangfire.Redis.StackExchange and MemoryStorage. Add gotcha about ASP.NET Core DI unable to inject PerformContext into the constructor.
|
|
||
| **NOTE**: Like `IJobCancellationToken`, `PerformContext` is a special argument type which Hangfire will substitute automatically. You should pass `null` when enqueuing a job. | ||
| **NOTE**: Like `IJobCancellationToken`, `PerformContext` is a special argument type which Hangfire will substitute automatically. You should pass `null` when enqueuing a job. It is not currently possible to use constructor injection for the PerformContext in ASP.NET Core. | ||
|
|
Owner
There was a problem hiding this comment.
You can actually do that with a custom job activator:
public class InjectContextJobActivator : JobActivator
{
private readonly IServiceScopeFactory _scopeFactory;
public InjectContextJobActivator(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
}
public override JobActivatorScope BeginScope(PerformContext context)
{
return new Scope(context, _scopeFactory.CreateScope());
}
private class Scope : JobActivatorScope, IServiceProvider
{
private readonly PerformContext _context;
private readonly IServiceScope _scope;
public Scope(PerformContext context, IServiceScope scope)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_scope = scope ?? throw new ArgumentNullException(nameof(scope));
}
public override object Resolve(Type type)
{
return ActivatorUtilities.GetServiceOrCreateInstance(this, type);
}
object IServiceProvider.GetService(Type serviceType)
{
if (serviceType == typeof(PerformContext))
return _context;
return _scope.ServiceProvider.GetService(serviceType);
}
}
}Just register it as a service in ConfigureServices:
services.AddSingleton<JobActivator, InjectContextJobActivator>();There was a problem hiding this comment.
Just for future readers that have copied this code basically blindly, i just found out that because the serviceScope is not disposed this will cause a memory leak and possible myriad of other issues if your transient/scoped services are not cleaned up properly.
There was a problem hiding this comment.
I.e. add:
/// <inheritdoc />
public override void DisposeScope()
{
_scope.Dispose();
}|
|
||
| - **Provider-agnostic**: (allegedly) works with any job storage provider (currently tested with SqlServer and MongoDB). | ||
| - **Provider-agnostic**: (allegedly) works with any job storage provider (currently tested with SqlServer, MemoryStorage, Redis.StackExchange and MongoDB). | ||
| - **100% Safe**: no Hangfire-managed data (e.g. jobs, states) is ever updated, hence there's no risk to corrupt it. |
Owner
There was a problem hiding this comment.
I believe there were some issues with MemoryStorage due to incorrect implementation of Set related API. Was it fixed already?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.