I have set up hangfire successfully for my ASP.NET project, i.e. the 11 Hangfire tables are created in my database. I tried the following command inside the Application_Start() of my project's Global.asax:
namespace myAPI
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start(
{
System.Diagnostics.Debug.WriteLine("Recurring job will be set up.");
RecurringJob.AddOrUpdate(
"some-id",
() => System.Diagnostics.Debug.WriteLine("Job instance started at " +
DateTime.Now)),
"*/2 * * * 1-5");
}
}
}
Sadly, inside Visual Studio's window Output > Debug I only see Reccuring job will be set up. and nothing ever after. However, a SELECT * FROM [myContext].[HangFire].[Set] shows me
Key Score Value ExpireAt
recurring-jobs 1579116240 some-id NULL
So far so good, this means that the job is indeed set up.
But how do I log inside my DB each and each time when the RecurringJob is executed? Do I assume correctly that Hangfire does not do that out of the box and I have to log it myself within the arrow-function? Or is there a more elegant way?
Question on the side: Why don't I see any output of System.Diagnostics.Debug.WriteLine within my recurring job?
References
Hangfire doesn't create tables in IIS
How to configure hangfire with ASP.NET to obtain connection string from config file?
Official hangfire.io docu on recurrent tasks
You can use SeriLog with Hangfire out of the box. Serilog comes with different sinks, e.g. Serilog.Sinks.MSSqlServer. You can configure it in startup.cs:
using Serilog;
using Serilog.Sinks.MSSqlServer;
Log.Logger = new LoggerConfiguration()
.WriteTo
.MSSqlServer(
connectionString: hangfireConnectionString,
tableName: "Logs",
autoCreateSqlTable: true
).CreateLogger();
// will display any issues with Serilog config. comment out in prod.
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
GlobalConfiguration.Configuration
.UseSqlServerStorage(hangfireConnectionString)
.UseSerilogLogProvider();
After you schedule your job, you can log it with
Log.Information(string.Format("Hanfire Job Scheduled at {0}", DateTime.Now));
Hangfire includes a concept of job filters (similar to ASP.NET MVC's Action Filters). For your use case, you would define one that would write to your database (adjust based on your needs):
using Hangfire.Common;
using Hangfire.Server;
class LogCompletionAttribute : JobFilterAttribute, IServerFilter
{
public void OnPerforming(PerformingContext filterContext)
{
// Code here if you care when the execution **has begun**
}
public void OnPerformed(PerformedContext context)
{
// Check that the job completed successfully
if (!context.Canceled && context.Exception != null)
{
// Here you would write to your database.
// Example with entity framework:
using (var ctx = new YourDatabaseContext())
{
ctx.Something.Add(/**/);
ctx.SaveChanges();
}
}
}
}
And then apply the filter to the job method:
namespace myAPI
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start(
{
System.Diagnostics.Debug.WriteLine("Recurring job will be set up.");
RecurringJob.AddOrUpdate("some-id", () => MyJob(), "*/2 * * * 1-5");
}
[LogCompletion]
public static void MyJob()
{
System.Diagnostics.Debug.WriteLine("Job instance started at " + DateTime.Now)
}
}
}
Docs: https://docs.hangfire.io/en/latest/extensibility/using-job-filters.html
So the cron is set to fire At every 2nd minute on every day-of-week from Monday through Friday. I assume you are waiting for the job to execute and that it is in the right window of time.
Most of the references that I found on the web indicated that you can do.
RecurringJob.AddOrUpdate(() => Console.WriteLine("This job will execute once in every minute"), Cron.Minutely);
Maybe you have to line up the dots a bit better to write to the vs console.
There is also an admin portal that can be configured to see what is begin run and when.
I have the following setup.
Global.asax.cs
protected void Application_Start()
{
HangfireJobsConfig.Register();
}
public class HangfireJobsConfig
{
public static void Register()
{
if (App1Config.RunHangfireService)
{
JobStorage.Current = new SqlServerStorage(App1Config.DefaultConnectionStringName.Split('=').Last());
GlobalConfiguration.Configuration.UseConsole();
RecurringJob.AddOrUpdate("RunJob1", () => RunJob1(null), Cron.MinuteInterval(App1Config.RunJob1Interval));
RecurringJob.AddOrUpdate("RunJob2", () => RunJob2(null), Cron.MinuteInterval(App1Config.RunJob2Interval));
}
}
[AutomaticRetry(Attempts = 0, Order = 1)]
public static void RunJob1(PerformContext context)
{
//dostuff
}
[AutomaticRetry(Attempts = 0, Order = 2)]
public static void RunJob2(PerformContext context)
{
//do stuff
}
}
Startup.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
ConfigureHangFire(app);
}
public void ConfigureHangFire(IAppBuilder app)
{
if (App1Config.RunHangfireService)
{
GlobalConfiguration.Configuration.UseSqlServerStorage(
AppiConfig.DefaultConnectionStringName.Split('=').Last());
GlobalConfiguration.Configuration.UseConsole();
app.UseHangfireServer();
var options = new DashboardOptions
{
AuthorizationFilters = new[]
{
new AuthorizationFilter { Roles = "Inventory" }
}
};
app.UseHangfireDashboard("/hangfire", options);
}
}
}
The actual problem was a very trivial one, the initialization of the actual background server was missing BackgroundJobServer();. Here the fully functional code:
namespace myAPI
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
string connString = ConfigurationManager.ConnectionStrings["myContext"].ToString();
Hangfire.GlobalConfiguration.Configuration.UseConsole();
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(connString,
new SqlServerStorageOptions {
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true
});
var bgndJS = new BackgroundJobServer(); // <--- this is essential!
RecurringJob.AddOrUpdate("myRecurringJob", () => HangfireRecurringJob(), "*/2 * * * 1-5");
System.Diagnostics.Debug.WriteLine("---> RecurringJob 'myHangfireJob' initated.");
}
public void HangfireRecurringJob() {
System.Diagnostics.Debug.WriteLine("---> HangfireRecurringJob() executed at" + DateTime.Now);
Console.Beep(); // <-- I was really happy to hear the beep
}
}
}
Related
I'm currently writing an integration test (https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-5.0) for my ASP .Net Core 5 REST API.
The API is using Serilog for logging (with the static Serilog Logger). I am running tests with NUnit, Visual Studio 2019, Resharper.
I want all the messages, that are logged during the runtime of the API code, to be visible in the test console output.
For example, if this controller method is called:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Serilog;
namespace My.Crazy.Api.Controllers
{
public sealed class WheelsController : Controller
{
[HttpGet("getwheels")]
public async Task<IActionResult> Get()
{
Log.Error("An extremely urgent error");
return Ok();
}
}
}
I expect the "An extremely urgent error" message to be shown in the test console.
However, this is not happening.
Here is my TestServer setup:
[OneTimeSetUp]
public async Task Setup()
{
var hostBuilder = new HostBuilder()
.ConfigureWebHost(webHost =>
{
webHost.UseTestServer();
webHost.UseStartup<Startup>(); // Startup is the API project's Startup class
Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();
});
var host = await hostBuilder.StartAsync();
_client = host.GetTestClient();
}
[Test]
public async Task FirstTest()
{
var response = await _client.GetAsync("getwheels");
}
I have also tried logging with a custom Sink:
...
// in the test setup
Log.Logger = new LoggerConfiguration().WriteTo.Sink(new CustomSink()).CreateLogger();
...
public class CustomSink : ILogEventSink
{
public void Emit(LogEvent logEvent)
{
var message = logEvent.RenderMessage();
Console.WriteLine(message);
}
}
This does not work as well. However, I have confirmed that the Emit method is being invoked when API code logs any message.
Finally, I have tried using a File output:
Log.Logger = new LoggerConfiguration().WriteTo.File("C:\\temp\\test_output.txt").CreateLogger();
which worked as expected. However, I still want to log in the console.
Is this possible?
Using anything else for Serilog or NUnit is unfortunately not an option.
So I would try with a custom logger provider with logger:
LoggerProvider:
public class NUnitLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName)
{
return new NUnitLogger();
}
public void Dispose()
{
}
}
Logger:
public class NUnitLogger : ILogger, IDisposable
{
public void Dispose()
{
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
Func<TState, Exception, string> formatter) {
var message = formatter(state, exception);
Debug.WriteLine(message);
}
public bool IsEnabled(LogLevel logLevel) => true;
public IDisposable BeginScope<TState>(TState state) => this;
}
Then in the test file:
var hostBuilder = new HostBuilder()
.ConfigureWebHost(webHost =>
{
webHost.UseTestServer()
.UseStartup<TestStartup>()
.ConfigureLogging((hostBuilderContext, logging) =>
{
logging.Services.AddSingleton<ILoggerProvider, NUnitLoggerProvider>();
});
});
And instead of Debug.WriteLine(message) you can use something else to log to.
I had the same problem. After days of digging, I found a workaround with the initialization of the test server. The key is in setting to true the PreserveExecutionContext which is by default false. Setting it to true brings the logs to the test output. False - no server logs are visible, only client ones.
var path = Assembly.GetAssembly(typeof(MyTestServer))?.Location;
var directoryName = Path.GetDirectoryName(path);
if (directoryName == null)
throw new InvalidOperationException("Cannot obtain startup directory name");
var hostBuilder = new WebHostBuilder()
.UseContentRoot(directoryName)
.ConfigureAppConfiguration(
configurationBuilder => configurationBuilder.AddJsonFile("appsettings.json", false))
.UseStartup<Startup>()
.ConfigureTestServices(services =>
{
//adding mock services here
});
server = new TestServer(hostBuilder)
{
//set this to true!!!
PreserveExecutionContext = true
};
Note: we're running these tests (and the system under test) on .NET7. I am not sure whether this makes any difference.
I have an ASP.NET Web API REST service and I would like to execute some code once when service is started only for first time, not each time a web api method is requested/invoked from my ASP.NET MVC application.
I would like to do this because I want to initialize an EventLog and then use it to create entries in the windows event viewer.
Is there some easy way to do it?
UPDATE:
As Jonhatan suggested in his answer I create a method within global.asax.cs:
Global.asax.cs:
namespace MyWebAPIApp
{
public class WebApiApplication : System.Web.HttpApplication
{
public MyLog _myLog;
protected void Application_Start()
{
// Here some stuff
SetupEventLogging();
}
private void SetupEventLogging()
{
if (!EventLog.SourceExists("MyWebApiLog"))
{
EventLog.CreateEventSource("MyWebApiLog", "MyWebApiLogLog");
}
EventLog eventLog = new EventLog();
eventLog.Source = "MyWebApiLog";
eventLog.Log = "MyWebApiLog";
_myLog = new MyLog(eventLog, "MyWebApiService");
}
}
}
Controller:
namespace MyWebAPIApp.Controllers
{
public class MyController : ApiController
{
public void GetAll()
{
_myLog.Success("All records read");
}
}
}
But now if I create a global variable _myLog, how can I access this variable from all the methods in my Controller in order to do _myLog.Error(...) or _myLog.Success(...)?
You would typically do that in the ApplicationStart method in your global.asax.cs:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
SetupLogging(); // do something in here / wire up your flavour of logging
}
Often, the pattern will be:
set up your logging on app start - this is where you set up the database connection to store the logs, etc
call a static logger.Write method throughout your code whenever you want to write to log.
I use Microsoft.Practices.EnterpriseLibrary.Logging, but I think Serilog or Log4Net are 2 probably more common frameworks now.
So, in my global.asax.cs, the SetupLogging() method is:
private static void SetupLogging()
{
var configurationSource = ConfigurationSourceFactory.Create();
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(configurationSource));
var logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create());
var daysToKeepLogsInDb = int.Parse(ConfigurationManager.AppSettings["DaysToKeepLogsInDb"]);
CustomLogger.PurgeLogs(daysToKeepLogsInDb); // only keep last 90 etc days of event logging in the db
CustomLogger.Write("Application Starting", TraceEventType.Information);
}
Basically just the things that the framework needs to 'get going', and a little custom cleanup. And then I have a CustomLogger class to help write entries the way I want, run a custom stored procedure to clean up old logs, etc:
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
namespace MyApplication.Helpers
{
public class CustomLogger
{
private static readonly ICollection<string> EmptyCategoriesList = new List<string>(0);
private const string LogTitle = "MyApplication Name";
public static void Write(object message)
{
Write(message, TraceEventType.Error);
}
public static void Write(object message, TraceEventType severity)
{
Logger.Write(message, EmptyCategoriesList, -1, 1, severity, LogTitle);
}
public static void PurgeLogs(int keepLastXDays)
{
var connectionString = ConfigurationManager.ConnectionStrings["MyLoggingConnectionString"].ConnectionString;
using (var con = new SqlConnection(connectionString))
{
using (var command = new SqlCommand("PurgeLogs", con)) // custom stored procedure
{
var dateTo = DateTime.Now.AddDays(keepLastXDays * -1);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("#dateTo", dateTo));
command.Parameters.Add(new SqlParameter("#title", LogTitle));
con.Open();
command.ExecuteNonQuery();
con.Close(); // technically not required because in using, but leaving in case this block gets copy-pasted out of here
}
}
}
}
}
And then, within my code (controller, helper, whatever), I write tot he log through the static method in the custom logger:
public static void EndSession(Session session)
{
try
{
Logon.DoLogoff(session);
}
catch (Exception exception)
{
CustomLogger.Write(exception);
throw new Exception("Error ending session.");
}
}
If you do this with dependency injection, it would (especially) allow you to swap out your logging framework more easily, and allow you to unit test a little more easily. But you would have to create another 'layer' between your application and the logger to abstract the relationship out a bit more. You should read up on dependency injection, as it is something that is often worth using.
But now if I create a global variable _myLog, how can I access this variable from all the methods in my Controller in order to do _myLog.Error(...) or _myLog.Success(...)?
Make _myLog static and reference it WebApiApplication._myLog where WebApiApplication is application class defined in global.asax.cs.
I'd rather create some static class with MyLog static property:
public static class LogManager
{
public static MyLog Logger;
}
And in global.asax.cs in SetupEventLogging() put
LogManager.Logger = new MyLog(eventLog, "MyWebApiService");
I need to setup NLog when Azure Functions instantiates my assembly.
public class Startup : FunctionsStartup {
public Startup()
{
var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
}
public override void Configure(IFunctionsHostBuilder builder) {
... other setup code
builder.Services.AddLogging((loggingBuilder) =>
{
loggingBuilder.AddNLog();
});
}
}
NLog folks recommend manually shutting down the logger via NLog.LogManager.Shutdown().
The problem is that I am not sure how to catch the instance of Azure shutting my assembly down.
Does Azure expose a Dispose event of some sort that I am not seeing?
Looks like you are depending on Microsoft Extension Logging, so I would probably hook into its lifetime-logic (Disable AutoShutdown and enable ShutdownOnDispose)
public class Startup : FunctionsStartup {
public Startup()
{
var logger = LogManager.Setup()
.SetupExtensions(e => e.AutoLoadAssemblies(false))
.LoadConfigurationFromFile("nlog.config", optional: false)
.LoadConfiguration(builder => builder.LogFactory.AutoShutdown = false)
.GetCurrentClassLogger();
}
public override void Configure(IFunctionsHostBuilder builder) {
... other setup code
builder.Services.AddLogging((loggingBuilder) =>
{
loggingBuilder.AddNLog(new NLogProviderOptions() { ShutdownOnDispose = true });
});
}
}
What is the proper way to update SQL Azure using Entity Framework from Azure Web Job ?
I only able to found a post SQL Azure using Azure Web Job
But above solution using SQL Client not through Entity Framework !!!
can bellow code run properly under Azure Web Job
public void DoSomething([TimerTrigger("*/5 * * * * *")] TimerInfo timer, TextWriter log)
{
try
{
var tempdetails = _sampleRepository.SearchFor(x=> DateTime.UtcNow > x.DateTo);
foreach (var detail in tempdetails)
{
if (detail.ID == 2)
{
detail.ID = 5;
}
_sampleRepository.Update(detail);
}
_unitOfWork.Commit();
}
catch (Exception ex)
{
log.WriteLine(ex.Message);
}
}
Ninject Binding
public class NinjectBindings : NinjectModule
{
public override void Load()
{
//Register Context
Kernel.Bind<MyDbContext>().ToSelf();
Kernel.Bind<IUnitOfWork<MyDbContext>>().To<UnitOfWork<MyDbContext>>();
//Register Repository
Kernel.Bind(x => x
.FromAssemblyContaining<MyDbContext>()
.SelectAllClasses()
.InheritedFrom(typeof(IRepository<>))
.BindDefaultInterface());
}
}
Program.CS
static void Main()
{
using (IKernel kernel = new StandardKernel(new NinjectBindings()))
{
var config = new JobHostConfiguration()
{
JobActivator = new NinjectJobActivator(kernel)
};
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
config.UseTimers();
var host = new JobHost(config);
host.RunAndBlock();
}
}
App.Config
<connectionStrings>
<add name="AzureWebJobsDashboard" connectionString="" />
<add name="AzureWebJobsStorage" connectionString="" />
<add name="EFContext" connectionString="Server=xxxx,1433;Initial Catalog=xxxxxx;....;User
ID=xxxxxxx;Password=xxxxxxxx;...;Encrypt=True;TrustServerCertificate=False;Connection
Timeout=30;App=EntityFramework" providerName="System.Data.SqlClient" /> </connectionStrings>
"Azure Portal Setting to Run Web Job"
As discussed with [ MS Support ] , after we added the
AzureWebJobsStorage and AzureWebJobDashboard connection strings as the Application settings blade in Azure Portal the web-job started to work
without the exception.
How To find out Root cause of the bellow issue
[ Working Code - Update Database]
var tempdetails = _myRepository.SearchFor(condifiotn);
if(tempdetails != null)
{
foreach (var detail in tempdetails)
{
_unitOfWork.GetContext.Entry(detail).State = EntityState.Detached;
//Do Some Modification
detail.Name = "Test";
_unitOfWork.GetContext.Entry(detail).State = EntityState.Modified;
}
//Save Changes
_unitOfWork.Commit();
}
[ Not Working Code - Unable to update database]
var tempdetails = _myRepository.SearchFor(condifiotn);
if(tempdetails != null)
{
foreach (var detail in tempdetails)
{
//Do Some Modification
detail.Name = "Test";
_myRepository.Update(detail);
}
//Save Changes
_unitOfWork.Commit();
}
Update Method
public void Update(E entity)
{
//_dbSet.Attach(entity);
UnitOfWork.GetContext.Entry(entity).State = System.Data.Entity.EntityState.Modified;
}
According to your description, I followed this tutorial to implement my Repository and Unit of Work Pattern in my Azure Web Jobs. You could follow the code snippet below to check your code.
NinjectBindings:
public class NinjectBindings : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<DbContext>().ToMethod(ctx=> {
return new BruceDbContext();
});
Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
BruceDbContext:
public class BruceDbContext : DbContext
{
public BruceDbContext()
: base("name=brucedbcontext")
{
}
public BruceDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
//...
}
UnitOfWork:
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext _dbContext;
public UnitOfWork(DbContext dbContext)
{
_dbContext = dbContext;
}
public IRepository<Author> AuthorRepository =>
new GenericRepository<Author>(_dbContext);
public void SaveChanges()
{
_dbContext.SaveChanges();
}
public void Dispose()
{
_dbContext.Dispose();
}
}
Functions.cs
public class Functions
{
private static IUnitOfWork _iUnitOfWork;
public Functions(IUnitOfWork iUnitOfWork)
{
_iUnitOfWork = iUnitOfWork;
}
public void DoSomething([TimerTrigger("*/30 * * * * *")] TimerInfo timer, TextWriter log)
{
_iUnitOfWork.AuthorRepository.Add(new Author()
{
Name = Guid.NewGuid().ToString()
});
_iUnitOfWork.Commit();
var allRecords=_iUnitOfWork.AuthorRepository.GetAll().ToList();
Console.WriteLine(JsonConvert.SerializeObject(allRecords));
}
}
Note: Since you are using TimerTrigger, you need to call the config.UseTimers(); before constructing the JobHost. Additionally, you could debug your webjob locally to find the detailed error. Or you could update your question and provide more detailed errors (e.g. ex.StackTrace) for us to narrow this issue.
Ninject binding in Singleton Scope do the job as exception. I know this is not the best solution. Waiting for best ans. If some how I configure Ninject in Call Scope.
//Register Context
Kernel.Bind<MyDbContext>().ToSelf().InSingletonScope();
Kernel.Bind<IUnitOfWork<MyDbContext>>().To<UnitOfWork<MyDbContext>>().InSingletonScope();
[ using InSingletonScope bellow code working as expected ]
var tempdetails = _myRepository.SearchFor(condifiotn);
if(tempdetails != null)
{
foreach (var detail in tempdetails)
{
//Do Some Modification
detail.Name = "Test";
_myRepository.Update(detail);
}
//Save Changes
_unitOfWork.Commit();
}
How can I use .NET Core's default dependency injection in Hangfire?
I am new to Hangfire and searching for an example which works with ASP.NET Core.
See full example on GitHub https://github.com/gonzigonz/HangfireCore-Example.
Live site at http://hangfirecore.azurewebsites.net/
Make sure you have the Core version of Hangfire:
dotnet add package Hangfire.AspNetCore
Configure your IoC by defining a JobActivator. Below is the config for use with the default asp.net core container service:
public class HangfireActivator : Hangfire.JobActivator
{
private readonly IServiceProvider _serviceProvider;
public HangfireActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public override object ActivateJob(Type type)
{
return _serviceProvider.GetService(type);
}
}
Next register hangfire as a service in the Startup.ConfigureServices method:
services.AddHangfire(opt =>
opt.UseSqlServerStorage("Your Hangfire Connection string"));
Configure hangfire in the Startup.Configure method. In relationship to your question, the key is to configure hangfire to use the new HangfireActivator we just defined above. To do so you will have to provide hangfire with the IServiceProvider and this can be achieved by just adding it to the list of parameters for the Configure method. At runtime, DI will providing this service for you:
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
IServiceProvider serviceProvider)
{
...
// Configure hangfire to use the new JobActivator we defined.
GlobalConfiguration.Configuration
.UseActivator(new HangfireActivator(serviceProvider));
// The rest of the hangfire config as usual.
app.UseHangfireServer();
app.UseHangfireDashboard();
}
When you enqueue a job, use the registered type which usually is your interface. Don't use a concrete type unless you registered it that way. You must use the type registered with your IoC else Hangfire won't find it.
For Example say you've registered the following services:
services.AddScoped<DbManager>();
services.AddScoped<IMyService, MyService>();
Then you could enqueue DbManager with an instantiated version of the class:
BackgroundJob.Enqueue(() => dbManager.DoSomething());
However you could not do the same with MyService. Enqueuing with an instantiated version would fail because DI would fail as only the interface is registered. In this case you would enqueue like this:
BackgroundJob.Enqueue<IMyService>( ms => ms.DoSomething());
DoritoBandito's answer is incomplete or deprecated.
public class EmailSender {
public EmailSender(IDbContext dbContext, IEmailService emailService)
{
_dbContext = dbContext;
_emailService = emailService;
}
}
Register services:
services.AddTransient<IDbContext, TestDbContext>();
services.AddTransient<IEmailService, EmailService>();
Enqueue:
BackgroundJob.Enqueue<EmailSender>(x => x.Send(13, "Hello!"));
Source:
http://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html
Note: if you want a full sample, see my blog post on this.
All of the answers in this thread are wrong/incomplete/outdated. Here's an example with ASP.NET Core 3.1 and Hangfire.AspnetCore 1.7.
Client:
//...
using Hangfire;
// ...
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddHangfire(config =>
{
// configure hangfire per your requirements
});
}
}
public class SomeController : ControllerBase
{
private readonly IBackgroundJobClient _backgroundJobClient;
public SomeController(IBackgroundJobClient backgroundJobClient)
{
_backgroundJobClient = backgroundJobClient;
}
[HttpPost("some-route")]
public IActionResult Schedule([FromBody] SomeModel model)
{
_backgroundJobClient.Schedule<SomeClass>(s => s.Execute(model));
}
}
Server (same or different application):
{
//...
services.AddScoped<ISomeDependency, SomeDependency>();
services.AddHangfire(hangfireConfiguration =>
{
// configure hangfire with the same backing storage as your client
});
services.AddHangfireServer();
}
public interface ISomeDependency { }
public class SomeDependency : ISomeDependency { }
public class SomeClass
{
private readonly ISomeDependency _someDependency;
public SomeClass(ISomeDependency someDependency)
{
_someDependency = someDependency;
}
// the function scheduled in SomeController
public void Execute(SomeModel someModel)
{
}
}
As far as I am aware, you can use .net cores dependency injection the same as you would for any other service.
You can use a service which contains the jobs to be executed, which can be executed like so
var jobId = BackgroundJob.Enqueue(x => x.SomeTask(passParamIfYouWish));
Here is an example of the Job Service class
public class JobService : IJobService
{
private IClientService _clientService;
private INodeServices _nodeServices;
//Constructor
public JobService(IClientService clientService, INodeServices nodeServices)
{
_clientService = clientService;
_nodeServices = nodeServices;
}
//Some task to execute
public async Task SomeTask(Guid subject)
{
// Do some job here
Client client = _clientService.FindUserBySubject(subject);
}
}
And in your projects Startup.cs you can add a dependency as normal
services.AddTransient< IClientService, ClientService>();
Not sure this answers your question or not
Currently, Hangfire is deeply integrated with Asp.Net Core. Install Hangfire.AspNetCore to set up the dashboard and DI integration automatically. Then, you just need to define your dependencies using ASP.NET core as always.
If you are trying to quickly set up Hangfire with ASP.NET Core (tested in ASP.NET Core 2.2) you can also use Hangfire.MemoryStorage. All the configuration can be performed in Startup.cs:
using Hangfire;
using Hangfire.MemoryStorage;
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(opt => opt.UseMemoryStorage());
JobStorage.Current = new MemoryStorage();
}
protected void StartHangFireJobs(IApplicationBuilder app, IServiceProvider serviceProvider)
{
app.UseHangfireServer();
app.UseHangfireDashboard();
//TODO: move cron expressions to appsettings.json
RecurringJob.AddOrUpdate<SomeJobService>(
x => x.DoWork(),
"* * * * *");
RecurringJob.AddOrUpdate<OtherJobService>(
x => x.DoWork(),
"0 */2 * * *");
}
public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider)
{
StartHangFireJobs(app, serviceProvider)
}
Of course, everything is store in memory and it is lost once the application pool is recycled, but it is a quick way to see that everything works as expected with minimal configuration.
To switch to SQL Server database persistence, you should install Hangfire.SqlServer package and simply configure it instead of the memory storage:
services.AddHangfire(opt => opt.UseSqlServerStorage(Configuration.GetConnectionString("Default")));
I had to start HangFire in main function. This is how I solved it:
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
using (var serviceScope = host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
try
{
var liveDataHelper = services.GetRequiredService<ILiveDataHelper>();
var justInitHangfire = services.GetRequiredService<IBackgroundJobClient>();
//This was causing an exception (HangFire is not initialized)
RecurringJob.AddOrUpdate(() => liveDataHelper.RePopulateAllConfigDataAsync(), Cron.Daily());
// Use the context here
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Can't start " + nameof(LiveDataHelper));
}
}
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
Actually there is an easy way for dependency injection based job registration.
You just need to use the following code in your Startup:
public class Startup {
public void Configure(IApplicationBuilder app)
{
var factory = app.ApplicationServices
.GetService<IServiceScopeFactory>();
GlobalConfiguration.Configuration.UseActivator(
new Hangfire.AspNetCore.AspNetCoreJobActivator(factory));
}
}
However i personally wanted a job self registration including on demand jobs (recurring jobs which are never executed, except by manual trigger on hangfire dashboard), which was a little more complex then just that. I was (for example) facing issues with the job service activation, which is why i decided to share most of my implementation code.
//I wanted an interface to declare my jobs, including the job Id.
public interface IBackgroundJob {
string Id { get; set; }
void Invoke();
}
//I wanted to retrieve the jobs by id. Heres my extension method for that:
public static IBackgroundJob GetJob(
this IServiceProvider provider,
string jobId) => provider
.GetServices<IBackgroundJob>()
.SingleOrDefault(j => j.Id == jobId);
//Now i needed an invoker for these jobs.
//The invoker is basically an example of a dependency injected hangfire job.
internal class JobInvoker {
public JobInvoker(IServiceScopeFactory factory) {
Factory = factory;
}
public IServiceScopeFactory Factory { get; }
public void Invoke(string jobId)
{
//hangfire jobs should always be executed within their own scope.
//The default AspNetCoreJobActivator should technically already do that.
//Lets just say i have trust issues.
using (var scope = Factory.CreateScope())
{
scope.ServiceProvider
.GetJob(jobId)?
.Invoke();
}
}
//Now i needed to tell hangfire to use these jobs.
//Reminder: The serviceProvider is in IApplicationBuilder.ApplicationServices
public static void RegisterJobs(IServiceProvider serviceProvider) {
var factory = serviceProvider.GetService();
GlobalConfiguration.Configuration.UseActivator(new Hangfire.AspNetCore.AspNetCoreJobActivator(factory));
var manager = serviceProvider.GetService<IRecurringJobManager>();
var config = serviceProvider.GetService<IConfiguration>();
var jobs = serviceProvider.GetServices<IBackgroundJob>();
foreach (var job in jobs) {
var jobConfig = config.GetJobConfig(job.Id);
var schedule = jobConfig?.Schedule; //this is a cron expression
if (String.IsNullOrWhiteSpace(schedule))
schedule = Cron.Never(); //this is an on demand job only!
manager.AddOrUpdate(
recurringJobId: job.Id,
job: GetJob(job.Id),
cronExpression: schedule);
}
//and last but not least...
//My Method for creating the hangfire job with injected job id
private static Job GetJob(string jobId)
{
var type = typeof(JobInvoker);
var method = type.GetMethod("Invoke");
return new Job(
type: type,
method: method,
args: jobId);
}
Using the above code i was able to create hangfire job services with full dependency injection support. Hope it helps someone.
Use the below code for Hangfire configuration
using eForms.Core;
using Hangfire;
using Hangfire.SqlServer;
using System;
using System.ComponentModel;
using System.Web.Hosting;
namespace eForms.AdminPanel.Jobs
{
public class JobManager : IJobManager, IRegisteredObject
{
public static readonly JobManager Instance = new JobManager();
//private static readonly TimeSpan ZeroTimespan = new TimeSpan(0, 0, 10);
private static readonly object _lockObject = new Object();
private bool _started;
private BackgroundJobServer _backgroundJobServer;
private JobManager()
{
}
public int Schedule(JobInfo whatToDo)
{
int result = 0;
if (!whatToDo.IsRecurring)
{
if (whatToDo.Delay == TimeSpan.Zero)
int.TryParse(BackgroundJob.Enqueue(() => Run(whatToDo.JobId, whatToDo.JobType.AssemblyQualifiedName)), out result);
else
int.TryParse(BackgroundJob.Schedule(() => Run(whatToDo.JobId, whatToDo.JobType.AssemblyQualifiedName), whatToDo.Delay), out result);
}
else
{
RecurringJob.AddOrUpdate(whatToDo.JobType.Name, () => RunRecurring(whatToDo.JobType.AssemblyQualifiedName), Cron.MinuteInterval(whatToDo.Delay.TotalMinutes.AsInt()));
}
return result;
}
[DisplayName("Id: {0}, Type: {1}")]
[HangFireYearlyExpirationTime]
public static void Run(int jobId, string jobType)
{
try
{
Type runnerType;
if (!jobType.ToType(out runnerType)) throw new Exception("Provided job has undefined type");
var runner = runnerType.CreateInstance<JobRunner>();
runner.Run(jobId);
}
catch (Exception ex)
{
throw new JobException($"Error while executing Job Id: {jobId}, Type: {jobType}", ex);
}
}
[DisplayName("{0}")]
[HangFireMinutelyExpirationTime]
public static void RunRecurring(string jobType)
{
try
{
Type runnerType;
if (!jobType.ToType(out runnerType)) throw new Exception("Provided job has undefined type");
var runner = runnerType.CreateInstance<JobRunner>();
runner.Run(0);
}
catch (Exception ex)
{
throw new JobException($"Error while executing Recurring Type: {jobType}", ex);
}
}
public void Start()
{
lock (_lockObject)
{
if (_started) return;
if (!AppConfigSettings.EnableHangFire) return;
_started = true;
HostingEnvironment.RegisterObject(this);
GlobalConfiguration.Configuration
.UseSqlServerStorage("SqlDbConnection", new SqlServerStorageOptions { PrepareSchemaIfNecessary = false })
//.UseFilter(new HangFireLogFailureAttribute())
.UseLog4NetLogProvider();
//Add infinity Expiration job filter
//GlobalJobFilters.Filters.Add(new HangFireProlongExpirationTimeAttribute());
//Hangfire comes with a retry policy that is automatically set to 10 retry and backs off over several mins
//We in the following remove this attribute and add our own custom one which adds significant backoff time
//custom logic to determine how much to back off and what to to in the case of fails
// The trick here is we can't just remove the filter as you'd expect using remove
// we first have to find it then save the Instance then remove it
try
{
object automaticRetryAttribute = null;
//Search hangfire automatic retry
foreach (var filter in GlobalJobFilters.Filters)
{
if (filter.Instance is Hangfire.AutomaticRetryAttribute)
{
// found it
automaticRetryAttribute = filter.Instance;
System.Diagnostics.Trace.TraceError("Found hangfire automatic retry");
}
}
//Remove default hangefire automaticRetryAttribute
if (automaticRetryAttribute != null)
GlobalJobFilters.Filters.Remove(automaticRetryAttribute);
//Add custom retry job filter
GlobalJobFilters.Filters.Add(new HangFireCustomAutoRetryJobFilterAttribute());
}
catch (Exception) { }
_backgroundJobServer = new BackgroundJobServer(new BackgroundJobServerOptions
{
HeartbeatInterval = new System.TimeSpan(0, 1, 0),
ServerCheckInterval = new System.TimeSpan(0, 1, 0),
SchedulePollingInterval = new System.TimeSpan(0, 1, 0)
});
}
}
public void Stop()
{
lock (_lockObject)
{
if (_backgroundJobServer != null)
{
_backgroundJobServer.Dispose();
}
HostingEnvironment.UnregisterObject(this);
}
}
void IRegisteredObject.Stop(bool immediate)
{
Stop();
}
}
}
Admin Job Manager
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
if (Core.AppConfigSettings.EnableHangFire)
{
JobManager.Instance.Start();
new SchedulePendingSmsNotifications().Schedule(new Core.JobInfo() { JobId = 0, JobType = typeof(SchedulePendingSmsNotifications), Delay = TimeSpan.FromMinutes(1), IsRecurring = true });
}
}
protected void Application_End(object sender, EventArgs e)
{
if (Core.AppConfigSettings.EnableHangFire)
{
JobManager.Instance.Stop();
}
}
}