我正在开发一个 asp.net MVC-5 Web 应用程序,并使用 nuget 我安装了 hangfire 工具:-
Install-Package Hangfire
但是当我运行我的应用程序时,我得到了这个异常: -
The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- No assembly found containing a Startup or [AssemblyName].Startup class.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.
第二个问题。如果我得到了上述错误修复,我如何使用 hangfire 在预定义的时间间隔调用操作方法。目前我在我的 glabal.asax 中定义如下: -
static void ScheduleTaskTrigger()
{
HttpRuntime.Cache.Add("ScheduledTaskTrigger",
string.Empty,
null,
Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(60)),
CacheItemPriority.NotRemovable,
new CacheItemRemovedCallback(PerformScheduledTasks));
}
static void PerformScheduledTasks(string key, Object value, CacheItemRemovedReason reason)
{
//Your TODO
HomeController h = new HomeController();
var c = h.ScanServer("12345", "allscan");
ScheduleTaskTrigger();
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
ScheduleTaskTrigger();
}
- - 编辑 - - - - -
现在,在添加了 startup.css 类之后,我在 global.asax 中定义了以下内容:-
HomeController h = new HomeController();
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
// ScheduleTaskTrigger();
RecurringJob.AddOrUpdate(() => h.ScanServer("12345","allscan"), Cron.Minutely);
}
主要是调用Home控制器下一个名为“ScanServer”的action方法。现在 ScanServer 是一个异步任务,具有以下定义:-
public async Task<ActionResult> ScanServer(string tokenfromTMS, string FQDN)
{
所以我的 global.asax 引发了这个错误:-
Async methods are not supported. Please make them synchronous before using them in background.