3

I have a TaskScheduler with the function EmailInactiveUsers that I start from Application_Start().

I need a link to the root of my application in my email that I send out. But how do I get the base url of my application without having access to an HttpContext?

My backup solution would be to add this url to the web.config, but it would be nice if I could do this dynamically since we are deploying this application to a lot of different places.

Application_Start

protected void Application_Start()
{
    ...

    TaskScheduler.EmailInactiveUsers();
}

TaskScheduler

public class TaskScheduler
{
    ...

    public static void EmailInactiveUsers()
    {
        IJobDetail job = JobBuilder.Create<InactiveUsersCheck>().Build();

        ITrigger trigger = TriggerBuilder.Create()
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInHours(24)
                    .RepeatForever())
                .Build();

        scheduler.ScheduleJob(job, trigger);
    }
}

InactiveUsersCheck

public class InactiveUsersCheck : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        ...

        // Get base url here but there's no Request or HttpContext.Current available
        var Url = Request.Url.GetLeftPart(UriPartial.Authority);
    }
}
Niklas
  • 13,005
  • 23
  • 79
  • 119
  • Did you ever find a solution to this, I am having the exact same issue? – Rob Jun 21 '17 at 11:34
  • Good question, I'm afraid I'm not involved in this project anymore. If I get the possibility to look at it again I'll share the solution or the workaround. – Niklas Jun 21 '17 at 12:36

0 Answers0