HangfireIO/Hangfire · error · ArgumentNullException
exception
Error message
exception
What it means
Thrown by the ClientExceptionContext constructor when the exception argument is null. ClientExceptionContext carries the exception that occurred during job creation to IClientExceptionFilter.OnClientException handlers, enabling filters to inspect, log, or mark the exception handled. A null exception is a contract violation because the context's sole purpose is to wrap that exception. It is constructed inside BackgroundJobFactory.Create's catch block, so it should never be null in normal flows.
Source
Thrown at src/Hangfire.Core/Client/ClientExceptionContext.cs:29
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with Hangfire. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace Hangfire.Client
{
/// <summary>
/// Provides the context for the <see cref="IClientExceptionFilter.OnClientException"/>
/// method of the <see cref="IClientExceptionFilter"/> interface.
/// </summary>
public class ClientExceptionContext : CreateContext
{
public ClientExceptionContext(CreateContext createContext, Exception exception)
: base(createContext)
{
if (exception == null) throw new ArgumentNullException(nameof(exception));
Exception = exception;
}
/// <summary>
/// Gets an exception that occurred during the creation of the job.
/// </summary>
public Exception Exception { get; }
/// <summary>
/// Gets or sets a value that indicates that this <see cref="ClientExceptionContext"/>
/// object handles an exception occurred during the creation of the job.
/// </summary>
public bool ExceptionHandled { get; set; }
}
}View on GitHub (pinned to c236dd0f93)
Solutions
- Always pass a concrete Exception instance (e.g., new InvalidOperationException("test")) when constructing ClientExceptionContext.
- In production code, rely on BackgroundJobFactory.Create which builds ClientExceptionContext from the caught exception — never null.
- Guard upstream in custom factories: if (ex == null) return; before constructing the context.
Example fix
// before
var ctx = new ClientExceptionContext(createContext, null);
// after
var ctx = new ClientExceptionContext(createContext, new InvalidOperationException("boom")); Defensive patterns
Strategy: validation
Validate before calling
if (exception == null) throw new ArgumentException("Exception is required to build ClientExceptionContext", nameof(exception));
var ctx = new ClientExceptionContext(createContext, exception); Type guard
public static bool IsValidException(Exception ex)
=> ex != null; Prevention
- Only construct ClientExceptionContext inside a catch block using the caught exception.
- Let BackgroundJobFactory.Create build the context — do not replicate its catch logic.
- In tests, pass a concrete Exception (e.g., new InvalidOperationException()).
When it happens
Trigger: Manually instantiating new ClientExceptionContext(createContext, null); a custom IClientExceptionFilter or test that builds the context without a real Exception; reflection-based construction.
Common situations: Unit testing IClientExceptionFilter implementations; building a custom exception-dispatch pipeline that mimics BackgroundJobFactory's catch block; mocking frameworks that default reference arguments to null.
Related errors
AI-assisted analysis of HangfireIO/Hangfire@c236dd0f93 (2026-08-13).
Data as JSON: /api/errors/a5c5da77a6bd6f37.
Report an issue: GitHub.