Copied to clipboard

Flag this post as spam?

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


  • Jon Dunfee 199 posts 468 karma points
    Jun 08, 2014 @ 20:27
    Jon Dunfee
    0

    Mocking UmbracoContext for Examine Custom Indexer

    I've gone so many directions with this and time to resort to the forums.  I have a helper class I use extensively where I've gone from working with DynamicContent to IContent to now IPublishedContent as I've been encountering issues after upgrading from 6.1.6 to 6.2.1.  The site is snappy now and I'm happy with everything EXCEPT my custom indexer isn't working cause I need to access UmbracoContext for the UmbracoHelper.

    Helper = new UmbracoHelper(UmbracoContext.Current);

    I thought I might be able to fake one like the following:

    // mock up a context if necessary
    if (HttpContext.Current == null)
    {
        var mockUrl = "http://mydomain.com";
        HttpContext.Current = new HttpContext(
            new HttpRequest("", mockUrl, ""),
            new HttpResponse(new StringWriter())
            )
        {
            User = new GenericPrincipal(
                new GenericIdentity("myname"),
                new string[0]
                )
        };
    }
    
    if (UmbracoContext.Current == null)
    {
        UmbracoContext.EnsureContext(new HttpContextWrapper(HttpContext.Current), ApplicationContext.Current);
    }

    It seems to create one, but later in the indexer I receive an error, Cannot render a macro when UmbracoContext.PageId is null. I have a single instance of Umbraco with several domains bound to a single node structure, it pulls in content specific to the domain and I have a custom indexer to render out and index only the content which would be served for each individual domain's content.  This was all working in 6.1.6, but stopped working when I upgraded to 6.2 and later to 6.2.1.  Any suggestions on how I mock up a legitemate UmbracoContext?

    Link to similar issue I was having which lead me to getting this working to start with until now: http://our.umbraco.org/forum/developers/extending-umbraco/47617-RenderMacro-for-a-Lucene-Index

  • Jon Dunfee 199 posts 468 karma points
    Jun 08, 2014 @ 21:10
    Jon Dunfee
    0

    I've kicked the can down the road a little.

    if (UmbracoContext.Current == null)
    {
        UmbracoContext.EnsureContext(new HttpContextWrapper(HttpContext.Current), ApplicationContext.Current);
        UmbracoContext.Current.HttpContext.Items["pageID"] = e.NodeId;
    }
    

    New error: Cannot render a macro when there is no current PublishedContentRequest.

  • Devin Bost 37 posts 78 karma points c-trib
    Sep 07, 2014 @ 06:17
    Devin Bost
    0

    If you figure out how to fake the UmbracoContext, please let me know how you accomplished this.

  • Jon Dunfee 199 posts 468 karma points
    Sep 07, 2014 @ 17:39
    Jon Dunfee
    0

    I didn't.  I went a different direction and basically extracted all the information from the macro tags and basically manually generated the output in code enough to index what I needed to satisfy search results.  I only had a handful of macros so it wasn't too bad, not ideal, but enough.

Please Sign in or register to post replies

Write your reply to:

Draft