diff --git a/docs/en/Background-Jobs-Hangfire.md b/docs/en/Background-Jobs-Hangfire.md index 6f13d32e05..69903e1c85 100644 --- a/docs/en/Background-Jobs-Hangfire.md +++ b/docs/en/Background-Jobs-Hangfire.md @@ -70,7 +70,7 @@ After you have installed these NuGet packages, you need to configure your projec > You have to configure a storage for Hangfire. -2. If you want to use hangfire's dashboard, you can add `UseHangfireDashboard` call in the `OnApplicationInitialization` method in `Module` class: +2. If you want to use hangfire's dashboard, you can add `UseAbpHangfireDashboard` call in the `OnApplicationInitialization` method in `Module` class: ````csharp public override void OnApplicationInitialization(ApplicationInitializationContext context) @@ -79,7 +79,7 @@ After you have installed these NuGet packages, you need to configure your projec // ... others - app.UseHangfireDashboard(); //should add to the request pipeline before the app.UseConfiguredEndpoints() + app.UseAbpHangfireDashboard(); //should add to the request pipeline before the app.UseConfiguredEndpoints() app.UseConfiguredEndpoints(); } ```` @@ -128,9 +128,10 @@ You can integrate the Hangfire dashboard to [ABP authorization system](Authoriza class. This class is defined in the `Volo.Abp.Hangfire` package. The following example, checks if the current user is logged in to the application: ```csharp -var dashboardOptions = app.ApplicationServices.GetRequiredService(); -dashboardOptions.AsyncAuthorization = new[] { new AbpHangfireAuthorizationFilter() }; -app.UseHangfireDashboard("/hangfire", dashboardOptions); +app.UseAbpHangfireDashboard("/hangfire", options => +{ + options.AsyncAuthorization = new[] { new AbpHangfireAuthorizationFilter() }; +}); ``` * `AbpHangfireAuthorizationFilter` is an implementation of an authorization filter. @@ -145,10 +146,11 @@ app.UseHangfireDashboard("/hangfire", dashboardOptions); If you want to require an additional permission, you can pass it into the constructor as below: ```csharp -var dashboardOptions = app.ApplicationServices.GetRequiredService(); -dashboardOptions.AsyncAuthorization = new[] { new AbpHangfireAuthorizationFilter(requiredPermissionName: "MyHangFireDashboardPermissionName") }; -app.UseHangfireDashboard("/hangfire", dashboardOptions); +app.UseAbpHangfireDashboard("/hangfire", options => +{ + options.AsyncAuthorization = new[] { new AbpHangfireAuthorizationFilter(requiredPermissionName: "MyHangFireDashboardPermissionName") }; +}); ``` -**Important**: `UseHangfireDashboard` should be called after the authentication and authorization middlewares in your `Startup` class (probably at the last line). Otherwise, +**Important**: `UseAbpHangfireDashboard` should be called after the authentication and authorization middlewares in your `Startup` class (probably at the last line). Otherwise, authorization will always fail! diff --git a/docs/en/Background-Workers-Hangfire.md b/docs/en/Background-Workers-Hangfire.md index 9506eb97a7..70c8d0cf95 100644 --- a/docs/en/Background-Workers-Hangfire.md +++ b/docs/en/Background-Workers-Hangfire.md @@ -70,7 +70,7 @@ After you have installed these NuGet packages, you need to configure your projec > You have to configure a storage for Hangfire. -2. If you want to use hangfire's dashboard, you can add `UseHangfireDashboard` call in the `OnApplicationInitialization` method in `Module` class +2. If you want to use hangfire's dashboard, you can add `UseAbpHangfireDashboard` call in the `OnApplicationInitialization` method in `Module` class ````csharp public override void OnApplicationInitialization(ApplicationInitializationContext context) @@ -79,7 +79,7 @@ After you have installed these NuGet packages, you need to configure your projec // ... others - app.UseHangfireDashboard(); //should add to the request pipeline before the app.UseConfiguredEndpoints() + app.UseAbpHangfireDashboard(); //should add to the request pipeline before the app.UseConfiguredEndpoints() app.UseConfiguredEndpoints(); } ```` diff --git a/framework/src/Volo.Abp.BackgroundJobs.HangFire/Microsoft/AspNetCore/Builder/AbpHangfireApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Microsoft/AspNetCore/Builder/AbpHangfireApplicationBuilderExtensions.cs new file mode 100644 index 0000000000..2b79b37f76 --- /dev/null +++ b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Microsoft/AspNetCore/Builder/AbpHangfireApplicationBuilderExtensions.cs @@ -0,0 +1,26 @@ +using System; +using Hangfire; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.BackgroundJobs.Hangfire; + +namespace Microsoft.AspNetCore.Builder; + +public static class AbpHangfireApplicationBuilderExtensions +{ + public static IApplicationBuilder UseAbpHangfireDashboard( + this IApplicationBuilder app, + string pathMatch = "/hangfire", + Action? configure = null, + JobStorage? storage = null) + { + var options = app.ApplicationServices.GetRequiredService().Get(); + configure?.Invoke(options); + return app.UseHangfireDashboard(pathMatch, options, storage); + + + app.UseAbpHangfireDashboard("/hangfire", options => + { + options.AsyncAuthorization = new[] { new AbpHangfireAuthorizationFilter() }; + }); + } +} \ No newline at end of file