2017-10-12 74 views
0

我試圖在我的應用程序中使用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

回答

相關問題