Reputation: 163
I am using Hangfire to do some recurring jobs, but I'm getting the following exception:
System.MissingMethodException
No parameterless constructor defined for this object.
System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Hangfire.JobActivator.ActivateJob(Type jobType)
at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)
at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)
at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_0.<PerformJobWithFilters>b__0()
at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_1.<PerformJobWithFilters>b__2()
at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable`1 filters)
at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)
at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)
I have initialized my Hangfire in my AppBuilder:
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(ConfigurationManager.ConnectionStrings[ConfigurationKeys.DbDefaultConnectionName].ConnectionString);
app.UseHangfireDashboard();
app.UseHangfireServer();
Basically, in my application you can create contracts. Everytime a contract is created, I am adding a new recurring job, using the following line:
RecurringJob.AddOrUpdate(() => AutomaticMovementsCreation(contractToCreate.DebitValue, contractId, descriptionId.Result), Cron.Minutely);
This is the construtor of the class where the RecurringJob is being added:
public ContractServiceImpl(IAuthService authService, IContractStore contractStore)
{
_contractStore = contractStore;
_authService = authService;
}
The _contractStore
is being used by the method AutomaticMovementsCreation
.
I think it is something due to dependy injection, but I really do not know what is exacly and how to solve it. By the way I am using an Unity Container to register my application dependencies.
The whole Startup:
public void Configuration(IAppBuilder app)
{
var httpConfiguration = new HttpConfiguration();
ConfigureUnity(app, httpConfiguration);
ConfigureWebApi(httpConfiguration);
ConfigureAuth(app);
//Set configuration into Owin
app.UseWebApi(httpConfiguration);
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(ConfigurationManager.ConnectionStrings[ConfigurationKeys.DbDefaultConnectionName].ConnectionString);
app.UseHangfireDashboard();
app.UseHangfireServer();
}
public void ConfigureUnity(IAppBuilder app, HttpConfiguration config) {
UnityContainer container = UnityConfig.GetConfiguredContainer() as UnityContainer;
if (app.GetDataProtectionProvider() == null) {
app.UseAesDataProtectionProvider();
}
IDataProtectionProvider dataProtectionProvider = app.GetDataProtectionProvider();
container.RegisterInstance(dataProtectionProvider);
app.CreatePerOwinContext(() =>
UnityConfig.GetConfiguredContainer().Resolve<ApplicationUserManager>()
);
config.DependencyResolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());
}
In this method GetConfiguredContainer
is where I register my services and datastores.
Upvotes: 3
Views: 2706
Reputation: 12745
You need to inject the dependencies to get this working. Install nuget unity package :
Install-Package Hangfire.Unity
And then register on Global.asax you will have BootStraper initialise method.Navigate to boot strapper class and in initialise have following code,
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
Complete code will look something like following if you are using Unity.
public static class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
GlobalConfiguration.Configuration.UseUnityActivator(container);
RegisterTypes(container);
return container;
}
Upvotes: 0
Reputation: 163
Solved this using the following code:
Hangfire.GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(container));
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(ConfigurationManager.ConnectionStrings[ConfigurationKeys.DbDefaultConnectionName].ConnectionString);
app.UseHangfireDashboard();
app.UseHangfireServer();
public class ContainerJobActivator : JobActivator
{
private IUnityContainer _container;
public ContainerJobActivator(UnityContainer container)
{
_container = container;
}
public override object ActivateJob(Type type)
{
return _container.Resolve(type);
}
}
Upvotes: 2
Reputation: 3455
First, you need to create an instance of the UnityContainer (at startup), load configuration from web.config, and finally associated the container with your HttpConfiguration.
I Dont remember the code exaxtly, but it goes like this:
var container = new UnityContainer();
container.LoadConfiguration();
config = new HttpConfiguration();
config.DependencyResolver = new UnityDependencyResolver(container);
app.UseWebApi(config); // Something like that
Upvotes: 1