Copied to clipboard

Flag this post as spam?

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


  • Ian 23 posts 113 karma points
    Mar 28, 2016 @ 09:51
    Ian
    0

    Task.Run with GetPropertyValue gives NullRefException

    Hi lovely umbraco community :) I wonder if anyone has seen this / if its by design:

    I have a NullReferenceException being generated when I try to use GetPropertyValue within a method called from Task.Run, within a surface controller.

    The Task.Run part looks like this:

    Task.Run(() => {
      SendInternalEmail(model); //do async
    });
    

    And the SendInternalEmail method roughly like this:

    private void SendInternalEmail(MyModel model)
    {
      //NOTE: the GetPropertyValue throws a NullReferenceException when this method is run async!
      //-even though the node is correctly found and the prop can be seen on it with a value
      var node = Umbraco.TypedContent(model.NodeId); 
      string emailAddress = node.GetPropertyValue<string>("internalEmailAddress");
    

    The umbraco logs show this (confidential code replaced with mycode):

     2016-03-28 10:29:19,663 [P10052/D15/T471] ERROR <mycode> - Object reference not set to an instance of an object.
    System.NullReferenceException: Object reference not set to an instance of an object.
       at Umbraco.Web.Templates.TemplateUtilities.ParseInternalLinks(String text, Boolean preview)
       at Umbraco.Web.PropertyEditors.ValueConverters.TextStringValueConverter.ConvertDataToSource(PublishedPropertyType propertyType, Object source, Boolean preview)
       at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.<.ctor>b__0()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.<.ctor>b__1()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at Umbraco.Web.PublishedPropertyExtension.GetValue[T](IPublishedProperty property, Boolean withDefaultValue, T defaultValue)
       at Umbraco.Web.PublishedContentExtensions.GetPropertyValue[T](IPublishedContent content, String alias)
       at <mycode>.SendInternalEmail(MyModel model) in C:\Development\<mycode>:line 71
    

    Any one seen this? Am I missing something? Im using umbraco v7.4.

    tia Ian

  • David Armitage 510 posts 2081 karma points
    Jun 09, 2016 @ 05:25
    David Armitage
    0

    Hi,

    I am not sure if it is related but since moving to Umbraco 7.4 I have experience a few similar problems. My problems have mainly been running umbraco saves within the background tasks. The gets I haven't really noticed any problems but I haven't looked at the logs in a while.

    Basically the problems I've been having is the context is missing and when I attempt to update / save something into umbraco within a background task it fails.

    I googled around and found an hack to fake the context.

    //fake the context to get around an error
    //exception details: Value cannot be null.Parameter name: httpContext
    HttpContext.Current = new HttpContext(new HttpRequest(null, "https://www.google.com", null), new HttpResponse(null));
    

    I am not sure if your issues are caused by the same thing but I have a feeling they might be. Try adding this block of code before dealing with the Umbraco gets and sets. See if it helps.

    Kind Regards

    David

  • Mark Wemekamp 73 posts 385 karma points
    Feb 07, 2017 @ 16:20
    Mark Wemekamp
    0

    Have you managed to fix this problem?

    I'm currently experiencing the exact same problem. Running a task with a GetPropertyValue call in it returns a NullReferenceException

    I've tried adding the fake HttpContext on a few places, but no luck so far.

    Kind regards,

    Mark

  • Ian 23 posts 113 karma points
    Feb 08, 2017 @ 08:19
    Ian
    0

    Hi Mark

    no I didnt fix it, I just worked around it by getting all the properties I needed before the Task.Run call and just passing them in.

    Ian

  • Tom 3 posts 73 karma points
    Mar 04, 2019 @ 22:05
    Tom
    0

    Hey, I know this is an old post but I ran into a similar issue and was able to resolve it. I passed reference to the the current Umbraco context to the async function and then in that function I got the property like so:

    IPublishedProperty myProp = myNode.Properties.Where(x => x.PropertyTypeAlias == "myPropertyAlias").First();
    string myString = (string)myProp.DataValue;
    

    Hope this helps somebody.

  • Georgios Rovolis 9 posts 80 karma points
    Aug 16, 2019 @ 10:53
    Georgios Rovolis
    0

    Legend! Works fine any idea if the value is an Id or Udi, how I can access that entity as well?

Please Sign in or register to post replies

Write your reply to:

Draft