如何使用Hangfire

时间:2015-09-10 15:36:12

标签: c# asp.net asynchronous asp.net-mvc-5 hangfire

我正在开发一个asp.net mvc5 web应用程序,我安装了Hangfire: -

Install-Package Hangfire

之后我创建了一个startup.css类,如下所示: -

public class Startup
{
   public void Configuration(IAppBuilder app)
   {

   }
}

然后在我的global.asax文件中我尝试调用2个动作方法; Index ()& ScanServer(),如下: -

 HomeController h = new HomeController();
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);


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

&安培;

 RecurringJob.AddOrUpdate(() =>  h.ScanServer(*****), Cron.Minutely);

现在当Hangfire尝试调用具有以下定义的Index()操作方法时: -

 public ActionResult Index()

我收到了这个错误: -

  

JobStorage.Current属性值尚未初始化。你必须   在使用Hangfire客户端或服务器API之前设置它。

当Hangfire尝试调用ScanServer()操作方法时,该方法是异步任务,具有以下定义: -

 public async Task<ActionResult> ScanServer(string tokenfrom)

我收到了这个错误: -

  

不支持异步方法。请让它们同步   在后台使用它们。

那么有人可以建议如何解决这两个问题吗?

由于

修改

我在Startup类中写了以下内容: -

using Hangfire;
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScanningFinal;
[assembly: OwinStartup(typeof(Startup))]
namespace ScanningFinal
{

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

&安培;这是连接字符串: -

 <add name="scanservice"    connectionString="data source=localhost;initial catalog=ScanningService;integrated security=True" providerName="System.Data.SqlClient"/>

但我仍然收到此错误: -

  

JobStorage.Current属性值尚未初始化。你必须   在使用Hangfire客户端或服务器API之前设置它。

0 个答案:

没有答案