Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Copy Markdown
Owner

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 MemoryStorage due to incorrect implementation of Set related API. Was it fixed already?

- **With Live Updates**: new messages will appear as they're logged, as if you're looking at real console.
- (blah-blah-blah)
Expand Down Expand Up @@ -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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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 ConfigureServices:

services.AddSingleton<JobActivator, InjectContextJobActivator>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Expand Down