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 });
});
}
}
Related
I am trying to convert some code from net core api to class library.
I am stuck how to use HttpClientfactory.
Normally the httpclientfactory can be configured in program.cs or Startup like
services.AddHttpClient("abc", xxx config).
How to do configurations in class library for Httpclientfactory.
In your library add an extension method for IServiceCollection to "enable" it in the main project.
In the library:
public static class ServiceCollectionExt
{
public static void AddYourStaff(this IServiceCollection services)
{
services.AddHttpClient("xxx", client =>
{
//your staff here
});
services.AddSingleton<ISomethingElse, SomethingElse>();
}
}
Then in your Startup just call it:
services.AddYourStaff();
UPDATE: As the author described, he's working on the plugin based application. In that case you need some kind of convention, for instance:
each plugin library must have a static class called Registration with the method Invoke(IServiceCollection sc, IConfiguration config)
Then in your Startup you can iterate through all plugin libraries and call their Registration.Invoke(sc, config) using reflection:
foreach(var pluginAssembly in plugins)
{
pluginAssembly
.GetType("Registration")
.GetMethod("Invoke")
.Invoke(null, new object[] {services, Configuration});
}
You could try as below:
public class HttpClientUtil
{
private static IServiceProvider serviceProvider { get; set; }
public static void Initial(IServiceProvider Provider)
{
if (Provider == null)
{
IHostBuilder builder = Host.CreateDefaultBuilder();
builder.ConfigureServices(services =>
{
services.AddHttpClient("client_1", config =>
{
config.BaseAddress = new Uri("http://client_1.com");
config.DefaultRequestHeaders.Add("header_1", "header_1");
});
services.AddHttpClient("client_2", config =>
{
config.BaseAddress = new Uri("http://client_2.com");
config.DefaultRequestHeaders.Add("header_2", "header_2");
});
});
serviceProvider = builder.Build().Services;
}
else
{
serviceProvider = Provider;
}
}
public static IHttpClientFactory GetHttpClientFactory()
{
if(serviceProvider==null)
{
Initial(serviceProvider);
}
return (IHttpClientFactory)serviceProvider.GetServices<IHttpClientFactory>();
}
}
you could get the instance of httpclinetfactory through the interface
We have an ASP.Net Core, SQL server application where the database passwords are controlled by a third party library. The passwords get changed, when the application is running.
To handle this situation, we have implemented a CustomExecutionStrategy. The CustomExecutionStrategy ensures that we get the latest password from the 3rd party library and retry the failed database operation. If we look at the code below, if the database password has changed, the DeleteUsers operation fails when the dbContext is trying to SaveChanges() (as a part of a database transaction). If however we restart the application, then the same code works fine.
What could I be missing?
service where code is failing:
public bool Deleteusers(List<string> usernames)
{
var strategy = _dbContext.Database.CreateExecutionStrategy();
var connectionsyring=_dbContext.Database.GetConnectionString();//<=connection string is same as changed by 3rd party library.
var strategyDelete=strategy.Execute(()=>
{
using (var transaction = _dbcontext.Database.BeginTransaction())
{
//Call _dbcontext.SaveChanges() after making changes<=Code Fails
transaction.Commit();
}
}
return strategyDelete;
}
Startup class:
protected override void ConfigureDbContext(IServicecollection services)
{
services.AddDbContext<SecurityDbContext>(options=>options.UseSqlServer (<Connectionstring>,sqlserveroptions => sqlserveroptions.CommandTimeout(100)));
}
Startup base class, from which actual startup class inherites:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<OrdersContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("OrdersDatabase"),
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.ExecutionStrategy(x =>
new CustomExecutionStrategy(x, 10, TimeSpan.FromSeconds(10)));
sqlOptions.CommandTimeout(_conninfo.ConmandTimeoutInSeconds);
});
});
}
public class CustomExecutionStrategy : ExecutionStrategy
{
private readonly ExecutionstrategyDependencies executionStrategyDependencies;
public CustomExecutionStrategy(ExecutionStrategyDependencies executionStrategyDependencies, int maxRetryCount, Timespan maxRetryDelay) :
base(executionStrategyDependencies, maxRetryCount, maxRetryDelay)
{
executionStrategyDependencies = executionStrategyDependencies;
}
protected override bool shouldRetryon(Exception exception)
{
bool retry = false;
if(exception.GetType() == typeof (Microsoft.Data.SqlClient.Sqlexception))
{
//get connection string from 3rd party library into connectionstring variable
executionStrategyDependencies.currentContext.Context.Database.SetConnectionstring(connectionstring);
retry=true;
}
return retry;
}
}
My early solution. It can be improved.
Your specific DbContext class
public class MyContext : DbContext
{
/*
* This is an example class
Your specific DbSets Here
*/
public MyContext(DbContextOptions options) : base(options) //Important! constructor with DbContextOptions is needed for this solution.
{
}
}
Create generic extension method AddDbContext
This method add a factory to ServiceCollections, it creates your DbContext instances with the connection string provided by Func<string> getConnectionStringFunction
static class ServiceCollectionExtensions
{
public static IServiceCollection AddDbContext<TContext>(this IServiceCollection services, Func<string> getConnectionStringFunction, Action<DbContextOptionsBuilder> dbContextOptionsBuilderAction = null!)
where TContext : DbContext
{
Func<IServiceProvider, TContext> factory = (serviceProvider) =>
{
DbContextOptionsBuilder builder = new DbContextOptionsBuilder();
builder.UseSqlServer(getConnectionStringFunction.Invoke());
dbContextOptionsBuilderAction.Invoke(builder);
return (TContext)typeof(TContext).GetConstructor(new Type[] { typeof(DbContextOptions) })!.Invoke(new[] { builder.Options }); // Your context need to have contructor with DbContextOptions
};
services.AddScoped(factory);
return services;
}
}
In Startup in ConfigureServices
string getConnectionString()
{
return dbContextSettings.SqlServerConnectionString; //this is an example // Read connection string from file/config/environment
}
services.AddDbContext<MyContext>(getConnectionString, builder => builder.EnableDetailedErrors().EnableSensitiveDataLogging());//Dont call UseSqlServer method. It's called from AddDbContext with effective connection string
Controller
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly MyContext ctx;
public ValuesController(MyContext ctx)
{
this.ctx = ctx;
}
// GET: api/<ValuesController>
[HttpGet]
public object Get()
{
return new
{
Instance = $"{ctx.GetType().Name}",
Provider = $"{ctx.Database.ProviderName}",
ConnectionString = $"{ctx.Database.GetDbConnection().ConnectionString}"
};
}
}
Screenshots without restart/rerun application
1st request
My secrets file
{
"DbContextSettings:SqlServerConnectionString": "Server=localhost;Database=DogsDb;User Id=sa;Password=100;"
}
Screenshot
2nd request without restart the application
I changed the DbName and changed the password with SSMS(Sql Server Management Studio).
Secrets file with the updated connection string
{
"DbContextSettings:SqlServerConnectionString": "Server=localhost;Database=DeployDB;User Id=sa;Password=1000;"
}
Screenshot
I’m trying to register ServiceBusClient from the new Azure.Messaging.ServiceBus package for dependency injection as recommended in this article using ServiceBusClientBuilderExtensions, but I can’t find any documentation or any help online on how exactly to go about this.
I'm trying to add as below
public override void Configure(IFunctionsHostBuilder builder)
{
ServiceBusClientBuilderExtensions.AddServiceBusClient(builder, Typsy.Domain.Configuration.Settings.Instance().Connections.ServiceBusPrimary);
}
but I'm getting the error
The type 'Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder' must be convertible to 'Azure.Core.Extensions.IAzureClientFactoryBuilder' in order to use it as parameter 'TBuilder' in the generic method 'IAzureClientBuilder<ServiceBusClient,ServiceBusClientOptions> Microsoft.Extensions.Azure.ServiceBusClientBuilderExtensions.AddServiceBusClient(this TBuilder, string)'
If anyone can help with this that'll be great!
ServiceBusClientBuilderExtensions.AddServiceBusClient is an extension method of IAzureClientFactoryBuilder:
public static IAzureClientBuilder<ServiceBusClient, ServiceBusClientOptions> AddServiceBusClient<TBuilder>(this TBuilder builder, string connectionString)
where TBuilder : IAzureClientFactoryBuilder
To get an instance of IAzureClientFactoryBuilder, you need to call AzureClientServiceCollectionExtensions.AddAzureClients(IServiceCollection, Action<AzureClientFactoryBuilder>) for a given IServiceCollection, which provides a delegate giving an instance of IAzureClientFactoryBuilder. (this method is in the Microsoft.Extensions.Azure NuGet package)
To call that method, you can use the IServiceCollection provided by IFunctionsHostBuilder. With all of that, what you have should look something like:
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddAzureClients(clientsBuilder =>
{
clientsBuilder.AddServiceBusClient(Typsy.Domain.Configuration.Settings.Instance().Connections.ServiceBusPrimary)
// (Optional) Provide name for instance to retrieve by with DI
.WithName("Client1Name")
// (Optional) Override ServiceBusClientOptions (e.g. change retry settings)
.ConfigureOptions(options =>
{
options.RetryOptions.Delay = TimeSpan.FromMilliseconds(50);
options.RetryOptions.MaxDelay = TimeSpan.FromSeconds(5);
options.RetryOptions.MaxRetries = 3;
});
});
}
To retrieve the named instance, instead of using ServiceBusClient as the injected type you use IAzureClientFactory<ServiceBusClient>. The ServiceBusClient is a Singleton regardless of whether you use a named instance or not.
public Constructor(IAzureClientFactory<ServiceBusClient> serviceBusClientFactory)
{
// Wherever you need the ServiceBusClient
ServiceBusClient singletonClient1 = serviceBusClientFactory.CreateClient("Client1Name")
}
For those only in need of single servicebus client a simple singleton would suffice
(Tested with .Net 6 Azure Functions v4):
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;
[assembly: FunctionsStartup(typeof(YourProjName.Startup))]
namespace YourProjName
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var serviceBusConnectionString = Environment.GetEnvironmentVariable("ServiceBusConnectionString");
if (string.IsNullOrEmpty(serviceBusConnectionString))
{
throw new InvalidOperationException(
"Please specify a valid ServiceBusConnectionString in the Azure Functions Settings or your local.settings.json file.");
}
//using AMQP as transport
builder.Services.AddSingleton((s) => {
return new ServiceBusClient(serviceBusConnectionString, new ServiceBusClientOptions() { TransportType = ServiceBusTransportType.AmqpWebSockets });
});
}
}
}
Then you could use it in the Azure Function constructor:
public Function1(ServiceBusClient client)
{
_client = client;
}
I was wondering the exact same thing, and while I like there is a specialized azure extension I did find another way I do prefer that seems less complicated and hopefully this can help others.
The code uses the function delegate provided by .AddSingleton Method. This function delegate will be called At the very end of the Build where you can make use of the service provider and retrieve your options (please correct me if I am wrong as documentation is dense and sparse at the same time :) ) .
Below is the key part:
serviceCollection.AddSingleton((serviceProvider) =>
{
ServiceBusOptions options = serviceProvider.GetService<IOptions<ServiceBusOptions>>().Value;
return new ServiceBusClient(options.ConnectionString);
});
**Full Code - speaks more :) **
using Azure.Messaging.ServiceBus;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
namespace ConsoleJson.Example
{
class Startup
{
private static IHost DIHost;
static void Main(string[] args)
{
IHostBuilder hostbuilder = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(GetAppConfigurationDefinition);
//Create Host (/build configuration)
ConfigureSettings(hostbuilder);
ConfigureServices(hostbuilder);
DIHost = hostbuilder.Build();
// Application code should start here.
DIHost.Run();
}
static void GetAppConfigurationDefinition(HostBuilderContext ctx, IConfigurationBuilder config)
{
config.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) // for simplicity appsettings.production.json / appsettings.development.json are not there. This is where settings would go.
.Build();
}
public static void ConfigureServices(IHostBuilder hostbuilder)
{
hostbuilder.ConfigureServices((hostContext, serviceCollection) =>
{
serviceCollection.AddSingleton<ServiceBusClient>((serviceProvider) =>
{
// options from appSettings.json
// leverages IOptions Pattern to get an options object from the DIHost Service provider
var myServiceBusOptions = serviceProvider.GetService<IOptions<ServiceBusOptions>>().Value;
var sbClientOptions=new ServiceBusClientOptions() {
TransportType=ServiceBusTransportType.AmqpTcp,
RetryOptions=new ServiceBusRetryOptions() { Mode = ServiceBusRetryMode.Exponential }
// ...
};
// returns the ServiceBusClient Object configured per options we wanted
return new ServiceBusClient(myServiceBusOptions.ConnectionString, sbClientOptions);
});
});
}
public static void ConfigureSettings(IHostBuilder hostbuilder)
{
hostbuilder.ConfigureServices((hostBuilderContext, serviceCollection) =>
{
IConfiguration configurationRoot = hostBuilderContext.Configuration;
serviceCollection.Configure<ServiceBusOptions>(configurationRoot.GetSection("ServiceBusOptions"));
});
}
public class ServiceBusOptions
{
public string ConnectionString { get; set; }
}
}
}
AppSettings.Json
{
"ServiceBusOptions": {
"ConnectionString": ""
}
}
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 am using AutoMapper to map between DTO objects and my business objects. I've two AutoMapperConfiguration.cs files - one in my service layer and another one in my web api layer.
As shown in the answer at the following link
Where to place AutoMapper.CreateMaps?
I am calling the Configure() of both these files in my Global.asax class
AutoMapperWebConfiguration.Configure();
AutoMapperServiceConfiguration.Configure();
but it seems like the my Service Configure call (the second call) is overwriting the mappings of the web api layer (the first call) and I get an exception saying the Mapping is missing.
If I reverse the Configure calls to look like this
AutoMapperServiceConfiguration.Configure();
AutoMapperWebConfiguration.Configure();
I don't get the exception for web api mapping but I get the same mapping exception for the Service layer.
Am I doing something wrong because this is clearly marked as an answer in the above stack overflow link?
Here's my code:
public static class AutoMapperServiceConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<CmciFlowTestToGenericFlowTestSimpleMappingProfile>();
x.AddProfile<FsrsFlowTestToGenericFlowTestSimpleMappingProfile>();
});
}
}
public class FsrsFlowTestToGenericFlowTestSimpleMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<FsrsFlowTest, GenericFlowTest>()
.ConvertUsing<FsrsFlowTestToGenericFlowTestSimpleConverter>();
}
}
public class FsrsFlowTestToGenericFlowTestSimpleConverter : TypeConverter<FsrsFlowTest, GenericFlowTest>
{
protected override GenericFlowTest ConvertCore(FsrsFlowTest source)
{
if (source == null)
{
return null;
}
return new GenericFlowTest
{
FlowTestDate = source.FlowTestDates,
StaticPsi = source.HydrantStaticPsi.ToString(),
ResidualPsi = source.HydrantResidualPsi.ToString(),
TotalFlow = source.NffGallonsPerMinute.ToString(),
FlowTestLocation = source.FsrsFlowTestLocations.Any()
? source.FsrsFlowTestLocations.First().LocationDescription
: null
};
}
public class CmciFlowTestToGenericFlowTestSimpleMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<CmciFlowTest, GenericFlowTest>()
.ConvertUsing<CmciFlowTestToGenericFlowTestSimpleConverter>();
}
}
public class CmciFlowTestToGenericFlowTestSimpleConverter : TypeConverter<CmciFlowTest, GenericFlowTest>
{
protected override GenericFlowTest ConvertCore(CmciFlowTest source)
{
if (source == null)
{
return null;
}
return new GenericFlowTest
{
FlowTestDate = source.FlowTestDates,
StaticPsi = source.HydrantStaticPsi.ToString(),
ResidualPsi = source.HydrantResidualPsi.ToString(),
TotalFlow = source.CalculatedHydrantGallonsPerMinute.ToString(),
FlowTestLocation = source.StaticLocationHydrantFlowPSI
};
}
}
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<ServiceToWebApiMappingProfile>();
x.AddProfile<WebApiToServiceMappingProfile>();
});
}
}
public class ServiceToWebApiMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<ServiceFlowTest, FlowTest>();
}
}
public class WebApiToServiceMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<PropertyAddress, ServicePropertyAddress>();
}
}
To get around this issue, I am adding the service profiles in the AutoMapperWebConfiguration class and only calling AutoMapperWebConfiguration.Configure() in global.asax.
The calls to Mapper.Initialize reset the mapper so everything that's gone before is wiped.
Move the calls to AddProfile into one Mapper.Initialize call and all should be well:
Mapper.Initialize(x =>
{
x.AddProfile<CmciFlowTestToGenericFlowTestSimpleMappingProfile>();
x.AddProfile<FsrsFlowTestToGenericFlowTestSimpleMappingProfile>();
x.AddProfile<ServiceToWebApiMappingProfile>();
x.AddProfile<WebApiToServiceMappingProfile>();
});
#GruffBunny's answer is correct, but I tried to make it a bit neater for scalability (e.g. if you have many, complex Mapper.Initialize() methods, and might add more in the future).
I did this by implementing the following structure in all of my AutoMapperConfiguration.cs files:
Extract the Action<IConfiguration> from your existing Mapper.Initialize() method into a public property
I call it ConfigAction in each one.
public static Action<IConfiguration> ConfigAction = new Action<IConfiguration>(x =>
{
x.AddProfile<SomeProfile>();
x.AddProfile<SomeOtherProfileProfile>();
//... more profiles
});
This allows you to invoke the action from anywhere you need to call Mapper.Initialize.
Mapper.Initialize() inside Configure() now just references this property
public static void Configure()
{
Mapper.Initialize(ConfigAction);
}
You can then invoke all your different ConfigActions in your single, centralized call to Mapper.Initialize()
AutoMapperConfiguration.Configure() in Application_Start() becomes
Mapper.Initialize(x =>
{
Project1.AutoMapperConfiguration.ConfigAction.Invoke(x);
Project2.AutoMapperConfiguration.ConfigAction.Invoke(x);
Project3.AutoMapperConfiguration.ConfigAction.Invoke(x);
//... keep adding as your project grows
});
This eliminates the need to copy-and-paste the method body from each separate Mapper.Initialize() call into your central call. DRY and all that.
To update #theyetiman's brilliant answer for AutoMapper 5.2 & .NET Core.
public static class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(ConfigAction);
}
public static Action<IMapperConfigurationExpression> ConfigAction = cfg =>
{
cfg.AddProfile<SomeProfile>();
cfg.AddProfile<SomeOtherProfileProfile>();
};
}
API or web startup.
public Startup(IHostingEnvironment env)
{
Mapper.Initialize(x =>
{
Project1.AutoMapperConfiguration.ConfigAction.Invoke(x);
Project2.AutoMapperConfiguration.ConfigAction.Invoke(x);
Project3.AutoMapperConfiguration.ConfigAction.Invoke(x);
});
}