Hangfire.Autofac与MVC应用程序 - 注射失败 [英] Hangfire.Autofac with MVC app - injection fails

查看:2496
本文介绍了Hangfire.Autofac与MVC应用程序 - 注射失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的迟发型测试,但它不工作。这里是所有重要的代码,我如何与Hangire.Autofac配置它。不知道我在这里失踪。 。我收到了/迟发型资讯主页上的例外是下面还



 公共类AmazonSqsService:IAmazonSqsService 
$ { b $ b私人只读IBackgroundJobClient _backgroundJobClient;
私人只读ILogService _logService;

公共AmazonSqsService(IBackgroundJobClient backgroundJobClient,ILogService logService)
{

_backgroundJobClient。 = backgroundJobClient;
_logService = logService;
}

公共异步任务<串GT;测试()
{

返回_backgroundJobClient.Enqueue(()=>活套());

}

公共无效的Looper(){
,而(真){_logService.Info(在活套环); Thread.sleep代码(5000); }
}
}

公共部分类启动
{
公共静态的IContainer ConfigureContainer()
{
VAR建设者=新ContainerBuilder();
RegisterApplicationComponents(制造商);
AppGlobal.Container = builder.Build();
}

公共静态无效RegisterApplicationComponents(ContainerBuilder建设者)
{
builder.RegisterType< LogService方式>()为< ILogService方式>()InstancePerLifetimeScope();
builder.RegisterType&所述; AmazonSqsService>()为< IAmazonSqsService方式>()InstancePerLifetimeScope();
builder.RegisterType&所述; BackgroundJobClient>()为< IBackgroundJobClient方式>()InstancePerLifetimeScope();
builder.Register(C => JobStorage.Current)。作为与所述; JobStorage>()InstancePerLifetimeScope();
builder.Register(C =>新建StateMachineFactory(JobStorage.Current))。为<&IStateMachineFactory GT;()InstancePerLifetimeScope()。

}

公共静态无效ConfigureHangfire(IAppBuilder应用程序)
{
app.UseHangfire(配置=>
{
config.UseAutofacActivator(AppGlobal.Container);
config.UseSqlServerStorage(DefaultDatabase);
config.UseServer();
});
}
}



然而,在仪表板我不断收到此错误的任务:




失败的作业激活过程中出现的异常。
Autofac.Core.Registration.ComponentNotRegisteredException



所请求的服务App.Services.AmazonSqsService'尚未注册。为了避免这种例外,无论是注册一个组件提供服务,使用IsRegistered()检查服务注册,或使用ResolveOptional()方法来解决一个可选的依赖。



解决方案

想通了这一点,最终



正确使用:

 公共类服务: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();
     }
}

这篇关于Hangfire.Autofac与MVC应用程序 - 注射失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆