Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send IServiceProvider to DoWork method of the periodic background worker #2539

Closed
hikalkan opened this issue Jan 3, 2020 · 3 comments · Fixed by #2545
Closed

Send IServiceProvider to DoWork method of the periodic background worker #2539

hikalkan opened this issue Jan 3, 2020 · 3 comments · Fixed by #2545

Comments

@hikalkan
Copy link
Member

hikalkan commented Jan 3, 2020

  • Added IServiceScopeFactory to the PeriodicBackgroundWorkerBase constructor.
  • Added PeriodicBackgroundWorkerContext to the DoWork method.
@hikalkan hikalkan added this to the 2.0 milestone Jan 3, 2020
@hikalkan hikalkan self-assigned this Jan 3, 2020
hikalkan added a commit that referenced this issue Jan 3, 2020
* Send IServiceProvider to DoWork method of the periodic background worker #2539
* Introduce AsyncPeriodicBackgroundWorkerBase #2540
@dicksonkimeu
Copy link

@hikalkan

Any sample code on how to use this, before 2.o my code was working, but now it's not.

Added IServiceScopeFactory to the PeriodicBackgroundWorkerBase constructor.
Added PeriodicBackgroundWorkerContext to the DoWork method.

@dicksonkimeu
Copy link

@hikalkan How should i use the workerContext and the scope factory ?

Below is my code

public class BankingSwitchSync : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
        private readonly IBackgroundJobManager _backgroundJobManager;
        private readonly ICurrentUser _currentUser;
        private readonly ICurrentTenant _currentTenant;
        public BankingSwitchSync(AbpTimer timer, IServiceScopeFactory serviceScopeFactory, IBackgroundJobManager backgroundJobManager,
             ICurrentTenant currentTenant, ICurrentUser currentUser
            )
            : base(timer,serviceScopeFactory)
        {
            _backgroundJobManager = backgroundJobManager;
            _currentTenant = currentTenant;
            _currentUser = currentUser;
            timer.Period = 6000;
        }
        protected override void DoWork(PeriodicBackgroundWorkerContext workerContext)
        {
            this.Logger.LogDebug("Banking Switch Sync => Do Work..");
            string user = "";
            string tenant = "";
            user = _currentUser.UserName;
            tenant = _currentTenant.Name;
            string settings1 = "";
        }
    }

@acjh
Copy link
Contributor

acjh commented Jan 19, 2020

@dicksonkimeu Sample code on how to use workerContext:

var store = workerContext.ServiceProvider.GetRequiredService<IBackgroundJobStore>();

You should not need to use IServiceScopeFactory directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants