eXpandFramework / eXpand

DevExpress XAF (eXpressApp) extension framework. 𝗹𝗶𝗻𝗸𝗲𝗱𝗶𝗻.𝗲𝘅𝗽𝗮𝗻𝗱𝗳𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸.𝗰𝗼𝗺, 𝘆𝗼𝘂𝘁𝘂𝗯𝗲.𝗲𝘅𝗽𝗮𝗻𝗱𝗳𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸.𝗰𝗼𝗺 and 𝘁𝘄𝗶𝘁𝘁𝗲𝗿 @𝗲𝘅𝗽𝗮𝗻𝗱𝗳𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸 and or simply 𝗦𝘁𝗮𝗿/𝘄𝗮𝘁𝗰𝗵 this repository and get notified from 𝗚𝗶𝘁𝗛𝘂𝗯
http://expand.expandframework.com
Microsoft Public License
220 stars 114 forks source link

JobScheduler.Hangfire Dependency Injection on the JobProvider does not work #974

Closed ss-carlo closed 1 year ago

ss-carlo commented 1 year ago

I tried to copy the JobProvider class in the sample video but only the empty constructor is being used when the job is triggered from my blazor application. Are there additional steps to do to make dependency injection work?

apobekiaris commented 1 year ago

did you try the docs? what the problem with them?

https://github.com/eXpandFramework/Reactive.XAF/tree/master/src/Modules/JobScheduler.Hangfire

there are also tests that pass

https://github.com/eXpandFramework/Reactive.XAF/blob/e7ac9411c7f91404b31efd13b674fa3cc5e560e4/src/Tests/JobScheduler.Hangfire/TestJob.cs#L14-L20

ss-carlo commented 1 year ago

The docs just specified that I should use dependency injection to get object instances. The problem is that the constructor with the needed dependency is not being invoked when I trigger the hangfire job from the Blazor application.

apobekiaris commented 1 year ago

is it decorated? did you try running the tests and not work as well there? do u have a sample

ss-carlo commented 1 year ago

The code is just as basic as the sample in the docs. Here is the code for the JobProvider class. I have also attached a sample project containing this source.

`using DevExpress.ExpressApp.Blazor; using Xpand.XAF.Modules.JobScheduler.Hangfire;

namespace HfSample.Blazor.Server.Hangfire { [JobProvider] public class SampleJob { public BlazorApplication Application;

    public SampleJob() 
    {
        Console.WriteLine("Default constructor");
    }

    public SampleJob(BlazorApplication application) 
    {
        Application = application;
    }

    public void PerformOperation1()
    {
        Console.WriteLine(string.Format("PerformOperation1 app={0}", Application != null));
    }

    public void PerformOperation2()
    {
        Console.WriteLine(string.Format("PerformOperation2 app={0}", Application != null));
    }
}

}`

SampleProject

apobekiaris commented 1 year ago

is it decorated

the link i send above uses the ActivatorUtilitiesConstructor attribute in the ctor, can u try it

apobekiaris commented 1 year ago

the docs look outdate passing the BlazorAPp in the ctor is not supported anymore here is a test job from the link i posted above

https://github.com/eXpandFramework/Reactive.XAF/blob/e7ac9411c7f91404b31efd13b674fa3cc5e560e4/src/Tests/JobScheduler.Hangfire/TestJob.cs#L42-L69

please try this and let me know

    public class TestJob {
        public static readonly Subject<TestJob> Jobs=new();

        public PerformContext Context { get; protected set; }

        public TestJob() { }
        public IServiceProvider Provider { get; }

        [ActivatorUtilitiesConstructor]
        protected TestJob(IServiceProvider provider) => Provider = provider;

        [AutomaticRetry(Attempts = 0)][JobProvider]
        public void FailMethodNoRetry() => throw new NotImplementedException();

        [AutomaticRetry(Attempts = 1,DelaysInSeconds = new[] {1})][JobProvider]
        public void FailMethodRetry() => throw new NotImplementedException();

        [JobProvider]
        public void Test() => Jobs.OnNext(this);

        [JobProvider]
        public void TestJobId(PerformContext context) {
            Context = context;

            Jobs.OnNext(this);
        }
    }
}
apobekiaris commented 1 year ago

similarly from the TestApplication u can try this https://github.com/eXpandFramework/Reactive.XAF/blob/master/src/Tests/EasyTests/TestApplication/TestApplication.Module.Blazor/JobScheduler/Job.cs

ss-carlo commented 1 year ago

Thanks, it worked with the ActivatorUtilitiesConstructor attribute!

apobekiaris commented 1 year ago

this is a DI attribute and not expand and I suggest you consult with Hangfire docs as it operates independent of XAF or eXpand as you can see there are more attributes u can use e.g. for Retry etc