Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Accessing an application service from a console app - webjob #11306


User avatar
0
michael.pear created
  • What is your product version? 11.2.1
  • What is your product type (Angular or MVC)? Angular, but doesn't matter for this question.
  • What is product framework type (.net framework or .net core)? .net core

I'm creating an Azure Webjob to handle overnight updates in a tenant database which I manage with a separate database context from the AspNetZero default/admin database. I've got the basic shell running using the .Migrator project console app as an example.

My goal is to use an Application Service with the update actions to encapsulate all of the database operations. I am calling the application service methods from my console app, and am running into the requirement that an AbpSession needs to be defined.

  • Is there an example of calling an application service directly from a console app showing how to setup a session?
  • Is there a way to authenticate user credentials from a console app to establish a session, without going through the web server?

1 Answer(s)
  • User Avatar
    0
    michael.pear created

    Regarding setting up a session, this turned out to be easy to do with the Use method in IAbpSession:

            static async Task Main()
            {
                Console.WriteLine("Hello,  World!");
                using (var bootstrapper = AbpBootstrapper.Create<WebJobsModule>())
                {
                    bootstrapper.Initialize();
    
                    using (var appService = bootstrapper.IocManager.ResolveAsDisposable<ITraineeAppService>())
                    {
                        ((Abp.Application.Services.ApplicationService) appService.Object).AbpSession.Use(null, 1);
                        var traineesAll = await appService.Object.GetAll();
                        Console.WriteLine("Traineees All Count {0}", traineesAll.Items.Count());
                    }
                    Console.WriteLine("Press ENTER to exit...");
                    Console.ReadLine();
                }           
            }