简体   繁体   English

如何使用Hangfire呼叫异步动作方法

[英]How can i call an async action method using Hangfire

I am working on an asp.net mvc5 web application, and i installed the Hangfire :- 我正在处理一个asp.net mvc5 Web应用程序,并且我安装了Hangfire:-

Install-Package Hangfire

after that i created a startup.css class as follow:- 之后,我创建了一个startup.css类,如下所示:

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

   }
}

then inside my global.asax file i tried calling 2 action methods ; 然后我在global.asax文件中尝试调用2个操作方法; Index () & ScanServer() , as follow:- 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);

now when Hangfire tried calling the Index() action method which have the following definition :- 现在,当Hangfire尝试调用具有以下定义的Index()操作方法时:

 public ActionResult Index()

i got this error:- 我收到此错误:

JobStorage.Current property value has not been initialized. JobStorage.Current属性值尚未初始化。 You must set it before using Hangfire Client or Server API. 您必须先设置它,然后才能使用Hangfire客户端或服务器API。

while when Hangfire tried calling the ScanServer() action method which is an async Task ,which have the following definition :- 当Hangfire尝试调用ScanServer()操作方法(这是一个异步任务)时,其定义如下:-

 public async Task<ActionResult> ScanServer(string tokenfrom)

i got this error:- 我收到此错误:

Async methods are not supported. 不支持异步方法。 Please make them synchronous before using them in background. 在后台使用它们之前,请使其同步。

so can anyone advice how to fix these 2 issues ? 所以任何人都可以建议如何解决这两个问题?

Thanks 谢谢

EDIT 编辑

i wrote the following inside the Startup class:- 我在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");
        }
    }
}

& here is the connection string:- &这是连接字符串:-

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

but i am still getting this error :- 但我仍然收到此错误:-

JobStorage.Current property value has not been initialized. JobStorage.Current属性值尚未初始化。 You must set it before using Hangfire Client or Server API. 您必须先设置它,然后才能使用Hangfire客户端或服务器API。

You need to configure Hangfire within your Configuration method. 您需要在“配置”方法中配置Hangfire。

[assembly: OwinStartup(typeof(YourApp.Startup))] // Change YourApp to your base namespace
public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseHangfire(config => 
        {
            config.UseSqlServerStorage("NameOfConnectionStringKey"); // Other storage options are available
            config.UseDashboardPath("/hangfire");
            config.UseServer();
        });
    }
}

Basically your first issue is that you haven't configured Hangfire to use a database. 基本上,您的第一个问题是尚未将Hangfire配置为使用数据库。 With the above solution, I'm telling hangfire to use SqlServer as the job storage, passing it the connectionString key that is defined in the web.config. 使用上述解决方案,我告诉hangfire将SqlServer用作作业存储,并向其传递web.config中定义的connectionString键。 If you do not wish to use SQL Server, then you can use other storage options - I've had success with MongoDB in my projects. 如果您不希望使用SQL Server,则可以使用其他存储选项-我的项目在MongoDB方面取得了成功。

I'm also then setting the path to the dashboard, so you can access the pretty UI in your browser. 然后,我还要设置仪表板的路径,以便您可以在浏览器中访问漂亮的UI。

You can also supply your chosen dependency injection here too. 您也可以在此处提供您选择的依赖项注入。

As to your second question, are you able to alter your service method from async to a syncronous method? 关于第二个问题,您是否可以将服务方法从异步方法更改为同步方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM