私は自分の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