내 ASP.NET MVC 응용 프로그램에서 DI 용으로 Autofac을 사용하려고합니다. 그것은 잘 작동하지만 난 Hangfire와 함께 문제를 해결할 수 없습니다. 여기Autofac.Hangfire가 등록 된 유형을 찾지 못했습니다.
내 startup.cs 코드 :
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
GlobalConfiguration.Configuration.UseSqlServerStorage("MyContext");
var container = new AutofacContainer().Container;
var resolver = new AutofacDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.MapSignalR(new HubConfiguration
{
Resolver = resolver
});
AddSignalRInjection(container, resolver);
app.UseHangfireDashboard();
app.UseHangfireServer();
}
private void AddSignalRInjection(IContainer container, IDependencyResolver resolver)
{
var updater = new ContainerBuilder();
updater.RegisterInstance(resolver.Resolve<IConnectionManager>());
updater.RegisterInstance(resolver.Resolve<IMyContext>());
updater.RegisterInstance(resolver.Resolve<ILiveData>());
updater.RegisterInstance(resolver.Resolve<IErp>());
updater.Update(container);
}
}
} 여기
AutofacContainer.cs : 여기
public class AutofacContainer
{
public IContainer Container { get; set; }
public AutofacContainer()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterHubs(Assembly.GetExecutingAssembly()).PropertiesAutowired();
builder.RegisterType<LiveData>().As<ILiveData>().PropertiesAutowired().InstancePerLifetimeScope();
builder.RegisterType<MyContext>().As<IMyContext>().PropertiesAutowired();
builder.RegisterType<Erp>().As<IErp>().PropertiesAutowired();
Container = builder.Build();
GlobalConfiguration.Configuration.UseAutofacActivator(Container);
DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(Container));
}
}
Erp.cs 추출물 :
public interface IErp
{
void InitializeMachines();
}
public class Erp : IErp
{
public IConnectionManager ConnectionManager { get; set; }
public IMyContext _context { get; set; }
public ILiveData _liveData { get; set; }
public Erp(IMyContext context, ILiveData liveData)
{
_context = context;
_liveData = liveData;
}
public void InitializeMachines()
{
// do something
}
}
나는이 방법으로 직업을 추가한다. :
Hangfire.BackgroundJob.Enqueue<Erp>(x => x.InitializeMachines());
나는 다음과 같은 Hangfire 오류 얻을 :
Failed
An exception occurred during processing of a background job.
Autofac.Core.Registration.ComponentNotRegisteredException
The requested service 'MyProject.Classes.Erp' 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.
메시지가 ERP 서비스가 등록되어 있지 않습니다 말한다, 그러나 나는 이유를 이해하지 않습니다. 위에서 본 것처럼 실제로 등록했습니다 :
builder.RegisterType<Erp>().As<IErp>().PropertiesAutowired();
메시지가 다른 것을 말하고 있습니까? 당신은 구체적인 유형 Erp
을 사용하여 작업을 대기열에 Hangfire을 말한
Hangfire.BackgroundJob.Enqueue<Erp>(x => x.InitializeMachines());
:
'.As()'을 제거하면 어떻게됩니까? 또는 그것을 .AsImplementedInterfaces(). AsSelf()로 대체한다면? –
mjwills
콘크리트를 사용하여 작동합니다! 하지만 모두 현실 세계에서 인터페이스를 사용한다고 ... – Mark