标签: hangfire

如何在Hangfire中调用异步方法?

我有asp.net核心API应用程序,这是我第一次使用HangFire.

在.Net Core应用程序中,我的所有方法都是异步的.基于SO Post,wait()在hangfire中调用异步方法时使用它并不是一个好主意.
另外,根据v1.6.0中的hangfire支持问题,添加了异步支持.我使用的是1.6.12版,但我仍然没有看到异步支持.

我如何调用异步方法Enqueue.目前我正在使用wait()

public class MyController : Controller
{
    private readonly Downloader _downlaoder;
    private readonly IBackgroundJobClient _backgroungJobClient;
    public MyController(Downloader downloader, IBackgroundJobClient backgroungJobClient)
    {
        _downlaoder = downloader;
        _backgroungJobClient = backgroungJobClient;
    }

    [HttpPost]
    public void Post([FromBody]IEnumerable<string> files)
    {
        _backgroungJobClient.Enqueue(() => _downloader.DownloadAsync(files).Wait());
    }
}
Run Code Online (Sandbox Code Playgroud)

c# hangfire .net-core asp.net-core

15
推荐指数
1
解决办法
9536
查看次数

Hangfire DisableConcurrentExecution:超时到期后会发生什么?

根据Hangfire 0.8.2公告帖子,Hangfire有一个DisableConcurrentExecution过滤器,当应用于某个方法时,会阻止该方法的多个实例同时执行.

DisableConcurrentExecution过滤器采用一个timeoutInSecondsint参数.从链接文章中的示例:

[DisableConcurrentExecution(timeoutInSeconds: 10 * 60)]
public void SomeMethod()
{
    // Operations performed inside a distributed lock
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:当一个等待获取锁定DisableConcurrentExecution的作业的时候,作业等待的时间超过了这个timeoutInSeconds值,会发生什么?

c# hangfire

14
推荐指数
1
解决办法
9071
查看次数

ASP.NET中的Akka.NET actor系统

我在ASP.NET中使用RESTful API创建了一个服务,该服务托管在IIS中.在这个服务中,我想用Akka.NET创建一个actor系统.

在创建actor系统时:

var actorSystem = ActorSystem.Create("myActorSystem");
Run Code Online (Sandbox Code Playgroud)

抛出以下异常:

System.Web.dll中发生类型为"System.InvalidOperationException"的第一次机会异常附加信息:此时无法启动异步操作.异步操作只能在异步处理程序或模块中启动,或者在页面生命周期中的某些事件中启动.如果在执行页面时发生此异常,请确保将页面标记为<%@ Page Async ="true"%>.此异常还可能表示尝试调用"异步void"方法,该方法通常在ASP.NET请求处理中不受支持.相反,异步方法应该返回一个Task,调用者应该等待它.

actor系统本质上是一个并发系统,在actor之间交换异步消息.正如这里所解释的那样,这个actor系统无法在IIS中取消AppDomain,这可能就是为什么抛出上述异常的原因.

本文介绍如何在ASP.NET中运行后台任务.但是,我不知道如何将它用于我的actor系统,因为我无法控制可能由Akka.NET创建的后台任务的生命周期.

有没有办法使这项工作,或者我应该放弃在ASP.NET应用程序中使用actor系统的想法?

编辑:我还在Stackoverflow上看到一个关于使用Akka实现REST服务的问题.关于类似于Spray工具包的解决方案的任何建议,但欢迎为Akka.NET工作.

asp.net spray akka.net hangfire

13
推荐指数
2
解决办法
5770
查看次数

Hangfire - 防止多个相同的工作被排队

设想:

作业 1计划每 5 分钟运行一次,大约需要 1 分钟才能完成。

大量工作堆积如山,作业 1需要 15 分钟才能运行。

现在同时处理三个作业 1 - 我不想要这个。


如果作业 1已经存在,如何防止它再次添加到队列中?

是否有 Hangfire 设置,或者我是否需要手动轮询作业状态?

c# asp.net hangfire

13
推荐指数
5
解决办法
2万
查看次数

Hangfire.Autofac与MVC应用程序 - 注入失败

我正在尝试创建一个简单的Hangfire测试,但它无法正常工作.这是所有重要的代码,以及我如何使用Hangire.Autofac配置它.不知道我在这里缺少什么.我在/ hangfire dashbaord中获得的例外也在下面.

public class AmazonSqsService : IAmazonSqsService
{
    private readonly IBackgroundJobClient _backgroundJobClient;
    private readonly ILogService _logService;

    public AmazonSqsService(IBackgroundJobClient backgroundJobClient, ILogService logService) 
    {

        _backgroundJobClient. = backgroundJobClient;
        _logService= logService;
    }

    public async Task<string> Test()
    {

        return _backgroundJobClient.Enqueue(() => Looper());

    }

    public void Looper() {
        while (true) { _logService.Info("In Looper Loop"); Thread.Sleep(5000); } 
    } 
}

 public partial class Startup
{
    public static IContainer ConfigureContainer()
    {
        var builder = new ContainerBuilder();
        RegisterApplicationComponents(builder);
        AppGlobal.Container = builder.Build();
    }

    public static void RegisterApplicationComponents(ContainerBuilder builder)
    {
        builder.RegisterType<LogService>().As<ILogService>().InstancePerLifetimeScope(); …
Run Code Online (Sandbox Code Playgroud)

c# hangfire

12
推荐指数
1
解决办法
3510
查看次数

具有返回值的Hangfire背景作业

我正在从Task.Run切换到Hangfire.在.NET 4.5+中Task.Run可以返回Task<TResult>,它允许我运行返回除以外的任务void.我通常可以通过访问该属性等待并获得我的任务结果MyReturnedTask.Result

我的旧代码示例:

public void MyMainCode()
{
    List<string> listStr = new List<string>();
    listStr.Add("Bob");
    listStr.Add("Kate");
    listStr.Add("Yaz");

    List<Task<string>> listTasks = new List<Task<string>>();

    foreach(string str in listStr)
    {
        Task<string> returnedTask = Task.Run(() => GetMyString(str));
        listTasks.Add(returnedTask);
    }

    foreach(Task<string> task in listTasks)
    {
        // using task.Result will cause the code to wait for the task if not yet finished.
        // Alternatively, you can use Task.WaitAll(listTasks.ToArray()) to wait for all tasks in the list to …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asynchronous hangfire

12
推荐指数
1
解决办法
1万
查看次数

Hangfire Dashboard授权配置不起作用

我已经下载了nu-get包 Hangfire.Dashboard.Authorization

我正在尝试按照以下文档配置基于OWIN的授权,但我得到intellisense错误 DashboardOptions.AuthorizationFilters is obsolete please use Authorization property instead

我也得到intellisense错误 The type or namespace AuthorizationFilter and ClaimsBasedAuthorizationFilterd not be found

using Hangfire.Dashboard;
using Hangfire.SqlServer;
using Owin;
using System;

namespace MyApp
{
    public class Hangfire
    {
       public static void ConfigureHangfire(IAppBuilder app)
        {
           GlobalConfiguration.Configuration
           .UseSqlServerStorage(
               "ApplicationDbContext",
                new SqlServerStorageOptions 
                  { QueuePollInterval = TimeSpan.FromSeconds(1) });

           var options = new DashboardOptions
           {
               AuthorizationFilters = new[]
               {
                  new AuthorizationFilter { Users = "admin, superuser", Roles = "advanced" },
                  new ClaimsBasedAuthorizationFilter("name", "value")
               }
           };

           app.UseHangfireDashboard("/hangfire", …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc owin hangfire

12
推荐指数
2
解决办法
9127
查看次数

在Hangfire中设置JWT承载令牌授权/认证

如何在Hangfire中配置承载令牌授权/认证?

我有一个自定义身份验证筛选器,在初始请求时读取身份验证令牌,但所有其他请求(Hangfire调用)返回401.

如何将Auth Token附加到Hangfire所执行的每个请求的标头中?

如何在令牌过期时刷新令牌?

c# authentication jwt bearer-token hangfire

12
推荐指数
1
解决办法
1002
查看次数

如何为每天执行的hangfire作业创建cron表达式

我是cron表达的新手.我需要知道如何在Hangfire中创建cron,每隔1天在下午5点,凌晨1点,下午2点45分执行

了解Hangfire也接受标准的CronExpression,我已经尝试过探索这个频率的cron表达式,但是找不到它 - https://en.wikipedia.org/wiki/Cron 我知道如何在15分钟内完成?*/15****我需要每天运行它.

cron hangfire

12
推荐指数
1
解决办法
1万
查看次数

在HangFire中设置"按需"唯一作业

在Hangfire中,我已经成功设置了定期作业,并且如果我愿意,我可以手动触发,这要归功于Web UI及其"触发器"按钮.

RecurringJob.AddOrUpdate(..);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是我愿意设置一个永远不会自动解雇的工作.仅限WebUi的需求.将其视为一组仅在需要时触发的维护任务.手动.

我想在await状态中添加一个非重新安排的Job,但是无法(听起来不对).

Hangfire可以"仅按需"工作吗?

c# hangfire

11
推荐指数
4
解决办法
4636
查看次数