Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Mar 13, 2015 @ 14:36
    Dave Woestenborghs
    0

    Umbraco.RenderTemplate Error

    Hi all,

    I'm trying to use the Umbraco.RenderTemplate function in a Examine GatheringNodeData event. I'm trying doing do this so I can parse the output of the page and store the content in the examin index. Otherwise I would end up writing a lot of indexing code for all my bits and pieces for example archetype and grid property editors.

    The Umbraco version I am using is 7.1.9 and I use RouteHijacking

    This is the code that I have :

    Register GatheringNodeData event :

    namespace MyProject.Events
    {
    
        public class Registration : ApplicationEventHandler
        {
             /// <summary>
            /// The application started event handler
            /// </summary>
            /// <param name="umbracoApplication">
            /// The umbraco application.
            /// </param>
            /// <param name="applicationContext">
            /// The application context.
            /// </param>
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {            
                var helper = new UmbracoHelper(UmbracoContext.Current);
                ExamineManager.Instance.IndexProviderCollection["SiteIndexer"].GatheringNodeData
                             += (sender, e) => ExamineEvents.GatheringContentData(sender, e, helper);
    
                base.ApplicationStarted(umbracoApplication, applicationContext);
            }        
        }
    }

    GatheringNodeData event handler :

    namespace MyProject.Events
    {
    
        internal static class ExamineEvents
        {
    
            public static void GatheringContentData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
            {
                int id = e.NodeId;
    
                var content = helper.TypedContent(id);
    
                if (content != null)
                {
                    // code removed to add some custom fields to index
    
                    var output = helper.RenderTemplate(id);
    
                    // code removed to parse returned html
    
                    e.Fields.Add("Content", output.ToString());
                }
            }
        }
    }

    This is the error I get on the RenderTemplate function :

    Error rendering template with id 2645: 'System.InvalidOperationException: An error occurred when trying to create a controller of type 'MyProject.Controllers.DefaultController'. Make sure that the controller has a parameterless public constructor. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null.
    Parameter name: umbracoContext
       at Umbraco.Web.Mvc.RenderMvcController..ctor()
       at MyProject.DefaultController..ctor()
       --- End of inner exception stack trace ---
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.Activator.CreateInstance(Type type)
       at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
       --- End of inner exception stack trace ---
       at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
       at Umbraco.Web.Mvc.RenderControllerFactory.CreateController(RequestContext requestContext, String controllerName)
       at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
       at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
       at Umbraco.Web.Templates.TemplateRenderer.RenderUmbracoRequestToString(RequestContext requestContext)
       at Umbraco.Web.Templates.TemplateRenderer.ExecuteTemplateRendering(TextWriter sw, PublishedContentRequest contentRequest)
       at Umbraco.Web.Templates.TemplateRenderer.Render(StringWriter writer)
       at Umbraco.Web.UmbracoHelper.RenderTemplate(Int32 pageId, Nullable`1 altTemplateId)' 

    Anybody got an idea how to solve this ?

    Dave

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Mar 15, 2015 @ 23:19
    Shannon Deminick
    0

    Its because this happens on a background thread that has no HttpContext, this will basically just never work unless you new-up a new HttpContext with everything that might be required in a request which will be difficult. I don't think this is a very good way to achieve what you want and it will be awfully bad for performance.

    I understand the idea, you're basically attempting to index what Google would see. Even if you attempt to go down this route, you will have to create your own lucene analyzers to deal with the data since it will be full of HTML and information that you don't want to index.

    You're much better off just parsing the JSON for these property editors into separate fields. Also note that the DocumentWriting event in Examine gives you direct access to underlying Lucene Document instance so you can write whatever data you want there and create any fields you like without having to declare them in your examine config.

Please Sign in or register to post replies

Write your reply to:

Draft