Copied to clipboard

Flag this post as spam?

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


  • Corné Strijkert 80 posts 456 karma points c-trib
    Oct 22, 2020 @ 14:51
    Corné Strijkert
    0

    How to access one of the Umbraco services in TransformingIndexValues event

    Hi all,

    I've a load balanced set up with one master and one replica.

    I've implemented a custom TransformingIndexValues event to edit the ValueSet being indexed.

    When I add the following code to the first lines of code in this event I will always get a null reference exception on the replica (frontend server) at tagService.GetAllTag(null,"nl-nl"). Same thing happens when I try to access the IContentTypeService.

    private void IndexProviderSiteIndexOnTransformingIndexValues(object sender, IndexingItemEventArgs e)
    {
          var tagService = Current.Factory.GetInstance<ITagService>();
          var tags = tagService.GetAllTags(null, "en-US");
     }
    

    So it seems that I want to access the database to early? When I add a breakpoint and then wait a little then nothing goes wrong.

    Any help much appreciated

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Oct 22, 2020 @ 20:12
    Marc Goodson
    0

    Hi Corné

    I wonder if you would still have the issue if you wrap the call to the service inside a scope, the Examine event you are hooking into, can execute on a background thread, and in these circumstances there isn't always an UmbracoContext available.

    There is a bit of an explanation on this thread:

    https://our.umbraco.com/forum/using-umbraco-and-getting-started/102676-triggering-index-rebuild-via-hangfire-causes-objectdisposedexception-in-nucache

    and a note in the docs here

    https://our.umbraco.com/Documentation/Reference/Searching/Examine/examine-events#creating-a-transformingindexvalues-event

    suggesting you use a scope when accessing services too.

    of course... may make no difference!

    regards

    marc

  • Corné Strijkert 80 posts 456 karma points c-trib
    Oct 23, 2020 @ 06:51
    Corné Strijkert
    0

    Hi Marc,

    Thank you so much.

    Unfortunately the error still occurs, but slightly different.

    I've the following code inside my Transforming event:

    private void IndexProviderSiteIndexOnTransformingIndexValues(object sender, IndexingItemEventArgs e)
            {
                using (var scope = _scopeProvider.CreateScope(autoComplete: true))
                {
                    using (var umbracoContextReference = _contextFactory.EnsureUmbracoContext())
                    {
                        var tagService = Current.Factory.GetInstance<ITagService>();
    
                        var tags = tagService.GetAllTags(null, "en-US");
    
                        if (e.ValueSet.Category == IndexTypes.Content)
                        {
                            Parse(e);
                        }
                    }
                }
            }
    

    Exception was thrown on the closing curly brance of the using (var scope = _scopeProvider.CreateScope(autoComplete: true)) line with the following error:

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    I had a look at the Log Viewer and there I found this log entry:

    Error indexing queue items System.NullReferenceException: Object reference not set to an instance of an object. at Umbraco.Core.Scoping.Scope.<>cDisplayClass71_0.2() in d:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 437 at Umbraco.Core.Scoping.Scope.TryFinally(Int32 index, Action[] actions) in d:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 472 at Umbraco.Core.Scoping.Scope.TryFinally(Int32 index, Action[] actions) in d:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 476 at Umbraco.Core.Scoping.Scope.TryFinally(Int32 index, Action[] actions) in d:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 476 at Umbraco.Core.Scoping.Scope.RobustExit(Boolean completed, Boolean onException) in d:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 420 at Umbraco.Core.Scoping.Scope.Dispose() in d:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 363

    Another problem related to this is that is seems taht the following error 'randomly' occurs:

    An error occured
    Lock request time out period exceeded. The statement has been terminated.
    
    Exception Details
    System.Data.SqlClient.SqlException: Lock request time out period exceeded. The statement has been terminated.
    

    Another thing I currently don't understand is that when I attach the debugger and add a breakpoint to the using (var scope line on the Slave Umbraco and then publish a page on the Master Umbraco, the breakpoint will hit once, but I got two null reference exceptions on the closing curly brace of the using (var scope line.

    So I'm totally confused right now. Thanx for any further help or suggestions.

    Corne

  • Corné Strijkert 80 posts 456 karma points c-trib
    Oct 23, 2020 @ 08:26
    Corné Strijkert
    0

    Small update:

    When I add a Thread.Sleep of 2 seconds as the first line of code inside the TransformingValues method it works fine.

    Also I tested the replica with blanc umbraco 8.6.3 installation with only the search stuff: IndexComponent, Composer and a ServerRegistrar instance to tell Umbraco which installation replica or master.

    Very weird..

    Also, I see a lot of messages in the log now when using scopeprovider without actually doing stuff with the scope:

    Missed "ScopeContext" Object "0d7ee66e"
    
  • Zaki Naeem 2 posts 72 karma points
    Dec 16, 2020 @ 14:04
    Zaki Naeem
    0

    Hi Corné,

    Did you ever get this resolved? We're running into a similar issue and have a feeling this eventually leads to Examine index failures. WIll be good to know if you had this sorted on your end and possibly share some insights.

  • Corné Strijkert 80 posts 456 karma points c-trib
    Dec 16, 2020 @ 14:59
    Corné Strijkert
    0

    Unfortunately, I've never beaten this problem.. It's more stable currently, but not fully stable. Lot of debugging and config changes later I don't know anymore what exactly made it 'more' stable...

    Because you're in a load balanced setup it seems that processes are trying to do the same things and these collide.

    So dirty workaround could be to 'wait' until the object is not null anymore, before continuing with indexing based on Umbraco services.

    Corné

Please Sign in or register to post replies

Write your reply to:

Draft