0

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.
john Gu
  • 10,469
  • 55
  • 189
  • 381
  • 1
    Perhaps you're missing the startup.cs file? http://stackoverflow.com/questions/20068075/owin-startup-class-missing – nico_c Sep 10 '15 at 12:57
  • @NicolásCarlo ok i defined the startup class and the above problems were removed. but i am getting this error now "JobStorage.Current property value has not been initialized. You must set it before using Hangfire Client or Server API." now i think i am getting this error beucase i did not define the storage location inside the startup class. but in my case i do not want to store the hangfire data on my sql database so is there a way to store it on a text file ? – john Gu Sep 10 '15 at 15:05

1 Answers1

3

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);
Sirwan Afifi
  • 10,031
  • 11
  • 54
  • 104
  • thanks for the reply. but should i place this startup class on a specific location within my project ? for the second question where i need to write this method "RecurringJob.AddOrUpdate(() => CallMethod(), Cron.Hourly);" ?? inside the global.asax ,, can you adivce on these point please ? – john Gu Sep 10 '15 at 14:19
  • 1
    @startup class should be at root of your project. you can write that line of code in your Global.asax. – Sirwan Afifi Sep 10 '15 at 14:20
  • thanks i added the startup class, but seems i am unable to call async tasks inside the "RecurringJob.AddOrUpdate(()" , if you can please check my edit to the original question, as i provided the exact problem ?thanks – john Gu Sep 10 '15 at 14:46
  • As far as I know Hangfire deals only with synchronous method invocations, you can change your method signature to not use async way: http://discuss.hangfire.io/t/async-task-jobs/73 – Sirwan Afifi Sep 10 '15 at 18:26
  • but in my action method can also be invoked manually by end users. so i do not want to change it to be sync ,, not sure if there is a way to fix this ? – john Gu Sep 11 '15 at 00:21