abpframework/abp

background email job activate instance failed for IEmailSender dependancy missing

Closed this issue · 12 comments

Dear @maliming @hikalkan :

i've referrenced the volo.abp.mailkit and some other necessary modules , however the email sending job always run failed. below is the error codes.
i've tried to run any other background job and got the expected work; so i can judge that the problem is the sytem couldnot find any IEmailSender instance registered . how can i fixed it.

An exception was thrown while activating  xxxxHelper.Web.EmailSendingJob.
Autofac.Core.DependencyResolutionException: An exception was thrown while activating xxxxHelper.Web.EmailSendingJob.
 ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'xxxxHelper.Web.EmailSendingJob' can be invoked with the available services and parameters:
Cannot resolve parameter 'Volo.Abp.Emailing.IEmailSender emailSender' of constructor 'Void .ctor(Volo.Abp.Emailing.IEmailSender)'.

the backgroundjob definition code is :

public class EmailSendingJob : BackgroundJob<EmailSendingArgs>, ITransientDependency
    {
        private readonly IEmailSender _emailSender;

        public EmailSendingJob(IEmailSender emailSender)
        {
            _emailSender = emailSender;
        }
        //  work well with the background job without IEmailSender injection
        //        public EmailSendingJob()
        //        {
        //            
        //        }
        public override void Execute(EmailSendingArgs args)
        {
            //            Logger.Log<EmailSendingArgs>(LogLevel.Information,new EventId(1001)
            //            ,args,null,null);
            //            _emailSender.SendAsync(
            //                args.EmailAddress,
            //                args.Subject,
            //                args.Body
            //            ).Wait();
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("I'm  triggered without IEmailSender");
            Console.ForegroundColor = ConsoleColor.White;

        }
    }
[DependsOn(typeof(AbpEmailingModule))]
public class AbpMailKitModule : AbpModule
{

}

i found the source code of AbpMailKitModule is as simle as above, is there something wrong?

acjh commented

Did you [DependsOn(typeof(AbpMailKitModule))]?

ofcourse, i did that @acjh ; the issue is failed to get the the iemailsender injection .
the autofac dependency couldn't find any relevant instances

please see the module code

[DependsOn(
        typeof(AbpAutofacModule),
        typeof(XXXXApplicationModule),
        typeof(AbpMailKitModule),
        typeof(AbpBackgroundJobsModule),
        typeof(AbpSettingManagementDomainModule),
        typeof(AbpSettingManagementEntityFrameworkCoreModule)
    )]
    public class BackgroundJobTaskModule:AbpModule
    {


        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpBackgroundJobWorkerOptions>(options =>
            {
                options.DefaultTimeout = 86400; //1 days (as seconds)
                //Configure for fast running
                options.JobPollPeriod = 1000;
                options.DefaultFirstWaitDuration = 1;
                options.DefaultWaitFactor = 1;
            });
            Configure<AbpBackgroundJobOptions>(options =>
            {
                options.IsJobExecutionEnabled = true; //set to false can Disable job execution
//                options.AddJob<EmailSendingJob>();
//                options.AddJob<BackgroundEmailSendingJob>();
            });
 
             
        }

        public override void OnPostApplicationInitialization(ApplicationInitializationContext context)
        {
           var email= context.ServiceProvider
                .GetRequiredService<IEmailSender>();
        }
    }
acjh commented

I could not reproduce that.

In your OnPostApplicationInitialization method, is email set?

@acjh the email is null . it is frustrated.

acjh commented

Can you create a repro project?

ok, i'll do it . i'll notice you ASAP. thanks

dear @acjh : it's sorry so late to upload the project.
the related project is here:
https://github.com/johnnba/simpleAbpNextMysqlDemo.
sincere thanks

Please move the dependent modules and configuration code in BackgroundJobTaskModule to MysqlDemoWebModule.

You have defined a BackgroundJobTaskModule module but it is not used. Please check the documentation for more framework information. https://docs.abp.io/en/abp/latest/

https://github.com/johnnba/simpleAbpNextMysqlDemo/blob/master/src/MysqlDemo.Web/BackgroundJobTaskModule.cs#L24

@maliming but how to explain that without IEmailSender injection ,the background job worked well?
i think that the BackgroundJobTaskModule has been registered by the abp framework?

i think that the BackgroundJobTaskModule has been registered by the abp framework?

No.

but how to explain that without IEmailSender injection ,the background job worked well?

Because your DomainModule depends on it.
MyProjectNameDomainModule=>AbpBackgroundJobsDomainModule=>AbpBackgroundJobsModule

sincere thanks! @maliming
now i get a deeper understanding!
I should take some time to learn more about the source code of abp framework.