使用Hangfire创建重复作业时发生错误

问题描述:

我试图在我的应用程序中使用Hangfire从外部API获取货币转换率并在应用程序数据库中插入。 成功配置(是这么认为的)和迟发型表在数据库中创建和工作表是具有项,但没有成功执行的工作和在检查状态表也显示了故障,有一个像使用Hangfire创建重复作业时发生错误

{"FailedAt":"2017-10-12T07:55:00.3075439Z", 
    "ExceptionType":"System.MissingMethodException", 
    "ExceptionMessage":"Cannot create an instance of an interface.", 
    "ExceptionDetails":"System.MissingMethodException: Cannot create an instance of an interface.\r\n 
    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)\r\n 
    at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)\r\n 
    at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)\r\n 
     at System.Activator.CreateInstance(Type type, Boolean nonPublic)\r\n at System.Activator.CreateInstance(Type type)\r\n at Hangfire.JobActivator.ActivateJob(Type jobType)\r\n 
     at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)\r\n at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)\r\n 
     at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_0.<PerformJobWithFilters>b__0()\r\n 
     at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, 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 filters)\r\n 
     at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)\r\n 
     at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)"} 
的错误信息使用

代码:

public partial class Startup 
     { 
      public void Configuration(IAppBuilder app) 
      { 
       ConfigureAuth(app); 
GlobalConfiguration.Configuration.UseSqlServerStorage("ERPContext"); 

       RecurringJob.AddOrUpdate<IAPIRequest>(x => x.ProcessCurrencyConversion(), Cron.MinuteInterval(1)); 
       app.UseHangfireDashboard(); 
       app.UseHangfireServer(); 

      } 
     } 

接口和类有方法执行

public interface IAPIRequest 
    { 
     void ProcessCurrencyConversion(); 
    } 

public class APIRequest : IAPIRequest 
    { 
     public void ProcessCurrencyConversion() 
     { 
      WebClient client = new WebClient(); 

      string urlPattern = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDKWD,EURKWD,GBPKWD,AEDKWD,ZARKWD%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; 

      var jsonResult = client.DownloadString(urlPattern); 
      dynamic results = JsonConvert.DeserializeObject<dynamic>(jsonResult); 
      var rates = results.rate; 
      List<CurrencyJSON> currencies = new List<CurrencyJSON>(); 
      using (var db = new ERPContext()) 
      { 

       foreach (var rate in rates) 
       { 
        var currencyJson = new CurrencyJSON();//POCO 
        currencyJson.Bid = rate.Bid; 
        currencyJson.Name = rate.Name; 

        currencies.Add(currencyJson); 
       } 

       db.Configuration.AutoDetectChangesEnabled = false; 
       db.Configuration.ValidateOnSaveEnabled = false; 

       db.CurrencyJson.ToList().AddRange(currencies); 
       db.SaveChanges(); 
      } 
     } 
    } 

我在做什么wron G? 感谢任何帮助,提前致谢。 已经查看了相似的问题发布了here但没有帮助。

+1

似乎Hangfire默认不支持接口,而是需要一个IoC库将接口映射到类型。请参阅https://discuss.hangfire.io/t/architecture-questions/1167/2和https://discuss.hangfire.io/t/cannot-create-an-instance-of-an-interface/28。 – Diado

正如@Diado在评论中指出的,如果使用接口,HangFire需要IoC,因为没有默认支持。或者你可以直接使用该类而不是一个接口。

https://github.com/devmondo/HangFire.SimpleInjector是我使用的IoC注射器之一。