使用 Hangfire 创建重复作业时出错
Error while creating recurring job using Hangfire
我正在尝试在我的应用程序中使用 Hangfire 每天获取(从外部 API)货币兑换率并插入应用程序数据库。
成功配置(这么认为)并且在数据库中创建了 Hangfire Tables 并且作业 table 有条目但是作业没有成功执行并且在检查它显示的状态 Table 时失败并出现类似
的错误消息
{"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();
}
}
接口和 Class 具有要执行的方法
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();
}
}
}
我做错了什么?
感谢任何帮助,在此先感谢。
已经检查过 发布的类似问题,但没有帮助。
正如@Diado 在评论中指出的那样,如果使用接口,HangFire 需要 IoC,因为没有默认支持。或者您可以直接使用 Class 而不是接口。
https://github.com/devmondo/HangFire.SimpleInjector 是我使用的 IoC 注入器之一。
我正在尝试在我的应用程序中使用 Hangfire 每天获取(从外部 API)货币兑换率并插入应用程序数据库。 成功配置(这么认为)并且在数据库中创建了 Hangfire Tables 并且作业 table 有条目但是作业没有成功执行并且在检查它显示的状态 Table 时失败并出现类似
的错误消息{"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();
}
}
接口和 Class 具有要执行的方法
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();
}
}
}
我做错了什么?
感谢任何帮助,在此先感谢。
已经检查过
正如@Diado 在评论中指出的那样,如果使用接口,HangFire 需要 IoC,因为没有默认支持。或者您可以直接使用 Class 而不是接口。
https://github.com/devmondo/HangFire.SimpleInjector 是我使用的 IoC 注入器之一。