UmbracoContext.Current not available in RenderTask
Hi all
I try to process the content of my newsletter using render task.
While the UmbracoContext.Current in the class RenderTask is available, within the function ProcessUniqueItem(RenderResult renderResult, RenderTaskUniqueItemParameters parameters) the UmbracoContext.Current is null.
Because I process each unique item and render it very personalized, I need GetPropertyValue therefore the current UmbracoContext is necessary.
Is there a workaround to get and set the current UmbracoContext for this functionality?
Just to make sure that you understand =D This is a hack - but it should work =D You need to create an event-handler that move over the references to the sending thread. This is how it might look. We will create a better implementation i the future but this is what I got at the moment.
public class AppStart : ApplicationEventHandler
{
public static UmbracoContext UmbracoContext;
public static ApplicationContext AppContext;
public static HttpContextBase CurrentHttpContextBase;
public static System.Uri HostAndProtocol;
protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationInitialized(umbracoApplication, applicationContext);
}
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
SendNewsletterService.SendningInitializing += (sender, args) =>
{
UmbracoContext = Umbraco.Web.UmbracoContext.Current;
HostAndProtocol = RenderTask.ProtocolAndHost;
AppContext = ApplicationContext.Current;
CurrentHttpContextBase = new HttpContextWrapper(HttpContext.Current);
UmbracoContext.EnsureContext(CurrentHttpContextBase, AppContext);
};
base.ApplicationStarted(umbracoApplication, applicationContext);
}
}
Thanks for your Codesnippet. I implemented it in my Code but unfortunately the UmbracoContext.Current is in the method ProcessUniqueItem still null. So GetPropertyValue for the objects below has no value as well.
public class NewsletterRender : RenderTask
{
private static UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
public override void ProcessUniqueItem(RenderResult renderResult, RenderTaskUniqueItemParameters parameters)
{
var coverText = String.Empty;
var coverLinks = String.Empty;
var positionModelStart = renderResult.MessageBody.IndexOf("#Model.");
if (positionModelStart > -1)
{
var positionModelEnd = renderResult.MessageBody.Substring(positionModelStart + 7);
var modelId = positionModelEnd.Substring(0, positionModelEnd.IndexOf("#"));
IPublishedContent model = helper.TypedContent(modelId);
if (model != null)
{
coverText = model.GetPropertyValue<string>("coverText", true);
var coverLinksUrls = model.GetPropertyValue<MultiUrls>("coverLinks");
if (coverLinksUrls != null && coverLinksUrls.Any())
{
coverLinks = "<td class='mcnTextContent linkBlock' style='border-left: 1px solid #c9c9c7; padding-left: 50px'>";
foreach (var link in coverLinksUrls)
{
coverLinks += "<p style='margin: 0'><a href='" + link.Url + "' title='" + link.Name + "' target='_blank' style='text-decoration: none; width: 100%;'>> " + link.Name + "</a></p>";
}
coverLinks += "</td>";
}
}
renderResult.MessageBody = renderResult.MessageBody.Replace("#Model." + modelId + "#", String.Empty);
}
renderResult.MessageBody = renderResult.MessageBody.Replace("#coverText#", coverText);
renderResult.MessageBody = renderResult.MessageBody.Replace("#coverLinks#", coverLinks);
}
}
When I start sending a Newsletter, it goes first of all to the method ProcessPreRender, then to the event SendNewsletterService.SendningInitializing += (sender, args) and then to the method ProcessUniqueItem()
That sounds correct - the ProcessUniqueItem-method is called in a new thread and that's why the context is null but the code in the SendningInitializing -method should reinstantiate the UmbracoContext.
Could we talk over PM so that we can share some code? Send me an e-mail: markus [at sign goes here] enkelmedia.se.
UmbracoContext.Current not available in RenderTask
Hi all
I try to process the content of my newsletter using render task.
While the UmbracoContext.Current in the class RenderTask is available, within the function ProcessUniqueItem(RenderResult renderResult, RenderTaskUniqueItemParameters parameters) the UmbracoContext.Current is null.
Because I process each unique item and render it very personalized, I need GetPropertyValue therefore the current UmbracoContext is necessary.
Is there a workaround to get and set the current UmbracoContext for this functionality?
Thanks in advance.
Hi!
We're aware of this and I have a work around in another project - I'll share some more info during the afternoon.
Hi!
Just to make sure that you understand =D This is a hack - but it should work =D You need to create an event-handler that move over the references to the sending thread. This is how it might look. We will create a better implementation i the future but this is what I got at the moment.
Hi!
Thanks for your Codesnippet. I implemented it in my Code but unfortunately the UmbracoContext.Current is in the method ProcessUniqueItem still null. So GetPropertyValue for the objects below has no value as well.
Hi!
Are you sure that the event is firing before you start sending?
// m
Hi Markus
When I start sending a Newsletter, it goes first of all to the method ProcessPreRender, then to the event SendNewsletterService.SendningInitializing += (sender, args) and then to the method ProcessUniqueItem()
Is that incorrect? If yes, how can I change this?
Thanks!
That sounds correct - the ProcessUniqueItem-method is called in a new thread and that's why the context is null but the code in the SendningInitializing -method should reinstantiate the UmbracoContext.
Could we talk over PM so that we can share some code? Send me an e-mail: markus [at sign goes here] enkelmedia.se.
Cheers!
I was able to resolve this issue with setting the HttpContext and the UmbracoContext newly in the ProcessUniqueItem method as followed:
is working on a reply...