|
1 | 1 | # Hangfire.AspNet |
2 | 2 |
|
3 | | -[`Install-Package Hangfire.AspNet`](https://www.nuget.org/packages/Hangfire.AspNet/) |
4 | | - |
5 | | -[](https://github.com/HangfireIO/Hangfire.IIS/releases) |
6 | | -[](https://github.com/HangfireIO/Hangfire.IIS/blob/master/LICENSE) |
7 | | -[](https://ci.appveyor.com/project/odinserj/hangfire-aspnet) |
| 3 | +[](https://github.com/HangfireIO/Hangfire.AspNet/releases) |
| 4 | +[](https://ci.appveyor.com/project/hangfireio/hangfire-aspnet) |
8 | 5 |
|
9 | 6 | This package provides recommended way to install Hangfire to ASP.NET applications hosted in IIS with later transition to always-running mode in mind. It contains classes and methods that use `IRegisteredObject` and `IProcessHostPreloadClient` interfaces to plug in to the IIS and ASP.NET application lifecycle more tightly than regular OWIN methods available in the `Hangfire.Core` package. |
10 | 7 |
|
11 | | -This package also includes a Powershell script to enable Always Running mode for your application that is based on Service Autostart Providers. |
| 8 | +The package also includes a Powershell script to enable Always Running mode for your application that is based on Service Autostart Providers. |
| 9 | + |
| 10 | +The package aims to replace the documentation article *Making ASP.NET application always running*. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +This project is available as a NuGet Package: |
| 15 | + |
| 16 | +```powershell |
| 17 | +Install-Package Hangfire.AspNet |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +The package simplifies Hangfire configuration when an application has multiple startup paths, e.g. when using the autostart providers feature to make a web application always running as [described here](https://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html). |
| 23 | + |
| 24 | +We define a configuration method, and point Hangfire.AspNet to it from each startup point. Hangfire.AspNet will ensure it is called only once, so we have Hangfire initialized if any startup point is triggered. |
| 25 | + |
| 26 | +```csharp |
| 27 | +public class Startup |
| 28 | +{ |
| 29 | + public static IEnumerable<IDisposable> GetHangfireConfiguration() |
| 30 | + { |
| 31 | + // Calling configuration as a first step |
| 32 | + GlobalConfiguration.Configuration |
| 33 | + .SetDataCompatibilityLevel(CompatibilityLevel.Version_180) |
| 34 | + .UseSimpleAssemblyNameTypeSerializer() |
| 35 | + .UseRecommendedSerializerSettings() |
| 36 | + .UseSqlServerStorage("connection_string"); |
| 37 | + |
| 38 | + // And then creating background job servers, either one or multiple |
| 39 | + yield return new BackgroundJobServer(); |
| 40 | + } |
12 | 41 |
|
13 | | -This package aims to replace the documentation article *Making ASP.NET application always running*. |
| 42 | + public void Configuration(IAppBuilder app) |
| 43 | + { |
| 44 | + // Initializing from a regular web application startup, including |
| 45 | + // the developer workflow. |
| 46 | + app.UseHangfireAspNet(GetHangfireConfiguration); |
| 47 | + app.UseHangfireDashboard(); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
14 | 51 |
|
15 | | -Documentation is pending, please see https://github.com/HangfireIO/Hangfire.Highlighter. |
| 52 | +```csharp |
| 53 | +public class ApplicationPreload : IProcessHostPreloadClient |
| 54 | +{ |
| 55 | + public void Preload(string[] parameters) |
| 56 | + { |
| 57 | + // Pointing the same configuration from the Startup class, |
| 58 | + // and Hangfire.AspNet will ensure that it is called only |
| 59 | + // once. |
| 60 | + // This method will be triggered by autostart providers |
| 61 | + // feature as described in the article referenced above. |
| 62 | + HangfireAspNet.Use(Startup.GetHangfireConfiguration); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
0 commit comments