-
Notifications
You must be signed in to change notification settings - Fork 90
Update README.md #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Update README.md #80
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ Inspired by AppVeyor, Hangfire.Console provides a console-like logging experienc | |
|
|
||
| ## Features | ||
|
|
||
| - **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. | ||
| - **With Live Updates**: new messages will appear as they're logged, as if you're looking at real console. | ||
| - (blah-blah-blah) | ||
|
|
@@ -58,7 +58,7 @@ Here's what you can configure: | |
| Hangfire.Console provides extension methods on `PerformContext` object, | ||
| hence you'll need to add it as a job argument. | ||
|
|
||
| **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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 services.AddSingleton<JobActivator, InjectContextJobActivator>();There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I.e. add: /// <inheritdoc />
public override void DisposeScope()
{
_scope.Dispose();
} |
||
| Now you can write to console: | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there were some issues with
MemoryStoragedue to incorrect implementation of Set related API. Was it fixed already?