Copied to clipboard

Flag this post as spam?

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


  • Owain Jones 65 posts 437 karma points MVP 4x c-trib
    Oct 18, 2021 @ 15:46
    Owain Jones
    0

    Examine search query inside hangfire task returning 0 results

    I've followed Sebastiaan's example for allowing dependency injection in Hangfire Tasks, as found here: https://our.umbraco.com/forum/umbraco-8/96445-hangfire-dependency-injection#comment-316037

    But, when I inject the IExamineManager and perform a query, I get 0 results... enter image description here

    I know the query is correct as the exact same code returns results when used in an API Controller: enter image description here

    Does anyone know what would be causing this? And are there any workarounds?

  • Marc Goodson 2157 posts 14435 karma points MVP 9x c-trib
    Oct 18, 2021 @ 16:33
    Marc Goodson
    0

    Hi Owain

    Just a thought, but have you tried wrapping your examine stuff inside a scope? in case it's a lack of transient scope on a background thread that's causing the issue?

     using (_scopeProvider.CreateScope(autoComplete: true))
     {
             // Do your examine reading stuff here
     }
    

    There is an answer from Shannon here that gives a bit of context:

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

    or might give you a steer to an answer.

    regards

    Marc

  • Owain Jones 65 posts 437 karma points MVP 4x c-trib
    Oct 18, 2021 @ 16:49
    Owain Jones
    0

    Hi Marc,

    No dice, just tried that but also got 0 results :( enter image description here

    I bet it does have something to do with it being a background task and not having a proper scope/context though.

    I'm tempted to create an API that the HangFire task just pings to get results, but that just feels a bit hacky...

  • Simon Ulmbrant 34 posts 154 karma points
    Oct 18, 2021 @ 16:51
    Simon Ulmbrant
    0

    Try remove _scope?.Dispose(); in DisposeScope().

    I had issues when rebuilding indexes with hangfire and this line disposed the umbraco context causing the rebuild to crash.

  • Owain Jones 65 posts 437 karma points MVP 4x c-trib
    Oct 19, 2021 @ 09:08
    Owain Jones
    0

    Hey Simon,

    Thanks for your suggestion, no change though unfortunately :(

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Oct 18, 2021 @ 21:07
    Nik
    0

    Hey Owain,

    I think I had a similar problem when trying to do something similar with Hangfire.

    For some reason, I found that some of the Umbraco services didn't work if you "just" passed the existing context. I never figured out the reason for that but instead I ended up creating my own and re-registering all the services using the following block of code:

    var serviceContainer = new LightInject.ServiceContainer();
    foreach (var t in (Current.Factory.Concrete as LightInject.ServiceContainer).AvailableServices)
                serviceContainer.Register(t);
    serviceContainer.ScopeManagerProvider = new PerLogicalCallContextScopeManagerProvider();
    

    See if doing the same makes a difference for you :-)

    Nik

  • Owain Jones 65 posts 437 karma points MVP 4x c-trib
    Oct 19, 2021 @ 08:58
    Owain Jones
    0

    Hey Nik,

    I just tried using that inside of my composer, but I get the exception "No factory has been set" on Current.Factory. enter image description here

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies