Autofac Hangfire无参数构造函数错误

时间:2017-11-10 06:06:15

标签: c# .net dependency-injection autofac hangfire

我在设置上遇到Hangfire问题。我总是看到这个错误 Hangfire.State表

重试尝试1的10:没有为此obj定义的无参数构造函数... 虽然经过几次重试。它以某种方式自动解决。

我对Autofac并不擅长,只是基本的。我第一次使用Hangfire。

这是我的设置。使用Autofac。

public void Configuration(IAppBuilder app)
{
    GlobalConfiguration.Configuration.UseSqlServerStorage("connectionstring");

    var builder = new ContainerBuilder();

    builder.RegisterType<CSVCompanyAImporter>().Named<ISpecificImporter>("CSVCompanyAImporter").PropertiesAutowired();
    builder.RegisterType<CSVImporter>().Named<IImporter>("CSVImporter").PropertiesAutowired();
    builder.RegisterType<ImportManager>().AsSelf().PropertiesAutowired();

    var container = builder.Build();

    JobActivator.Current = new AutofacJobActivator(container);
    GlobalConfiguration.Configuration.UseAutofacActivator(container);

    app.UseHangfireDashboard();
    app.UseHangfireServer();
}

public class ImportManager
{
    public IImporter Importer { get; set; }
    public IIndex<string, IImporter> Importers { get; set; }
    private bool IsAutofacNamed { get; set; }

    public ImportManager(IImporter importer)
    {
        this.Importer = importer;
        this.IsAutofacNamed = false;
    }

    public ImportManager(IIndex<string, IImporter> importers)
    {
        this.Importers = importers;
        this.IsAutofacNamed = true;
    }

    public void Execute(string importType, string specificImportType)
    {
        if (IsAutofacNamed)
        {
            this.Importer = this.Importers[importType];
            this.Importer.InitializeSpecificImport(specificImportType);
        }

        bool hasFiles = this.Importer.LocateFile();

        if(hasFiles)
        { 
            this.Importer.ReadFile();
            this.Importer.ImportFile();
        }
    }
}

public class CSVImporter : IImporter
{
    public CSVImporter(ISpecificImporter specificImporter)
    {}

    public CSVImporter(IIndex<string, ISpecificImporter> specificImporters)
    {}
}

public class CSVCompanyAImporter : ISpecificImporter
{
    public CSVCompanyAImporter() {}
}

正在调用Hangfire的地方。

protected void btnImport_Click(object sender, EventArgs e)
{
    string importId = "CompanyAImport"

    RecurringJob.AddOrUpdate<ImportManager>(importId, importManager => importManager.Execute("CSVImporter", "CSVCompanyAImporter"), Cron.Minutely);
}

以下是例外情况:

  

{&#34; FailedAt&#34;:&#34; 2017-11-10T06:20:00.1294044Z&#34;&#34; ExceptionType&#34;:&#34; system.missingMethodException而&#34; ,&#34; ExceptionMessage&#34;:&#34;没有为此对象定义无参数构造函数。&#34;,&#34; ExceptionDetails&#34;:&#34; System.MissingMethodException:没有为此定义的无参数构造函数对象。\ r \ n在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,Boolean publicOnly,Boolean noCheck,Boolean&amp; canBeCached,RuntimeMethodHandleInternal&amp; ctor,Boolean&amp; bNeedSecurityCheck)\ r \ n在System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean skipCheckThis System.Activator.CreateInstance(Type type,Boolean nonPublic)中的System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,Boolean skipCheckThis,Boolean fillCache,StackCrawlMark&amp; stackMark)\ r \ n中的布尔fillCache,StackCrawlMark&amp; stackMark)\ r \ n r \ n at System.Activator.CreateInstance(Type type)\ r \ n在Hangfire.JobActivator.ActivateJob(类型jobTyp e)\ r \ n at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)\ r \ n at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)\ r \ n在Hangfire.Server.BackgroundJobPerformer。&lt;&gt; c__DisplayClass8_0 .b__0()\ r \ n在Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter过滤器,PerformingContext preContext,Func 1 continuation)\r\n at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_1.<PerformJobWithFilters>b__2()\r\n at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable 1过滤器)\ r \ n在Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)\ r \ n \ n在Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context,IStorageConnection连接,String jobId)&#34;}

请帮忙。我坚持这个问题。

0 个答案:

没有答案