【问题标题】:Unable to run my asp.net mvc web application after installing HangFire安装 HangFire 后无法运行我的 asp.net mvc Web 应用程序
【发布时间】: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.

【问题讨论】:

  • @NicolásCarlo 好的,我定义了启动类,上面的问题被删除了。但我现在收到此错误“JobStorage.Current 属性值尚未初始化。您必须在使用 Hangfire 客户端或服务器 API 之前设置它。”现在我想我收到了这个错误,因为我没有在启动类中定义存储位置。但就我而言,我不想将hangfire数据存储在我的sql数据库中,所以有没有办法将它存储在文本文件中?

标签: asp.net asp.net-mvc asp.net-mvc-5 hangfire


【解决方案1】:

您的 OWIN 启动类似乎丢失了,所以创建一个名为 Startup 的类:

public class Startup
{
   public void Configuration(IAppBuilder app)
   {
      //..codes
   }
}

对于第二个问题,如果你想调用一个方法,例如每小时你可以使用RecurringJob

RecurringJob.AddOrUpdate(() => CallMethod(), Cron.Hourly);

【讨论】:

  • 感谢您的回复。但是我应该把这个启动类放在我项目中的特定位置吗?对于我需要编写此方法的第二个问题“RecurringJob.AddOrUpdate(() => CallMethod(), Cron.Hourly);” ??在 global.asax 中,你能就这些问题提出建议吗?
  • @startup 类应该位于项目的根目录。您可以在 Global.asax 中编写该行代码。
  • 感谢我添加了启动类,但似乎我无法在 "RecurringJob.AddOrUpdate(()" 中调用异步任务,如果可以请检查我对原始问题的编辑,正如我提供的确切的问题?谢谢
  • 据我所知,Hangfire 只处理同步方法调用,您可以将方法签名更改为不使用异步方式:discuss.hangfire.io/t/async-task-jobs/73
  • 但在我的操作方法中也可以由最终用户手动调用。所以我不想把它改成同步,不知道有没有办法解决这个问题?
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-25
相关资源
最近更新 更多