Copied to clipboard

Flag this post as spam?

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


  • Paul de Quant 403 posts 1520 karma points
    Mar 18, 2016 @ 09:36
    Paul de Quant
    0

    EnsureContext issue

    Hello,

    I'm trying to use the following code in a IAsyncResult:-

                var searchResults = ApplicationContext.Current.Services.ContentService.GetContentOfContentType(contentType.Id);
    
                var httpBase = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);
    
                UmbracoContext.EnsureContext(httpBase, ApplicationContext.Current, new WebSecurity(httpBase, ApplicationContext.Current), UmbracoConfig.For.UmbracoSettings(), UrlProviderResolver.Current.Providers, false);
    
                UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
    
                var searchResultsFiltered = searchResults.Where(x => x.Name.ToLower().Contains(searchBy.ToLower())).Select(x => new { x.Name, Path = helper.NiceUrl(x.Id) });
    

    However, the results always comes back as:-

    Value cannot be null. Parameter name: httpContext

    I'm not that familiar with EnsureContext, so not sure I'm using it right. Can someone clarify how this should be done please.

    Many thanks

    Paul

  • Paul de Quant 403 posts 1520 karma points
    Mar 18, 2016 @ 10:00
    Paul de Quant
    102

    I've managed to work out how to implement this.

    Basically I had to create a new class called ContextHelper:-

     public static class ContextHelper
    {
        public static void EnsureUmbracoContext()
        {
            if (UmbracoContext.Current == null)
            {
                var dummyHttpContext = new HttpContextWrapper(new HttpContext(new SimpleWorkerRequest("blah.aspx", "", new StringWriter())));
                UmbracoContext.EnsureContext(
                    dummyHttpContext,
                    ApplicationContext.Current,
                    new WebSecurity(dummyHttpContext, ApplicationContext.Current),
                    UmbracoConfig.For.UmbracoSettings(),
                    UrlProviderResolver.Current.Providers,
                    false);
            }
        }
    
    }
    

    Then in my IAsyncResult file, I just called:-

                IContentType contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("KnowledgebaseArticle");
    
                var searchResults = ApplicationContext.Current.Services.ContentService.GetContentOfContentType(contentType.Id);
    
                ContextHelper.EnsureUmbracoContext(); // THIS IS NEW
    
                UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
    
                var searchResultsFiltered = searchResults.Where(x => x.Name.ToLower().Contains(searchBy.ToLower())).Select(x => new { x.Name, Path = helper.NiceUrl(x.Id) });
    

    For more information take a look at this link:-

    https://gist.github.com/sniffdk/7600822

    Cheers

    Paul

Please Sign in or register to post replies

Write your reply to:

Draft