John John
John John

Reputation: 1

Unable to run my asp.net mvc web application after installing HangFire

I am working on an asp.net MVC-5 web application, and using nuget i installed the hangfire tool:-

Install-Package Hangfire

but when i run my application i got this exception:-

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. 

second question. if i got the above error fix, how i can call an action method on predefined intervals using hangfire. currently i am defining this inside my glabal.asax as follow:-

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();
        }

----EDIT----------

now after adding the startup.css class , i defined the following inside my 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);
        }

mainly to call an action method named "ScanServer" under the Home controller. now the ScanServer is an async task which have the following defenition :-

public async Task<ActionResult> ScanServer(string tokenfromTMS, string FQDN) 
        {

so my global.asax is raising this error :-

Async methods are not supported. Please make them synchronous before using them in background.

Upvotes: 0

Views: 973

Answers (1)

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

It seems that your OWIN startUp class is missing, So create a class with name Startup:

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

For your second question, if you want to call a method, for example each hour you can use RecurringJob:

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

Upvotes: 3

Related Questions