abpframework / abp

Open Source Web Application Framework for ASP.NET Core. Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET and the ASP.NET Core platforms. Provides the fundamental infrastructure, production-ready startup templates, application modules, UI themes, tooling, guides and documentation.
https://abp.io
GNU Lesser General Public License v3.0
12.31k stars 3.32k forks source link

IBackgroundWorkerManager #2518

Closed dicksonkimeu closed 4 years ago

dicksonkimeu commented 4 years ago

This is how we used to call background service classes.

image in vNext the IocManager is un recognized. what am i missing here ? image

dicksonkimeu commented 4 years ago

This is the class i want to call.

I was expecting something like this. but in vNext the IocManager is un recognized.

var workManager = IocManager.Resolve<IBackgroundWorkerManager>();

workManager.Add(IocManager.Resolve<BankingSwitchSync>());

image

maliming commented 4 years ago

You should check vnext's documentation. https://docs.abp.io/en/abp/latest/Dependency-Injection https://docs.abp.io/en/abp/latest

dicksonkimeu commented 4 years ago

Have gone through the docs extensively, jobs are working (normal jobs) but can't see how to initialize my periodic background worker. Have been using something like this in the old framework

var workManager = IocManager.Resolve<IBackgroundWorkerManager>();
workManager.Add(IocManager.Resolve<BankingSwitchSync>());
wocar commented 4 years ago

context.ServiceProvider.GetRequiredService().Initialize()

dicksonkimeu commented 4 years ago

Hello William, Where should I place this code ?

wocar commented 4 years ago

On the initialize of the module

You can override the OnApplicationInitializing

El El mar, 31 de diciembre de 2019 a la(s) 8:29 p.m., dicksonkimeu < notifications@github.com> escribió:

Hello William, Where should I place this code ?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/abpframework/abp/issues/2518?email_source=notifications&email_token=ACGJYYIW45AK76ED6CBBQJ3Q3P5W7A5CNFSM4KBQHRLKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH4334Q#issuecomment-570015218, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACGJYYLK5SZXG4VWMXKEYGLQ3P5W7ANCNFSM4KBQHRLA .

--

Ing. William Obando Castellanos T: 351-123-7707

dicksonkimeu commented 4 years ago

I don't seem to get this, would you help me with a sample code.

image

wocar commented 4 years ago

This is how I do it

I would suggest using resharper to find the right extension method, if this doesn’t work try

.GetService(typeof(IBackgroundService))

El El mié, 1 de enero de 2020 a la(s) 2:23 a.m., dicksonkimeu < notifications@github.com> escribió:

I don't seem to get this, would you help me with a sample code.

[image: image] https://user-images.githubusercontent.com/4476822/71639512-0852b080-2c89-11ea-891a-d67dd27c7a05.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/abpframework/abp/issues/2518?email_source=notifications&email_token=ACGJYYNAVLPMGCKWVEQWHPDQ3RHHFA5CNFSM4KBQHRLKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH5AKXA#issuecomment-570033500, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACGJYYLHSJ2NVLQMHOLRQVLQ3RHHFANCNFSM4KBQHRLA .

--

Ing. William Obando Castellanos T: 351-123-7707

dicksonkimeu commented 4 years ago

Have added this

            context.ServiceProvider.GetService(typeof(IBackgroundWorkerManager));
            context.ServiceProvider.GetRequiredService(typeof(BankingSwitchSync));

I was not able to add //context.ServiceProvider.GetRequiredService().Initialize();

When i ran the app i get error; Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'eBanking.VISA.Integration.BankingSwitchSync' 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.

maliming commented 4 years ago

hi @dicksonkimeu Try

public class Class : BackgroundWorkerBase
{
    public  override Task StartAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        this.Logger.LogDebug("Started background worker: " + this.ToString());
        return Task.CompletedTask;
    }

    public  override Task StopAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        this.Logger.LogDebug("Stopped background worker: " + this.ToString());
        return Task.CompletedTask;
    }
}

public class Class2 : PeriodicBackgroundWorkerBase
{
    public Class2(AbpTimer timer) : base(timer)
    {
        timer.Period = 1000; //1s
    }

    protected override void DoWork()
    {
        this.Logger.LogDebug("Class2 => DoWork..");
    }
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
    var app = context.GetApplicationBuilder();
    var env = context.GetEnvironment();

    context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>().Add(context.ServiceProvider.GetRequiredService<Class>());
    context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>().Add(context.ServiceProvider.GetRequiredService<Class2>());

    //....
}
dicksonkimeu commented 4 years ago

2020-01-02 11:40:50.485 +03:00 [FTL] Host terminated unexpectedly! Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'eBanking.VISA.Integration.Class' 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.

maliming commented 4 years ago

Please share the relevant code.

dicksonkimeu commented 4 years ago

image image

dicksonkimeu commented 4 years ago

Have tried in OnApplicationInitialization and the results are the same.

      context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>().Add(context.ServiceProvider.GetRequiredService<BankingSwitchSync>());
                context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>().Add(context.ServiceProvider.GetRequiredService<Class>());
maliming commented 4 years ago

Please share a project to reproduce your problem, you can use cli to create a project.

wocar commented 4 years ago

Maybe add ISingletonDependency to Class and change OnApplicationInitialization to OnPostApplicationInitialization

El El jue, 2 de enero de 2020 a la(s) 2:44 a.m., maliming < notifications@github.com> escribió:

Please share a project to reproduce your problem, you can use cli to create a project.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/abpframework/abp/issues/2518?email_source=notifications&email_token=ACGJYYMFJD7UL6WPEOGYXW3Q3WSNLA5CNFSM4KBQHRLKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH532QI#issuecomment-570146113, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACGJYYJH3W7PQJQU6PAUQA3Q3WSNLANCNFSM4KBQHRLA .

--

Ing. William Obando Castellanos T: 351-123-7707

maliming commented 4 years ago

hi @wocar BackgroundWorkerBase=> IBackgroundWorker=> ISingletonDependency

dicksonkimeu commented 4 years ago

https://drive.google.com/open?id=1wwwSHUGHODjotIsM-qUF6X2NRsviz6uj

maliming commented 4 years ago

@dicksonkimeu You can share the project on github.

image

dicksonkimeu commented 4 years ago

Switch.zip

maliming commented 4 years ago

@dicksonkimeu Try adding an IntegrationModule. image

dicksonkimeu commented 4 years ago

@maliming @hikalkan this is working fine now, but i have this error when i run a service with DB operations in my Integration Module #2601

maliming commented 4 years ago

@dicksonkimeu Please discuss it in a new issue.