Get base url from a scheduled task in application start

458 Views Asked by At

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);
    }
}
0

There are 0 best solutions below