Base solution for your next web application
Open Closed

Run some code in Core on StartUp #10207


User avatar
0
MarkEaston created

We are using ASP.NET Zero 10.2. ASP.NET Core 5 and Angular 11.

I would like to be able run a Domain Service method only when the app is started.This method needs a unit of work so that I can work with my repositories. Is this possible?

I was trying to call it from <projectname>CoreModules.cs

ie like this

public override void PostInitialize() { IocManager.RegisterIfNot<IChatCommunicator, NullChatCommunicator>(); IocManager.Resolve<ChatUserStateWatcher>().Initialize(); IocManager.Resolve<AppTimes>().StartupTime = Clock.Now; IocManager.Resolve<IMyDomainService>().Run(); }

But the UnitOfWork attribute I used for the Run() method would not work. ie

[UnitOfWork] public void Run() { DoSomething(); }

I kept getting the following exception:

Abp.AbpException: Unknown filter name: MayHaveTenant. Be sure this filter is registered before. at Abp.Domain.Uow.UnitOfWorkBase.GetFilterIndex(String filterName) at Abp.Domain.Uow.UnitOfWorkBase.SetFilterParameter(String filterName, String parameterName, Object value) at Abp.Domain.Uow.UnitOfWorkBase.SetTenantId(Nullable1 tenantId, Boolean switchMustHaveTenantEnableDisable) at Abp.Domain.Uow.UnitOfWorkBase.Begin(UnitOfWorkOptions options) at Abp.Domain.Uow.UnitOfWorkManager.Begin(UnitOfWorkOptions options) at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.BackgroundJobStoreProxy.GetWaitingJobs(Int32 maxResultCount) at Abp.BackgroundJobs.BackgroundJobManager.DoWork() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Threading.BackgroundWorkers.PeriodicBackgroundWorkerBase.Timer_Elapsed(Object sender, EventArgs e)

Many thanks for you help.


4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Could you resolve IUnitOfWorkManager and start a unit of work manually ? See https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#iunitofworkmanager.

    Thanks,

  • User Avatar
    0
    MarkEaston created

    No, I tried that first and could not get it to work. Same problem as with the attribute (which I tried next).

  • User Avatar
    0
    MarkEaston created

    Could I do this as a background job on Startup? Would it then be able to use EF?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @MarkEaston

    Yes, you can start a background job on startup and it can access the database.