Click or drag to resize
AppBuilderExtensions Class
Provides extension methods for the IAppBuilder interface defined in the Owin NuGet package to simplify the integration with OWIN applications.
Inheritance Hierarchy
SystemObject
  HangfireAppBuilderExtensions

Namespace: Hangfire
Assembly: Hangfire.Core (in Hangfire.Core.dll) Version: 1.5.0.0
Syntax
[EditorBrowsableAttribute(EditorBrowsableState.Never)]
public static class AppBuilderExtensions
Methods
  NameDescription
Public methodStatic memberUseHangfireDashboard(IAppBuilder)
Adds Dashboard UI middleware to the OWIN request processing pipeline under the /hangfire path, for the Current storage.
Public methodStatic memberUseHangfireDashboard(IAppBuilder, String)
Adds Dashboard UI middleware to the OWIN request processing pipeline under the given path, for the Current storage.
Public methodStatic memberUseHangfireDashboard(IAppBuilder, String, DashboardOptions)
Adds Dashboard UI middleware to the OWIN request processing pipeline under the specified path and the given options, for the Current storage.
Public methodStatic memberUseHangfireDashboard(IAppBuilder, String, DashboardOptions, JobStorage)
Adds Dashboard UI middleware to the OWIN request processing pipeline with the specified parameters.
Public methodStatic memberUseHangfireServer(IAppBuilder)
Creates a new instance of the BackgroundJobServer class with default options and Current storage and registers its disposal on application shutdown.
Public methodStatic memberUseHangfireServer(IAppBuilder, BackgroundJobServerOptions)
Creates a new instance of the BackgroundJobServer class with the specified options and Current storage, and registers its disposal on application shutdown.
Public methodStatic memberUseHangfireServer(IAppBuilder, IBackgroundProcess)
Creates a new instance of the BackgroundJobServer class with the given collection of additional background processes and Current storage, and registers its disposal on application shutdown.
Public methodStatic memberUseHangfireServer(IAppBuilder, BackgroundJobServerOptions, JobStorage)
Creates a new instance of the BackgroundJobServer class with the given options and specified storage, and registers its disposal on application shutdown.
Public methodStatic memberUseHangfireServer(IAppBuilder, BackgroundJobServerOptions, IBackgroundProcess)
Creates a new instance of the BackgroundJobServer class with the specified options, given collection of background processes and Current storage, and registers its disposal on application shutdown.
Public methodStatic memberUseHangfireServer(IAppBuilder, JobStorage, BackgroundJobServerOptions, IBackgroundProcess)
Starts a new instance of the BackgroundJobServer class with the given arguments, and registers its disposal on application shutdown.
Top
Remarks

This class simplifies Hangfire configuration in OWIN applications, please read Getting Started with OWIN and Katana if you aren't familiar with OWIN and/or don't know what is the Startup class.

The methods of this class should be called from OWIN's Startup class.

UseHangfireDashboard

Dashboard UI contains pages that allow you to monitor almost every aspect of background processing. It is exposed as an OWIN middleware that intercepts requests to the given path.

OWIN implementation of Dashboard UI allows to use it outside of web applications, including console applications and Windows Services.

Important note Important
By default, an access to the Dashboard UI is restricted only to local requests for security reasons. Before publishing a project to production, make sure you still have access to the Dashboard UI by using the Hangfire.Dashboard.Authorization package.

UseHangfireServer

In addition to creation of a new instance of the BackgroundJobServer class, these methods also register the call to its Dispose method on application shutdown. This is done via registering a callback on the corresponding CancellationToken from OWIN environment ("host.OnAppDisposing" or "server.OnDispose" keys).

This enables graceful shutdown feature for background jobs and background processes without any additional configuration.

Please see BackgroundJobServer for more details regarding background processing.

Examples

Basic Configuration

Basic setup in an OWIN application looks like the following example. Please note that job storage should be configured before using the methods of this class.

C#
public void Configuration(IAppBuilder app)
{
    GlobalConfiguration.Configuration
        .UseSqlServerStorage(@"<your_connection_string>");

    app.UseHangfireServer();
    app.UseHangfireDashboard();
}

Adding Dashboard Only

If you want to install dashboard without starting a background job server, for example, to process background jobs outside of your web application, call only the UseHangfireDashboard Overload.

C#
public void Configuration(IAppBuilder app)
{
    // GlobalConfiguration usage was removed for clarity.
    app.UseHangfireDashboard();
}

Change Dashboard Path

By default, you can access Dashboard UI by hitting the http(s)://<app>/hangfire URL, however you can change it as in the following example.

C#
public void Configuration(IAppBuilder app)
{
    // GlobalConfiguration usage was removed for clarity.
    app.UseHangfireDashboard("/admin/hangfire");
}

Configuring Authorization

The following example demonstrates how to change default local-requests-only authorization for Dashboard UI.

C#
// Install the Hangfire.Dashboard.Authorization NuGet package first.

public void Configuration(IAppBuilder app)
{
    // GlobalConfiguration usage was removed for clarity.
    var options = new DashboardOptions
    {
        AuthorizationFilters = new [] 
        {
            new AuthorizationFilter { Roles = "admin" },
        }
    };

    app.UseHangfireDashboard("/hangfire", options);
}

Changing Application Path

Have you seen the Back to site button in the Dashboard? By default it leads you to the root of your site, but you can configure the behavior.

C#
public void Configuration(IAppBuilder app)
{
    // GlobalConfiguration usage was removed for clarity.
    var options = new DashboardOptions { AppPath = "/app" };
    app.UseHangfireDashboard("/hangfire", options);
}

Multiple Dashboards

The following example demonstrates adding multiple Dashboard UI endpoints. This may be useful when you are using multiple shards for your background processing needs.

C#
public void Configuration(IAppBuilder app)
{
    // GlobalConfiguration.UseStorage isn't necessary here,
    // because you are passing a storage instance explicitly.
    var storage1 = new SqlServerStorage(@"connection_string_1");
    var storage2 = new SqlServerStorage(@"connection_string_2");

    app.UseHangfireDashboard("/hangfire1", new DashboardOptions(), storage1);
    app.UseHangfireDashboard("/hangfire2", new DashboardOptions(), storage2);
}
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also