2014-10-28 6 views
10

विफल रहता है मैं एक साधारण हैंगफायर परीक्षण बनाने की कोशिश कर रहा हूं लेकिन यह काम नहीं कर रहा है। यहां सभी महत्वपूर्ण कोड हैं, और मैंने इसे हैंगियर.ऑटोफैक के साथ कैसे कॉन्फ़िगर किया है। यकीन नहीं है कि मैं यहाँ क्या खो रहा हूँ। मैं/hangfire dashbaord में जो अपवाद प्राप्त कर रहा हूं वह भी नीचे है।एमवीसी ऐप के साथ Hangfire.Autofac - इंजेक्शन

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() विधि का उपयोग करें।

उत्तर

21

अंत में यह पता चला।

सही उपयोग:

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

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

गलत उपयोग (मैं गलत क्या कर रहा था)

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

     public void MethodToQueue() { ... } 
} 

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

मैं वही गलती कर रहा था .. धन्यवाद .. !! – Praveen

संबंधित मुद्दे