- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何将 SignalR hub 上下文传递到 ASP .NET Core 2.1 上的 Hangfire 作业?
由于向 Hangfire 传递参数是通过序列化/反序列化完成的,因此 Hangfire 似乎很难重建 SignalR hub 上下文。
我使用以下方式安排作业(在我的 Controller 中):
BackgroundJob.Schedule(() => _hubContext.Clients.All.SendAsync(
"MyMessage",
"MyMessageContent",
System.Threading.CancellationToken.None),
TimeSpan.FromMinutes(2));
然后 2 分钟后,当作业尝试执行时,出现错误:
Newtonsoft.Json.JsonSerializationException: Could not create an instance of type Microsoft.AspNetCore.SignalR.IClientProxy. Type is an interface or abstract class and cannot be instantiated.
有什么想法吗?
更新1
我最终使用了 Startup.cs 中定义的静态上下文,并从 Configure() 分配
hbctx = app.ApplicationServices.GetRequiredService<IHubContext<MySignalRHub>>();
所以现在 Hangfire 安排了一个使用静态上下文的集线器助手:
BackgroundJob.Schedule(() => new MyHubHelper().Send(), TimeSpan.FromMinutes(2));
集线器助手通过 Startup.hbctx
尽管这有效,但有点臭
更新2
我也尝试使用 Access SignalR Hub without Constructor Injection 中的方法:
我的后台作业调度变成:
BackgroundJob.Schedule(() => Startup.GetService().SendOutAlert(2), TimeSpan.FromMinutes(2));
但是这一次,当我到达上面一行时出现了异常:
An unhandled exception has occurred while executing the request System.ObjectDisposedException: Cannot access a disposed object. Object name: 'IServiceProvider'.
更新3
谢谢大家。解决方案是创建一个帮助程序,通过 DI 通过其构造函数获取 hubcontext,然后使用hangfire 将帮助程序方法 Send 安排为后台作业。
public interface IMyHubHelper
{
void SendOutAlert(String userId);
}
public class MyHubHelper : IMyHubHelper
{
private readonly IHubContext<MySignalRHub> _hubContext;
public MyHubHelper(IHubContext<MySignalRHub> hubContext)
{
_hubContext = hubContext;
}
public void SendOutAlert(String userId)
{
_hubContext.Clients.All.SendAsync("ReceiveMessage", userId, "msg");
}
}
然后从任何地方启动后台作业:
BackgroundJob.Schedule<MyHubHelper>( x => x.SendOutAlert(userId), TimeSpan.FromMinutes(2));
最佳答案
Nkosi 的回答建议使用 Schedule<T>
泛型向我指出了我使用的最终解决方案:
首先,我的MySignalRHub只是一个继承自Hub的空类。
public class MySignalRHub
{
}
然后,我创建了一个集线器助手,用于在 MySignalRHub 上维护集线器上下文。 hubcontext 通过 ASP.Net Core 内置 DI 机制注入(inject)到 helper 类中(如 here 所述)。
辅助类:
public class MyHubHelper : IMyHubHelper
{
private readonly IHubContext<MySignalRHub> _hubContext;
public MyHubHelper(IHubContext<MySignalRHub> hubContext)
{
_hubContext = hubContext;
}
public void SendData(String data)
{
_hubContext.Clients.All.SendAsync("ReceiveMessage", data);
}
}
辅助界面:
public interface IMyHubHelper
{
void SendData(String data);
}
最后,我可以使用 Hangfire 从代码中的任何位置安排方法 SendData()
集线器助手作为后台作业:
BackgroundJob.Schedule<MyHubHelper>(h => h.SendData(myData), TimeSpan.FromMinutes(2));
关于c# - 如何将 SignalR hub 上下文传递到 ASP .NET Core 2.1 上的 Hangfire 作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53538213/
我有一个非常复杂的 sql 脚本来清理我的数据库,并且会不时手动运行。脚本运行后,我必须立即启动一个经常性的 hangfire 作业,该作业通常每天只执行一次。 为了不忘记运行脚本,我想直接从sql脚
我有 2 个 hangfire 实例/服务器在运行。两者都指向同一个 hangfire 数据库。如何限制其中一个 hangfire 实例仅从特定队列中获取作业并忽略其他队列? 谢谢 最佳答案 启动实例
我有 2 个 hangfire 实例/服务器在运行。两者都指向同一个 hangfire 数据库。如何限制其中一个 hangfire 实例仅从特定队列中获取作业并忽略其他队列? 谢谢 最佳答案 启动实例
我在使用相同数据库的两台服务器上运行了几个 Hangfire 实例。每个实例根据基于服务器名称的某些标准提交要运行的作业,以便没有两个实例运行相同的作业。我注意到它们正在运行相同的作业,这意味着当一个
nuget 包之间有什么区别 HangFire.AspNetCore和 HangFire和 HangFire.core ? 看来我可以将 hangfire 与 ASP .NET MVC Core 项目
我是 hangfire 的新手,我正在尝试设置一种方法来使用我现有的 serilog 记录器在 asp.net web api 中记录事件。这是我的记录器类: public static class
出于冗余原因,我需要使用相同的数据库运行同一服务的多个实例。 我发现了一些关于“Hangfire 多个实例”的问题,但出于不同的目的,然后是我的:通常是关于在同一数据库上为不同的任务运行多个实例,或与
此 documentation说你可以使用 Queue 指定一个队列要调用的方法的属性。这假设您总是希望在同一个队列上执行一个方法。调用 Enqueue 的进程有没有办法指定将作业放入的队列名称(有效
我有一个现有的 api,它按线程存储数据,并使用 HttpContext.Current 进行检索。 . 我正在尝试重构这个类以从 hangfire 作业中调用——我想知道是否有一个等效的静态方法来检
迁移到 asp.net core 2.1 破坏了我们的 Hangfire 作业设置。 在 Program.cs 的 Main 方法中,我们有类似的内容 var webHost = BuildWebHo
根据 hangfire documentation Starting from Hangfire 1.3.0, you are not required to do anything, if your
我刚刚开始使用Hangfire,现在我很喜欢它。 我了解到,Hangfire将成功工作的历史记录保留了1天,之后将其清除。 有没有一种方法可以自定义此默认行为并将历史记录保留任何持续时间(例如7天)?
我们的网络应用程序允许最终用户在 UI 上设置重复作业的队列。 (我们为每个服务器创建一个队列(使用服务器名称)并允许用户选择要运行的服务器)作业的注册方式: RecurringJob.AddOrUp
我有一个应用程序,具有 Multi-Tenancy 。我想在用户上下文下创建后台作业,但我找不到实现它的好方法。 我将解释一下我的架构。我正在使用包含 UserID 的接口(interface) IC
我想将 QUARTZ 作业转换为 hangfire 我有一个带有 Execute 方法的类。 如何在Hangfire中调用该方法。我尝试类似的事情 public static string CRON_
当使用 Hangfire 排队和处理后台作业时,我能够导致发生可重现的内存不足异常。 . 这些作业是简单的 Console.WriteLine 调用,因此我不希望堆内存以这种方式增加。 我的配置是否不
我有 ASP.NET Web API 应用程序。该应用程序使用 Unity 作为 IoC 容器。该应用程序也在使用 Hangfire,我正在尝试配置 Hangfire 以使用 Unity。 所以基于d
从 1.7.8 版本开始,可以将结果从父作业传递到 Hangfire 中的延续作业。但是没有提供文档或示例。在浏览代码时,我意识到我需要使用带有 pushResults: true 参数的 Conti
有没有人使用过 Hangfire 的多个实例? (在不同的应用程序中)使用相同的 SQL DB 进行配置。因此,我不想为每个 hangfire 实例创建新的 SQL 数据库,而是想与多个实例共享同一个
从 1.7.8 版本开始,可以将结果从父作业传递到 Hangfire 中的延续作业。但是没有提供文档或示例。在浏览代码时,我意识到我需要使用带有 pushResults: true 参数的 Conti
我是一名优秀的程序员,十分优秀!