I'm working with DotNetNuke's scheduler to schedule tasks and I'm looking to get the physical file path of a email template that I created. The problem is that HttpContext is NULL because the scheduled task is on a different thread and there is not http request. How would you go about getting the file's physical path?
5 Answers. Show activity on this post. Clearly HttpContext. Current is not null only if you access it in a thread that handles incoming requests.
It is stored in the memory of the server and the value is available for the entire lifetime of the request. Show activity on this post. I think the HTTPContext is stored on the server it comes from the HTTP request.
From ASP.NET 4.5 and after, Current HttpContext is passed through CallContext instead of [ThreadStatic] , so context remains available through out async calls in single logical context instead of current thread as each async call may end up on different threads.
The property stores the HttpContext instance that applies to the current request. The properties of this instance are the non-static properties of the HttpContext class. You can also use the Page. Context property to access the HttpContext object for the current HTTP request.
System.Web.Hosting.HostingEnvironment.MapPath is what you're looking for. Whenever you're using the Server or HttpContext.Current objects, check first to see if HostingEnvironment has what you need.
There are many ways of doing this, I personally get around it by storing path information as a config option for my modules, it isn't elegant, but it works and works every time.
Joe Brinkman I belive somewhere around has a blog posting on how to construct a new HTTPContext for use inside the scheduler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With