Render shape in IScheduleTaskHandler

Topics: Customizing Orchard
Nov 24, 2014 at 5:24 PM
I had been using an older version of orchard ... it may have been Commit: 7b72acfd36d2f47ae8a7c55db9b9a48fcb9f952d [7b72acf] of the 1.x branch from April 23, 2014.

I was working on a scheduled task handler to send emails. In one version, I used text from the database for the body of the email. I ran into trouble with the use of tokens, specifically the DisplayUrl token, which was not getting replaced properly, presumably due to the absence of the full runtime context of the background thread. A google search suggested that this had been fixed, so I pulled the latest node from the 1.x branch yesterday (Nov 23rd) and near as I can tell, the DisplayUrl token replacement works for scheduled task handlers.

But, the other type of email body that I was generating in my scheduled task handler was from a shape. I inject an IShapeFactory and an IShapeDisplay object into my service class. To create the shape I call
        var template = _shapeFactory.Create(shapeName, Arguments.From(new {
            Arg1 = localVar1
Then I render the shape using
        string body = _shapeDisplay.Display(template);

This had been working in the older version. Now, the call to _shapeDisplay.Display(template) throws this exception:

System.InvalidOperationException occurred
Message=The RouteData must contain an item named 'controller' with a non-empty string value.
   at System.Web.Routing.RouteData.GetRequiredString(String valueName)
   at System.Web.Mvc.VirtualPathProviderViewEngine.FindPartialView(ControllerContext controllerContext, String partialViewName, Boolean useCache)
   at Orchard.Mvc.ViewEngines.ThemeAwareness.ViewEngineCollectionWrapper.FindPartialView(ControllerContext controllerContext, String partialViewName, Boolean useCache) in c:\User\devel\orchard\src\Orchard\Mvc\ViewEngines\ThemeAwareness\ViewEngineCollectionWrapper.cs:line 17

The exception is from the following line:
            var result = engine.FindPartialView(controllerContext, partialViewName, useCache);

which sits in the following method of the class ViewEngineCollectionWrapper
    public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) {
        var searchedLocations = Enumerable.Empty<string>();
        foreach (var engine in _engines) {
            var result = engine.FindPartialView(controllerContext, partialViewName, useCache);
            if (result.View != null)
                return result;
            if (!useCache)
                searchedLocations = searchedLocations.Concat(result.SearchedLocations);
        return new ViewEngineResult(searchedLocations.Distinct());

If anyone can give me some guidance, I would appreciate it. Is rendering shapes during the processing of a scheduled task handler no longer possible? Perhaps it is still possible, and I am not creating/displaying the shape properly. Any help would be appreciated.
Nov 24, 2014 at 7:41 PM
I have just confirmed that rendering a shape like this during a scheduled task handler does work in the latest commit of the 1.8.x branch (which is probably where I should be anyway).