Getting HttpContext from WorkContext in Time Delayed Workflow - One {Ugly} Solution, Any Better

Topics: Core, Customizing Orchard
Sep 21, 2013 at 12:36 AM
I am making the Comments module better (at least I think so) and have completed all I set out to (as defined here:

I will release my code shortly but want to perform some "magic" and be able to time delay the Comment Alert Workflow using the "Timer". Unfortunately, as you might expect, using the timer completely removes the workContext.HttpContext so when I include the Approve Comment Url or the Delete Comment Url (the ones with the Nonce for approval via url) they are blank.

The code for generating these URL's comes from Orchard.Comments.Tokens.CommentTokens CreateProtectedUrl. Since there is no HttpContext, the url can't be set. I have stumbled upon a work around to recreate HttpContext based on information on StackOverflow though it feels like a hack AND is dependent on knowing the base url (which I get from WorkContext.CurrentSite.BaseUrl). Unfortunately, this means it breaks when testing in localhost or when the BaseUrl isn't set right in settings. It is still better than nothing and works most of the time. Here is the code snippet:
else if (!String.IsNullOrEmpty(workContext.CurrentSite.BaseUrl)) {
    string baseUrl = workContext.CurrentSite.BaseUrl;
    var request = new HttpRequest("/", baseUrl, "");
    var response = new HttpResponse(new StringWriter());
    var httpContext = new HttpContext(request, response);

    var httpContextBase = new HttpContextWrapper(httpContext);
    var routeData = new RouteData();
    var requestContext = new RequestContext(httpContextBase, routeData);

    var url = new UrlHelper(requestContext);
    var urlPath = url.Action(action, "Comment", new { area = "Orchard.Comments", nonce = _commentService.CreateNonce(part, TimeSpan.FromDays(7)) });
    var urlAbsolute = String.Format("{0}{1}", baseUrl, urlPath);
    return urlAbsolute;
The question then is, is there any way to consistently regain the HttpContext for background processes in Orchard?
Sep 21, 2013 at 7:02 PM
This issue with using UrlHelper is solved in 1.7.1. UrlHelper is now an injectable dependency, using a HttpContext created using the base URL set up in the general site settings. So this should just work now.
Sep 21, 2013 at 7:53 PM
Howdie Piedone,

That is crazy and, like magic, works. Thanks for sharing. Are there other injectables that have been added that I can learn about? Interesting how this resolves as it is from System.Web.Mvc namespace, injectables always impress me.

I will post all my code changes for "Comments" improvements here:

Thanks again.
Sep 21, 2013 at 8:01 PM
I think this is the only such news for 1.7.1 :-). Apart from the many new Azure services of course, but no other such exotics as far as I remember.
Sep 21, 2013 at 8:14 PM
If you don't mind me hijacking my own thread, I am running on Azure Websites and am not sure I fully understand the new Azure Features. Here is what it seems like:

If I am using Azure Cache service, then the "Windows Azure Database Cache" and "Windows Azure Output Cache" would become valuable. I don't yet use Azure Cache services so have determined I should not run these modules, is my understanding correct? Would love to hear if anyone uses Azure Cache and has experienced a nice boost.

If I am using Azure Blob Storage (or want to) then the "Windows Azure Media Storage" would become valuable to me. Since I don't yet utilize Azure Blob Storage I have not run this module (though it is interesting to me if Azure Blob storage acts like a CDN and serves content quickly). Same here, would love to hear if anyone uses Azure Blob Storage compared to the Azure Websites "regular" storage.

I would love to understand these three new features. Personally, Windows Azure Websites is working out fantastic and if these make it even greater, amazing.