【发布时间】:2015-09-10 12:49:46
【问题描述】:
我正在开发一个 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.
【问题讨论】:
-
也许您缺少 startup.cs 文件? stackoverflow.com/questions/20068075/owin-startup-class-missing
-
@NicolásCarlo 好的,我定义了启动类,上面的问题被删除了。但我现在收到此错误“JobStorage.Current 属性值尚未初始化。您必须在使用 Hangfire 客户端或服务器 API 之前设置它。”现在我想我收到了这个错误,因为我没有在启动类中定义存储位置。但就我而言,我不想将hangfire数据存储在我的sql数据库中,所以有没有办法将它存储在文本文件中?
标签: asp.net asp.net-mvc asp.net-mvc-5 hangfire