0

Is there any way to determine the host name from within a Sitecore Scheduled Task?

I found that within an ASP.NET MVC page for Sitecore 9.3, I can peer into the current HttpContext via Sitecore.Context.HttpContext.

However, it does not seem possible from within the implementation of a Scheduled Task -- Sitecore.Context.HttpContext just returns null.

2
  • I don't believe that is possible. What will the hostname allow you to do that a config setting can't accomplish? Commented Aug 27, 2023 at 2:46
  • @MichaelWest I will have to use environment-specific config settings if I can't determine the host name at runtime.
    – Daniel R
    Commented Aug 27, 2023 at 23:11

2 Answers 2

1

tl/dr is that you can't.

Scheduled tasks don't run within a request context, they run within the context of the shell website defined in the siteDefinition section of the config.

2
  • Understood. But even the shell website should have an HttpContext, right?
    – Daniel R
    Commented Aug 27, 2023 at 23:09
  • No. It runs in a background process. There is no request that kicks it off.
    – Richard Seal
    Commented Aug 28, 2023 at 13:17
-1

You can get the SiteContext for your site and then retrieve the hostName in Sitecore Scheduled Task Execute Method.

Site configuration

<sites>
  ...
  <site name="mysite" hostName="mysite.com|www.mysite.com" scheme="http" ... .../>
  ...
</sites>

Get hostname using this code.

var sitecoreContext  = Sitecore.Configuration.Factory.GetSite("mysite");
string hostname = sitecoreContext.HostName;

Hope it helps!

2
  • Thanks Sumit -- but that will only give the patterns that we prescribed in the <site> definition. I need the actual runtime host name.
    – Daniel R
    Commented Aug 27, 2023 at 23:22
  • You can't get the actual runtime hostname in schedule task. You have to get it like this or get it from config setting. Commented Aug 28, 2023 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.