Since the order of the allowed modules is not guaranteed, I have some problems with achieving this:
I have a module that registers a ScheduleService , this ScheduleService is responsible for trigger events at set intervals, etc.
I can load into different IScheduable elements that I do using XML Configuration . The problem that I have is IScheduable elements, requiring IScheduleService be ready so that it can register it on its own.
So in my <autofac><modules> I have
<module type="Namespace.ScheduleServiceModule, Namespace" />
Then the idea was that I could load so many different ISchedulable elements
<module type="SomeNamespace.ScheudleItem1, SomeNamespace /> <module type="SomeNamespace.ScheudleItem2, SomeNamespace /> <module type="SomeNamespace.ScheudleItem3, SomeNamespace /> <module type="SomeNamespace.ScheudleItem4, SomeNamespace />
I am currently doing this in these scheditem modules:
protected override void Load(ContainerBuilder builder) { builder.RegisterCallback(registry => { var scheduleService = new TypedService(typeof(IScheduleService)); var registrations = registry.RegistrationsFor(scheduleService); if (registrations != null && registrations.Any()) { IComponentRegistration componentRegistration = registrations.First(); componentRegistration.Activated += (sender, args) => { IScheduleService scheduleService = args.Instance as IScheduleService; if (scheduleService != null) { OnScheduleServiceAvailable(args.Context, scheduleService); } }; } }); base.Load(builder); }
This is an override in each of the ScheduleItems.
protected override void OnScheduleServiceAvailable(IComponentContext context, IScheduleService scheduleService) { scheduleService.Add( new SqlSyncSchedulable(Enabled, IntervalMS, ConnectionString, SqlSelect, context.Resolve<ILoggerService>(), context.Resolve<IPersonService>(), context.Resolve<ILoggingEventService>(), context.Resolve<ITemplateService>(), context.Resolve<ITemplateLoggingEventService>(), context.Resolve<IRuntimeSettingsService>())); }
This is pretty choppy. The ISchedule element must be registered, but the problem is that the Schedule service may be registered after these elements.
Should there be a way to achieve this?