gpt4 book ai didi

c# - Hangfire - 没有为此对象定义无参数构造函数

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:26 25 4
gpt4 key购买 nike

我正在使用 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();

基本上,您可以在我的应用程序中创建契约(Contract)。每次创建契约(Contract)时,我都会使用以下行添加一个新的重复性工作:

RecurringJob.AddOrUpdate(() => AutomaticMovementsCreation(contractToCreate.DebitValue, contractId, descriptionId.Result), Cron.Minutely);

这是要添加 RecurringJob 的类的构造函数:

public ContractServiceImpl(IAuthService authService, IContractStore contractStore)
{
_contractStore = contractStore;
_authService = authService;
}

方法 AutomaticMovementsCreation 正在使用 _contractStore

我认为是依赖注入(inject)的问题,但我真的不知 Prop 体是什么以及如何解决它。顺便说一句,我正在使用 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 是我注册服务和数据存储的地方。

最佳答案

使用以下代码解决了这个问题:

    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);
}
}

关于c# - Hangfire - 没有为此对象定义无参数构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com