-2

I am working on an asp.net mvc-5 web application. and i install the hangfire tool inside my web application using nuget tool.

https://www.nuget.org/packages/Hangfire/

Then i create the following startup.cs class, to call a method each minute as follow:-

 public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            GlobalConfiguration.Configuration
                .UseSqlServerStorage("scanservice");


            ScanningService ss = new ScanningService();
            RecurringJob.AddOrUpdate(() => ss.HypervisorScan("allscan"), Cron.Minutely);

        }

    }

and here is the definition of the method that will be called :-

public async Task<ScanResult> HypervisorScan(string FQDN)
{

but currently i deploy my application on IIS 7.5 , and the method is not being called at all . so can anyone adivce on this please ?

Thanks

John John
  • 1
  • 72
  • 238
  • 501
  • 2
    If you set a breakpoint in your `Startup` class, does it get reached? If you add the Hangfire dashboard (`app.UseHangfireDashboard();`) and then access the `~/hangfire` URL, do you see the job there? Do you have the OwinStartupAttribute (`[assembly: OwinStartup(typeof(MyWebApplication.Startup))]`) in your Startup class? – mason Sep 17 '15 at 02:14
  • @mason why i need to add the OwinStartupAttribute ? i did not get any error ,,, or this is mandetory to add even if VS did not raise any error ? – John John Sep 17 '15 at 11:51
  • after adding the referecne i am getting the following exception "Async methods are not supported. Please make them synchronous before using them in background." ,, could this be related that the metohd i am calling is async ? – John John Sep 17 '15 at 11:56

1 Answers1

1

You're missing the OwinStartupAttribute from your class. Add it. That tells OWIN where the code is to run at startup.

Also, you can't run async methods directly in Hangfire as the error clearly states. So wrap the method with a Wait call and pass that to Hangfire.

Lastly, you should stick with the convention that Async methods should end in the suffix Async. Rename ScanningService.HypervisorScan to ScanningService.HypervisorScanAsync

[assembly: OwinStartup(typeof(MyWebApplication.Startup))]
namespace MyWebApplication
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            GlobalConfiguration.Configuration
                .UseSqlServerStorage("scanservice");    


            RecurringJob.AddOrUpdate(() => HypervisorScan(), Cron.Minutely);
        }

       public void HypervisorScan()
       {
           ScanningService ss = new ScanningService();
           ss.HypervisorScanAsync("allscan").Wait();
       }
    }
}
mason
  • 31,774
  • 10
  • 77
  • 121
  • but at the end using .Wait() will occupy the thread as it is a blocking operation.so it is not recommended to use .Wait() as it is a blocking method , unlike using await which is 100% async .. so seems if i use hangfire i will have to break the async behavioure for my methods.thanks – John John Sep 17 '15 at 14:06
  • @johnG Hangfire doesn't *need* async methods, because by its very definition it runs things asynchronously. When you `AddOrUpdate` a job, it takes the method call from the lambda and adds it to it's list of scheduled jobs. So that's asynchronous, just no need to make use of the C# async concept. – mason Sep 17 '15 at 14:08
  • but the problem is with this "ss.HypervisorScanAsync("allscan").Wait();" as using wait() in this way means that the iis thread will be occupied till the end of the operation,, – John John Sep 17 '15 at 14:59