且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-16 15:03:19

想通了这一点,最终



正确使用:

 公共类服务:IService {
公共无效MethodToQueue(){...}
}

公类AnyOtherClass {
公共无效StartTasks(){
BackgroundJob.Enqueue&所述; IService>(X => x.MethodToQueue()); //好
}
}



使用不当(我在做什么错)

 公共类服务:IService {
公共无效StartTasks(){
BackgroundJob.Enqueue(( )= GT; this.MethodToQueue()); //错误
}

公共无效MethodToQueue(){...}
}

公共类AnyOtherClass {
公共AnyOtherClass( IService服务){
service.StartTasks();
}
}


I'm trying to create a simple Hangfire test but it's not working. Here's all the important code, and how I've configured it with the Hangire.Autofac . Not sure what I'm missing here. The exception I'm getting in the /hangfire dashbaord is below also.

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();
        });
    }
}

However in the dashboard I keep getting this error for the task:

Failed An exception occurred during job activation. Autofac.Core.Registration.ComponentNotRegisteredException

The requested service 'App.Services.AmazonSqsService' 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.

Figured this out eventually.

Correct Usage:

public class Service : IService {
      public void MethodToQueue() { ... }
}

public class AnyOtherClass {
     public void StartTasks() {
          BackgroundJob.Enqueue<IService>(x => x.MethodToQueue()); //Good
     } 
}

Incorrect usage (what I was doing wrong)

public class Service : IService {
     public void StartTasks() {
          BackgroundJob.Enqueue(() => this.MethodToQueue()); //Bad
     } 

      public void MethodToQueue() { ... }
}

public class AnyOtherClass {
     public AnyOtherClass(IService service) {
          service.StartTasks();
     }
}