I am new to .net core and need your help. Below is my implementation that does not work
Main.cs
static void Main(string[] args)
{
var serviceProvider = new ServiceCollection()
.AddLogging()
.AddTransient<ICustomLogger, TextWriterLogger>()
.BuildServiceProvider();
StartAsyncTest(args);
}
public async static void StartAsyncTest(string[] args)
{
HttpAsyncTest.SetupCommand dto = new HttpAsyncTest.SetupCommand();
HttpAsyncTest.ExecuteCommand executeCommand = new HttpAsyncTest.ExecuteCommand();
var test = executeCommand.ExecuteAsync(new HttpAsyncTest(dto));
await test;
}
My Execute Command is in a different .Net Core Class Library called AsyncTest.Domain
public partial class HttpAsyncTest : IValidEntity, IBuisnessEntity
{
public HttpAsyncTest(SetupCommand dto)
{
HttpRequestContainers = new List<HttpAsyncRequestContainer>();
this.Setup(dto);
}
private ICustomLogger _logger;
public HttpAsyncTest(ICustomLogger logger)
{
_logger = logger;
}
async public Task ExecuteAsync(ExecuteCommand dto)
{
_logger.Log // throws null reference exception
}
}
The ILogger interface is in the domain class library and its implementation is in the infrastructure library and the domain does not reference the infrastructure one as per the DDD principles.
What am I doing wrong above and how do I fix the null reference exception.
Sorry, I am new to the .net core and .net core DI and need your guidance.
Because you didn't use DI Container to create the instance, the _logger field will point NULL in HttpAsyncTest object, and you didn't assign ICustomLogger field from your code.
public partial class HttpAsyncTest : IValidEntity, IBuisnessEntity
{
public HttpAsyncTest(SetupCommand dto,ICustomLogger logger)
{
HttpRequestContainers = new List<HttpAsyncRequestContainer>();
_logger = logger;
this.Setup(dto);
}
private ICustomLogger _logger;
async public Task ExecuteAsync(ExecuteCommand dto)
{
_logger.Log;
}
}
In Main method, you can call by serviceProvider DI Container object which you can get HttpAsyncTest object from this Container that will create an object as you register.
static void Main(string[] args)
{
var serviceProvider = new ServiceCollection()
.AddLogging()
.AddTransient<ICustomLogger, TextWriterLogger>()
.AddTransient<SetupCommand>()
.AddTransient<HttpAsyncTest>()
.BuildServiceProvider();
}
public async static void StartAsyncTest(ServiceProvider serviceProvider)
{
HttpAsyncTest.ExecuteCommand executeCommand = new HttpAsyncTest.ExecuteCommand();
var test = executeCommand.ExecuteAsync(serviceProvider.GetService<HttpAsyncTest>());
await test;
}
I have written a Windows Service which logs its operations using Serilog
The application also uses Autofac for dependency Injection support and Seq for structured logging.
using Autofac;
public class ContainerInitiator
{
public static IContainer BuildContainer()
{
var _builder = new ContainerBuilder();
var logger = LoggerUtility.CreateLogger();
_builder.RegisterInstance(logger).As<ILogger>().SingleInstance();
var container = _builder.Build();
ContainerFactory.SetContainer(container);
return container;
}
}
Here is Log utility class
public class LoggerUtility
{
private static string connectionString = ConfigurationManager.AppSettings["applicationName"];
public static ILogger CreateLogger()
{
return GetLoggerConfiguration().CreateLogger();
}
private static LoggerConfiguration GetLoggerConfiguration()
{
var config = new LoggerConfiguration()
.ReadFrom.AppSettings();
config = config.
Enrich.WithProperty("ApplicationName", connectionString).
Enrich.WithExceptionDetails().
Enrich.WithMachineName().
Enrich.WithProcessId().
Enrich.WithThreadId().
ReadFrom.AppSettings();
return config;
}
}
Here is my service class, which is logging the activities
public class Engine : IEngine
{
private readonly ILogger _logger;
public RedistributeEngine(IContainerFactory containerFactory)
{
_logger = containerFactory.GetInstance<ILogger>();
}
public void Start()
{
_logger.Information("Engine started!");
}
}
As you see below Seq logs, it has two entries on the same time!
You're reading the configuration from App.Settings twice:
private static LoggerConfiguration GetLoggerConfiguration()
{
var config = new LoggerConfiguration()
.ReadFrom.AppSettings(); // <<<<<<<<<<<< *#*#*#*#*#*#*#*#
config = config.
Enrich.WithProperty("ApplicationName", connectionString).
Enrich.WithExceptionDetails().
Enrich.WithMachineName().
Enrich.WithProcessId().
Enrich.WithThreadId().
ReadFrom.AppSettings(); // <<<<<<<<<<<< *#*#*#*#*#*#*#*#
return config;
}
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();
}
}
}
I'm a little confused about how to avoid service locator when using a console application
Program
public static int Main(string[] args)
{
// Configuration
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").AddEnvironmentVariables().Build();
// DI container
var services = new ServiceCollection();
ConfigureServices(services, configuration);
var serviceProvider = services.BuildServiceProvider();
// Do I pass along the serviceProvider?
// Can resolve using locator pattern do I just use this in my classes?
// var exampleRepository = _serviceProvider.GetService<IExampleRepository>();
// Execute the correct command based on args
return CommandLineOptions.Execute(args);
}
private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddScoped<ApplicationDbContext>((s) => new ApplicationDbContext(configuration.GetSection("Data:DefaultConnection:ConnectionString").Value));
services.AddScoped<IExampleRepository, ExampleRepository>();
}
CommandLineOptions
public static class CommandLineOptions
{
public static int Execute(string[] args, IServiceProvider serviceProvider)
{
try
{
var app = new CommandLineApplication
{
Name = "dnx abc",
FullName = "Abc Commands",
Description = "ABC",
};
app.VersionOption("--version", PlatformServices.Default.Application.ApplicationVersion);
app.HelpOption("-?|-h|--help");
app.OnExecute(() =>
{
//ShowLogo();
app.ShowHelp();
return 2;
});
app.Command(
"task",
task=>
{
task.Name = "Task1";
task.FullName = "Task1";
task.Description = "Tasks";
task.HelpOption("-?|-h|--help");
task.OnExecute(() => { task.ShowHelp(); return 0; });
task.Command(
"task1",
data =>
{
data.FullName = "Task1 command";
data.Description = "Task1";
data.OnExecute(() =>
{
// Need to inject
var p = new Task1();
p.Process()
return 0;
});
I need to inject the IExampleRepository into the new Task1()
Task1
public class Task1
{
public Task1()
{
}
private readonly IExampleRepository _exampleRepository;
public Task1(IExampleRepository exampleRepository)
{
_exampleRepository = exampleRepository;
}
public void Process() {
....
}
So basically my understanding is that I register my dependencies, then I should be able to inject them throughout my classes. I'm not sure if I need to pass my serviceProvider down?
I believe that in MVC there is magic that happens to accomplish this. How would I go about injecting without using the service locator pattern?
Basically you don't want to have to pass IServiceProvider to any class except the bootstrapper (Startup) or factory methods/classes as this ties your classes to the specific IoC container.
What you can do is add dependencies to your CommandLineApplication class and resolve it in the Main method and from here you can start your dependency injection chain. This will work as long as you need/want to resolve all of your dependencies at once.
When you get in an situation where you only need to load a subset of it (i.e. using a different service or program logic when a certain parameter is passed), you'll need a kind of factory (a factory is a thin wrapper that creates and configures an object before passing it, in case of IoC it also resolves the dependencies).
In the factory implementation it's okay to reference the container, if necessary (you need scoped dependencies or transient resolving per object creation). You'll also need a factory if you need more than one instance of Task1.
There are two ways. For very simple factories you can use a factory method, that can be directly used while doing your IServiceCollection registrations.
services.AddTransient<Task1>();
services.AddTransient<Func<Task1>>( (serviceProvider) => {
return () => serviceProvider.GetService<Task1>();
});
then inject into your dependency.
public class MyTaskApplication
{
private readonly Func<Task> taskFactory;
public MyApplicationService(Func<Task> taskFactory)
{
this.taskFactory = taskFactory;
}
public void Run()
{
var task1 = taskFactory(); // one instance
var task2 = taskFactory(); // another instance, because its registered as Transient
}
}
If you need more complex configuration or with runtime parameter, it may make more sense to make a factory class.
public class TaskFactory : ITaskFactory
{
private readonly IServiceProvider services;
public TaskFactory(IServiceProvider services)
{
this.services = services;
}
public Task1 CreateNewTask()
{
// get default task service, which is transient as before
// so you get a new instance per call
return services.GetService<Task1>();
}
public Task1 CreateNewTask(string connectionString)
{
// i.e. when having multiple tenants and you want to
// to the task on a database which is only determined at
// runtime. connectionString is not know at compile time because
// the user may choose which one he wants to process
var dbContext = MyDbContext(connectionString);
var repository = new ExampleRepository(dbContext);
return new Task1(repository);
}
}
And the usage
public class MyTaskApplication
{
private readonly ITaskFactory taskFactory;
public MyApplicationService(ITaskFactory taskFactory)
{
this.taskFactory = taskFactory;
}
public void Run()
{
// Default instance with default connectionString from appsettings.json
var task1 = taskFactory.CreateNewTask();
// Tenant configuration you pass in as string
var task2 = taskFactory.CreateNewTask(tenantConnectionString);
}
}
This was my attempt at using your code in a test app, but I'm unsure if I'm doing this correctly.
I'm also unsure about how to pass in the connection string for the method in MyTaskApplication CreateNewTask(connectionString)
Will it need to be passed in as a property, or part of the constructor for MyTaskApplication or an alternative method?
public class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddScoped<Task1>();
services.AddScoped<MyTaskApplication>();
services.AddTransient<ITaskFactory, TaskFactory>();
var serviceProvider = services.BuildServiceProvider();
var m = serviceProvider.GetService<MyTaskApplication>();
m.Run();
}
}
public class TaskFactory : ITaskFactory
{
private readonly IServiceProvider services;
public TaskFactory(IServiceProvider services)
{
this.services = services;
}
public Task1 CreateNewTask()
{
// get default task service, which is transient as before
// so you get a new instance per call
return services.GetService<Task1>();
}
public Task1 CreateNewTask(string connectionString)
{
// i.e. when having multiple tenants and you want to
// to the task on a database which is only determined at
// runtime. connectionString is not know at compile time because
// the user may choose which one he wants to process
//var dbContext = MyDbContext(connectionString);
//var repository = new ExampleRepository(dbContext);
return new Task1(connectionString);
}
}
public interface ITaskFactory
{
Task1 CreateNewTask();
Task1 CreateNewTask(string connectionString);
}
public class MyTaskApplication
{
private readonly ITaskFactory taskFactory;
private string tenantConnectionString;
public MyTaskApplication(ITaskFactory taskFactory)
{
this.taskFactory = taskFactory;
}
public void Run()
{
// Default instance with default connectionString from appsettings.json
var task1 = taskFactory.CreateNewTask();
task1.Process();
// Tenant configuration you pass in as string
var task2 = taskFactory.CreateNewTask(tenantConnectionString);
task2.Process();
Console.WriteLine("Running");
}
}
public class Task1
{
private string _repositoryText;
public Task1()
{
_repositoryText = String.Empty;
}
public Task1(string repositoryText)
{
_repositoryText = repositoryText;
}
public void Process()
{
Console.WriteLine("process: " + _repositoryText);
}
}
I'm trying to use TopShelf together with Quartz.net and Autofac. The code I have below works just fine. However, this line:
cfg.UsingQuartzJobFactory(() => container.Resolve<IJobFactory>());
seems like the wrong way of doing things. Is there a better way of telling Topshelf to use the custom autofac jobfactory? What lifetime scope will the jobfactory have? I'm concerned this line of code is going to cause me some headaches sometime in the future. How do I release the jobfactory when it's no longer needed? Is this line okay as-is?
class Poller : IJob
{
private readonly ILogger _log;
public Poller(ILogger log)
{
_log = log;
_log.Info("Instantiating...");
}
public void Execute(IJobExecutionContext context)
{
_log.Info("Executing...");
}
}
class Program
{
static Autofac.IContainer BuildContainer()
{
var builder = new ContainerBuilder();
builder.RegisterModule<NLogModule>();
builder.RegisterModule<QuartzAutofacFactoryModule>();
builder.RegisterModule(new QuartzAutofacJobsModule(typeof(Poller).Assembly));
var container = builder.Build();
return container;
}
static void Main(string[] args)
{
var container = BuildContainer();
HostFactory.Run(cfg =>
{
cfg.UseNLog();
cfg.UseAutofacContainer(container);
cfg.SetDescription("DESCRIPTION");
cfg.SetDisplayName("DISPLAY");
cfg.SetServiceName("NAME");
cfg.UsingQuartzJobFactory(() => container.Resolve<IJobFactory>());
cfg.ScheduleQuartzJobAsService(q =>
{
q.WithJob(() => JobBuilder.Create<Poller>().Build());
q.AddTrigger(() => TriggerBuilder.Create().WithSimpleSchedule(b => b.WithIntervalInSeconds(20).RepeatForever()).Build());
});
cfg.StartAutomatically();
cfg.RunAsLocalSystem();
});
}
}
For reference: TopShelf.Quartz.ScheduleHobHostConfiguratorExtensions
Also reference: Autofac.Extras.Quartz.QuartzAutofacFactoryModule
I think you should initialize quartz Server with container, this example use unity, but I am sure that work with other containers.
try
{
var container = new UnityContainer();
schedulerFactory = CreateSchedulerFactory();
quartzscheduler = GetScheduler();
SyncPost.Initialize.RepositoryConfig(container);
SyncPost.Initialize.AddToSchedulerContextCustomVars(quartzscheduler, container);
quartzscheduler.JobFactory = new JobFactoryInjection(container);
}
catch (Exception e)
{
logger.Error("Server initialization failed:" + e.Message, e);
throw;
}
where JobFactoryInjection implement IJobFactory:
public class JobFactoryInjection : IJobFactory
{
private readonly UnityContainer container = new UnityContainer();
public JobFactoryInjection(UnityContainer container)
{
if (container == null) throw new ArgumentNullException("container", "Container is null");
this.container = container;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) {
// Return job registrated in container
bundle.JobDetail.JobDataMap.Put(SyncUtils.ContextKeyCenterCode, scheduler.Context.Get(SyncUtils.ContextKeyCenterCode));
return (IJob)container.Resolve(bundle.JobDetail.JobType);
}
public void ReturnJob(IJob job) {
}
}
About JobFactory lifetime, don't worry about it. From Quartz documentation:
"JobFactory simply activates a new instance of the job class. You may want to create your own implementation of JobFactory to accomplish things such as having your application's IoC or DI container produce/initialize the job instance"