November Happy Hour will be moved to Thursday December 5th.

Need help with calling a controller from a Scheduled Task

Vote:
 

I need to be able to call a controller method from an EPiServer scheduled job and send email from the controller . The controller method work perfect if called normally and send the email, but not if called from the scheduled job.

In Startup I have this but it did not work: services.AddScoped<BaseScheduledTask, BaseScheduledTask>();

Code removed
#318657
Edited, Mar 08, 2024 16:06
Vote:
 

Would it not be better to refactor out the logic into its own service and then both the controller and scheduled job call the service ? 

#318659
Mar 08, 2024 17:02
Eric - Mar 08, 2024 17:08
Probably so, just need some help or a template that would not have HttpContext not be null. I'm new to DI and services but willing to learn.
Johan Petersson - Mar 08, 2024 19:31
There is no http context in a scheduled job. So you need to rethink this approach.
Eric - Mar 08, 2024 19:37
That my whole problem. We need to be able send email from a job and when a job calls into a controller. Before going to version 12 asp core, we created a dummy controller, and it worked fine. Now with core, we need a to inject httpContext.
Johan Petersson - Mar 08, 2024 19:39
But why do you need a controller to send emails?
Eric - Mar 08, 2024 19:41
We don't necessarily. But we still can't send email from the Execute method in the job. Having the code in the controller allows us to execute the same code on demand without relying on episerver.
Johan Petersson - Mar 08, 2024 19:44
Why can't you send emails from the execute method? You're not giving us the full picture here. Would you mind sharing a bit more code from the method that sends emails?
Eric - Mar 08, 2024 19:47
We can't inject httpContext into the epi job required for our email. I've tried several ways but I'm new to DI. My example code show what I tried to do.
Johan Petersson - Mar 08, 2024 19:51
Yes, that we've already established. But what we're trying to get to, why even call a controller? Move the functionality to a separate service/class, so you can call it both from the job and the controller. A controller's main purpose is to be the glue between a view and the model, here we're not dealing with any of those.
Vote:
 

This is our mailer.

Code Removed
#318668
Edited, Mar 08, 2024 19:51
Johan Petersson - Mar 08, 2024 19:54
You're not even using the http context. So why not just remove that dependency from the ExamplesMailer class?

There's no controller here either. Why not call the ExamplesMailer class directly from the scheduled job?
Eric - Mar 08, 2024 20:05
Code Removed
Vote:
 

I got this to work...kind of. I now have what I think is a good reference to MailMessaging and ExampleMailer and this does run when calling a controller and when calling the mailer inside the Execute method. I hade to add this part to inject.

public class TestEpiJob(EmailMessaging emailMessaging, ExamplesMailer examplesMailer) : 
    BaseScheduledTask(emailMessaging, examplesMailer)

It however does not auctuall send an email so I need to look into this. I can get it to send emails using the sameExampleMailer outside of epi.

Code removed
#318671
Edited, Mar 08, 2024 20:38
Vote:
 

Hi Eric,

It sounds like your trying to use the Razor view as a template to sending the email, correct me if you think I am wrong here.

If this is the case then there is a good blog post on how to do this in pure MVC on Rick Strahl's site https://weblog.west-wind.com/posts/2022/Jun/21/Back-to-Basics-Rendering-Razor-Views-to-String-in-ASPNET-Core.

If I get a chance to over the weekend I will attempt to see if i can get a working solution of this blog post up and running.

There is also an example here that uses T4 templates to send an email replacing parameters https://mahdi-karimipour.medium.com/message-templates-with-t4-and-c-partial-classes-946b0ccda6fe 

Again i will try and get something running so you can see how it should work in both cases.

Paul

#318672
Mar 08, 2024 21:50
Eric - Mar 08, 2024 21:58
Yes - I'm using a razor view to send the email. I got the email part actually sending now. Thx for the links. I'm going to look them over and make sure I'm doing everything correctly. The key part I was missing was injecting the objects into me base class and then propagating it down to the job itself and updating the service to AddScope in StartUp for the base class.
Paul McGann (Netcel) - Mar 08, 2024 23:08
Glad you are getting somewhere with it now.

I found library that might make all this even easier and removes the need for the httpContext.

https://github.com/lukencode/FluentEmail
Vote:
 

Hi Eric,

I have pushed up some code that will help you with sending emails on github here https://github.com/paulmcgann/playground.

I created an email interface and then implemented that interface with an email service you can see this in the following folder:

https://github.com/paulmcgann/playground/tree/main/src/playground/Features/Email

Then in the startup.cs I registered some services one was for the FluentEmail library and the other is for the email interface and email service you can see that here:

https://github.com/paulmcgann/playground/blob/main/src/playground/Startup.cs

You will need to enter you own details for the FluentEmail to make this work you will need a host, username and password.

Finally I injected the email service into the StartPageController:

https://github.com/paulmcgann/playground/blob/main/src/playground/Controllers/StartPageController.cs

And a scheduled job:

https://github.com/paulmcgann/playground/blob/main/src/playground/Features/ScheduledJobs/EmailerScheduledJob.cs

This way you can inject your email service across various parts of your code and only write the code for it once and re-use and your system.

Hopefully this will get you going in the right direction.

If you need more help let us know and we can see where we can help you.

Paul

#318710
Mar 09, 2024 15:31
Eric - Apr 12, 2024 16:01
Is the the FluentEmail you used? https://github.com/lukencode/FluentEmail
Vote:
 

Thanks Paul. I'll get the code and have a look.

#318785
Mar 11, 2024 19:35
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.