After a short jerk through the Orchard source and inspiration from the Andrew Davy email application, I managed to find a solution. See code snippet below.
private void RenderMessage(MessageContext context, dynamic shape) { var httpContext = new EmailHttpContext(new Uri("http://localhost/orchard/")); var routeData = new RouteData(); routeData.DataTokens.Add("IWorkContextAccessor", _workContextAccessor); routeData.Values["controller"] = "Dummy"; var requestContext = new RequestContext(httpContext, routeData); var controllerContext = new ControllerContext(requestContext, new DummyController()); var viewContext = new ViewContext(controllerContext, new ShapeView(shape), new ViewDataDictionary(shape.Model), new TempDataDictionary(), new StringWriter()); var scope = _workContextAccessor.CreateWorkContextScope(viewContext.HttpContext); scope.WorkContext.CurrentTheme = _siteThemeService.GetSiteTheme(); var page = new EmailWebViewPage(viewContext, new ViewDataDictionary<dynamic>(shape.Model)); var displayHelperFactory = _services.WorkContext.Resolve<IDisplayHelperFactory>(); var display = displayHelperFactory.CreateHelper(page.ViewContext, page); context.MailMessage.Body = display(shape).ToHtmlString(); scope.Dispose(); } class DummyController : Controller { } class ShapeView : IView { private readonly dynamic _shape; public ShapeView(dynamic shape) { _shape = shape; } #region IView Members public void Render(ViewContext viewContext, TextWriter writer) { } #endregion }
EmailHttpContext was taken from the mail project. This was used to create the controller and view contexts. Then I just had to extend the orchard WebViewPage to create a MailWebViewPage that allows me to use the garden infrastructure to access viewers, etc.
This is not a pleasant solution and needs to be thoroughly tested, but it seems to do what I follow.
If someone has some kind of feedback or you want to get the full code, feel free to drop me a line.
source share