+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Creating Content null reference error when creating content in a separate thread

Creating Content null reference error when creating content in a separate thread

3 posts, 0 answered
  1. OakTree Digital
    OakTree Digital avatar
    27 posts
    Registered:
    06 Sep 2012
    04 Mar 2011
    Link to this post
    I've created a GC-Based module and for it have written an import routine that parses a CSV of about 5000 lines, and creates a new content item for each line. This routine works fine, but with a CSV of this length, we get a page timeout before the operation completes.

    Rather than increase the page timeout (it would have be be increased significantly), I tried executing my routine in a new thread. However, I get a null reference exception when calling ContentManager.Content.SaveContent(IContent content ) to create new content items. 

    After some investigation, it looks like the exception occurs in the StagedContent.Publish method, and occurs when the HostingEnvironent is checked. Since it's a new thread, i'm guessing HostingEnvironment is null.

    Eventually giving up on that approach, I've decided to build a WCF web service for adding content items, and call it batches from my long-running task. However, upon creating my web service, I've realized that i still get a null reference exception when calling SateContent from within the service. It appears that this occurs because Whie the HostingEnvironment is now not null, it's IsHosted property returns true, causing the applicationPath local variable to be set to HttpContext.Current.Request.ApplicationPath, causing another null reference exception.

    I'd subclass the StagedContent class and skip that piece of logic, but it's sealed.

    Are there any workarounds for this? We very much need to be able to import these large chunks of data, and doing it from within InitializeControls in the page lifecylcle is not acceptable.

    Thanks.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    07 Mar 2011
    Link to this post
    Hi Stuart ,

    You have to migrate your data through web service that is called from your application whereas the web service should be located in Sitefinity.
    There are several places where we use HostingEnvironment and IsHosted property which gets a value indicating whether the current application domain is being hosted by an ApplicationManager object. If you use WCF services you need to enable AspNetCompatibility

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. OakTree Digital
    OakTree Digital avatar
    27 posts
    Registered:
    06 Sep 2012
    11 Mar 2011
    Link to this post
    Thanks, enabling AspNetCompatiblity solved the issue with the web service.
Register for webinar
3 posts, 0 answered