We left Russia. We are against aggression and war with Ukraine. It is a tragedy for our nations, it is a nightmare

Use Hangfire for calling a method from another project


Can anyone tell me please if I can use Hangfire for calling a method from another project (not the project which has the code for Hangfire). The method is in a service which has just one constructor which has as parameters another services (which are in same project) ? Also I can use a container for this (DryIoc).

If is possible please show me a link with methodology or please describe here that methodology. I read the documentation about registering services with a container, about the configuration of Hangfire also, but I can not implement something which work without errors.

I put this code first in Startup file of an Asp.Net Core project, in ConfigureServices :

    Container container = new DryIoc.Container();

            // Setup your unity container, and registering the services from another project
            //first service has the method which must be called
            this.container.Register<IEmailsQueuesService, EmailsQueuesService>();

            //next servces are parameters for the constructor of the first service
            this.container.Register<IEmailSettingsService, EmailSettingsService>();
            this.container.Register<IEmailsContentService, EmailsContentService>();
            this.container.Register<IEmailsAttachmentsService, EmailsAttachmentsService>();
            this.container.Register<IArchiveDocumentService, ArchiveDocumentService>();
            this.container.Register<ISmtpEmailSender, SmtpEmailService>();

            services.AddHangfire(hangfireConfiguration =>
                                                            UseActivator(new ContainerJobActivator(this.container)).
                                                            UseSqlServerStorage(this.Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions
                                                                CommandBatchMaxTimeout = TimeSpan.FromMinutes(1),
                                                                SlidingInvisibilityTimeout = TimeSpan.FromMinutes(1),
                                                                QueuePollInterval = TimeSpan.Zero,
                                                                UseRecommendedIsolationLevel = true,
                                                                DisableGlobalLocks = true


Then I put this code in Configure :

        RecurringJob.AddOrUpdate<EmailsQueuesService>("easyjob", x => x.CopyFiles("Easy!"), Cron.Minutely);

After I put this code in my project did not work, I received errors. Can you help me, there is something wrong in code or in configuration ?

What is the error? When do you receive the error?

The Hangfire project must know of all code that pertains to jobs as it will be the one executing the code. If you want to separate them completely you would have to turn the other project into an API/command console. Then create a job that simply performs an http request (API) or executes the command exe.