我正在尝试创建一个简单的 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();
builder.RegisterType<AmazonSqsService>().As<IAmazonSqsService>().InstancePerLifetimeScope();
builder.RegisterType<BackgroundJobClient>().As<IBackgroundJobClient>().InstancePerLifetimeScope();
builder.Register(c => JobStorage.Current).As<JobStorage>().InstancePerLifetimeScope();
builder.Register(c => new StateMachineFactory(JobStorage.Current)).As<IStateMachineFactory>().InstancePerLifetimeScope();
}
public static void ConfigureHangfire(IAppBuilder app)
{
app.UseHangfire(config =>
{
config.UseAutofacActivator(AppGlobal.Container);
config.UseSqlServerStorage("DefaultDatabase");
config.UseServer();
});
}
}
但是在仪表板中,我不断收到该任务的此错误:
失败 作业激活期间发生异常。Autofac.Core.Registration.ComponentNotRegisteredException
请求的服务“App.Services.AmazonSqsService”尚未注册。要避免此异常,请注册一个组件以提供服务,使用 IsRegistered() 检查服务注册,或使用 ResolveOptional() 方法解决可选依赖项。