2020using Umbraco . Cms . Core . Composing ;
2121using Umbraco . Cms . Core . DependencyInjection ;
2222using Umbraco . Cms . Core . Services ;
23+ using Umbraco . Cms . Core . Web ;
2324using Umbraco . Cms . Web . BackOffice . Authorization ;
2425using Umbraco . Cms . Web . Common . ApplicationBuilder ;
2526using UmbracoConstants = Umbraco . Cms . Core . Constants ;
@@ -93,10 +94,12 @@ private void AddAuthorizedUmbracoDashboard(IUmbracoBuilder builder) {
9394 public class RegisterRecurringJobsComponent : IComponent {
9495 private readonly IRuntimeState _runtimeState ;
9596 private readonly Lazy < IMediator > _mediator ;
97+ private readonly Lazy < IUmbracoContextFactory > _umbracoContextFactory ;
9698
97- public RegisterRecurringJobsComponent ( IRuntimeState runtimeState , Lazy < IMediator > mediator ) {
99+ public RegisterRecurringJobsComponent ( IRuntimeState runtimeState , Lazy < IMediator > mediator , Lazy < IUmbracoContextFactory > umbracoContextFactory ) {
98100 _runtimeState = runtimeState ;
99101 _mediator = mediator ;
102+ _umbracoContextFactory = umbracoContextFactory ;
100103 }
101104
102105 public void Initialize ( ) {
@@ -106,35 +109,38 @@ public void Initialize() {
106109 . ToList ( ) ;
107110
108111 if ( recurringJobTypes . Any ( ) ) {
109- var jobReqs = new List < QueueRecurringJobReq > ( ) ;
112+ using ( _umbracoContextFactory . Value . EnsureUmbracoContext ( ) ) {
113+ var jobReqs = new List < QueueRecurringJobReq > ( ) ;
110114
111- foreach ( var jobType in recurringJobTypes ) {
112- if ( ! jobType . ImplementsGenericInterface ( typeof ( IRequestHandler < , , > ) ) ) {
113- throw new Exception ( "Recurring job attribute can only be applied to classes that inherit IRequestHandler<,,>" ) ;
114- }
115+ foreach ( var jobType in recurringJobTypes ) {
116+ if ( ! jobType . ImplementsGenericInterface ( typeof ( IRequestHandler < , , > ) ) ) {
117+ throw new
118+ Exception ( "Recurring job attribute can only be applied to classes that inherit IRequestHandler<,,>" ) ;
119+ }
115120
116- var attribute = jobType . GetCustomAttribute < RecurringJobAttribute > ( ) ;
117- var requestType = jobType . GetParameterTypesForGenericInterface ( typeof ( IRequestHandler < , , > ) )
118- . First ( ) ;
121+ var attribute = jobType . GetCustomAttribute < RecurringJobAttribute > ( ) ;
122+ var requestType = jobType . GetParameterTypesForGenericInterface ( typeof ( IRequestHandler < , , > ) )
123+ . First ( ) ;
119124
120- var triggerKey = TriggerKey . Generate ( requestType , typeof ( None ) ) ;
125+ var triggerKey = TriggerKey . Generate ( requestType , typeof ( None ) ) ;
121126
122- var jobReq = new QueueRecurringJobReq ( ) ;
127+ var jobReq = new QueueRecurringJobReq ( ) ;
123128
124- jobReq . CronExpression = attribute . CronExpression ;
125- jobReq . JobName = attribute . JobName ;
126- jobReq . TriggerKey = triggerKey ;
129+ jobReq . CronExpression = attribute . CronExpression ;
130+ jobReq . JobName = attribute . JobName ;
131+ jobReq . TriggerKey = triggerKey ;
127132
128- jobReqs . Add ( jobReq ) ;
129- }
133+ jobReqs . Add ( jobReq ) ;
134+ }
130135
131- var req = new RegisterRecurringJobsReq ( ) ;
132- req . Jobs = jobReqs ;
136+ var req = new RegisterRecurringJobsReq ( ) ;
137+ req . Jobs = jobReqs ;
133138
134- _mediator . Value
135- . SendAsync < RegisterRecurringJobsCommand , RegisterRecurringJobsReq > ( req )
136- . GetAwaiter ( )
137- . GetResult ( ) ;
139+ _mediator . Value
140+ . SendAsync < RegisterRecurringJobsCommand , RegisterRecurringJobsReq > ( req )
141+ . GetAwaiter ( )
142+ . GetResult ( ) ;
143+ }
138144 }
139145 }
140146 }
0 commit comments