c# - hangfire 作业激活和 Autofac 的问题

标签 c# autofac hangfire

我正在尝试在我的项目中实现hangfire。当我将 RecurringJob 添加到hangfire 时遇到问题,当它被触发时,我收到此错误:

Autofac.Core.Registration.ComponentNotRegisteredException

The requested service 'XXXX.Services.ScheduleTasks.RepairNotificationSevice' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

我已经安装了hangfire 和hangfire.autofac。我在 Startup.cs 中添加了hangfire 配置,但我的 ContainerBuilder 数据位于另一个文件中。我的startup.cs 文件如下所示:

app.UseHangfire(config =>
        {
            config.UseSqlServerStorage("EmacIntranetEntities");
            config.UseServer();
            var builder = new ContainerBuilder();
            config.UseAutofacActivator(builder.Build());
        });

它不起作用。我已检查在 var builder 和 config.UseAutofacActivator 之间粘贴所有容器构建器数据,但没有结果。我的 Autofac 配置位于从 Global.asax 调用的名为“Bootstrapper.cs”的文件中。我该如何解决这个问题? 谢谢

最佳答案

var builder = new ContainerBuilder();

您有两个独立的 autofac 容器。您需要注册您希望作业能够访问的组件。另请注意,如果您使用任何数据库连接,您将不再处于 Web 模式,并且将无法访问 Web 请求。

builder.RegisterModule<ServicesModule>(); 

builder.RegisterType<XXXX.Services.ScheduleTasks.RepairNotificationSevice>().PropertiesAutowired().

关于c# - hangfire 作业激活和 Autofac 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28236628/

相关文章:

c# - DataSource 中的更新重置了 CheckedListBox 复选框

c# - 在 ASP.NET MVC 2 Web 应用程序中输出具有 JSON 关系的 LINQ to SQL 实体时出现循环引用错误

c# - 列出依赖解析器 DefaultConstructorFinder 的所有缺失的 Autofac 注册

关于 SlidingInvisibilityTimeout 的 Hangfire 澄清

c# - 如何强制 hangfire 服务器在重启时删除该特定服务器的旧服务器数据?

C#统一if语句逻辑

c# - W10 Creators 更新后,UWP MapControl 子项不再固定定位

autofac - 如果在 Autofac 中找不到匹配的寄存器,如何使用默认类进行解析

c# - 通过依赖注入(inject)进行多重导航控制

c# - 通过控制台应用程序设置作业时如何访问hangfire仪表板