Hangfire - 没有为此对象定义无参数构造函数
Hangfire - No parameterless constructor defined for this object
我正在使用 Hangfire 做一些重复性工作,但出现以下异常:
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)
我已经在我的 AppBuilder 中初始化了我的 Hangfire:
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(ConfigurationManager.ConnectionStrings[ConfigurationKeys.DbDefaultConnectionName].ConnectionString);
app.UseHangfireDashboard();
app.UseHangfireServer();
基本上,您可以在我的应用程序中创建合同。每次创建合同时,我都会使用以下行添加一个新的重复性工作:
RecurringJob.AddOrUpdate(() => AutomaticMovementsCreation(contractToCreate.DebitValue, contractId, descriptionId.Result), Cron.Minutely);
这是要添加 RecurringJob 的 class 的构造函数:
public ContractServiceImpl(IAuthService authService, IContractStore contractStore)
{
_contractStore = contractStore;
_authService = authService;
}
方法 AutomaticMovementsCreation
正在使用 _contractStore
。
我觉得是依赖注入的问题,但是我真的不知道具体是什么以及如何解决。顺便说一句,我正在使用 Unity 容器来注册我的应用程序依赖项。
整个启动:
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());
}
在这种方法中 GetConfiguredContainer
是我注册服务和数据存储的地方。
首先,您需要创建一个 UnityContainer 实例(在启动时),从 web.config 加载配置,最后将容器与您的 HttpConfiguration 相关联。
我不太记得代码了,但它是这样的:
var container = new UnityContainer();
container.LoadConfiguration();
config = new HttpConfiguration();
config.DependencyResolver = new UnityDependencyResolver(container);
app.UseWebApi(config); // Something like that
使用以下代码解决了这个问题:
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);
}
}
您需要注入依赖项才能使其正常工作。
安装 nuget 统一包:
Install-Package Hangfire.Unity
然后在 Global.asax 上注册,您将让 BootStraper 初始化 method.Navigate 以启动 strapper class 并在初始化中有以下代码,
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
如果您使用的是 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;
}
我正在使用 Hangfire 做一些重复性工作,但出现以下异常:
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)
我已经在我的 AppBuilder 中初始化了我的 Hangfire:
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(ConfigurationManager.ConnectionStrings[ConfigurationKeys.DbDefaultConnectionName].ConnectionString);
app.UseHangfireDashboard();
app.UseHangfireServer();
基本上,您可以在我的应用程序中创建合同。每次创建合同时,我都会使用以下行添加一个新的重复性工作:
RecurringJob.AddOrUpdate(() => AutomaticMovementsCreation(contractToCreate.DebitValue, contractId, descriptionId.Result), Cron.Minutely);
这是要添加 RecurringJob 的 class 的构造函数:
public ContractServiceImpl(IAuthService authService, IContractStore contractStore)
{
_contractStore = contractStore;
_authService = authService;
}
方法 AutomaticMovementsCreation
正在使用 _contractStore
。
我觉得是依赖注入的问题,但是我真的不知道具体是什么以及如何解决。顺便说一句,我正在使用 Unity 容器来注册我的应用程序依赖项。
整个启动:
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());
}
在这种方法中 GetConfiguredContainer
是我注册服务和数据存储的地方。
首先,您需要创建一个 UnityContainer 实例(在启动时),从 web.config 加载配置,最后将容器与您的 HttpConfiguration 相关联。
我不太记得代码了,但它是这样的:
var container = new UnityContainer();
container.LoadConfiguration();
config = new HttpConfiguration();
config.DependencyResolver = new UnityDependencyResolver(container);
app.UseWebApi(config); // Something like that
使用以下代码解决了这个问题:
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);
}
}
您需要注入依赖项才能使其正常工作。 安装 nuget 统一包:
Install-Package Hangfire.Unity
然后在 Global.asax 上注册,您将让 BootStraper 初始化 method.Navigate 以启动 strapper class 并在初始化中有以下代码,
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
如果您使用的是 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;
}