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 21 posts 198 karma points MVP c-trib
    Oct 18, 2021 @ 15:46
    Owain Jones

    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:

    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 1653 posts 10865 karma points MVP 6x c-trib
    Oct 18, 2021 @ 16:33
    Marc Goodson

    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:

    or might give you a steer to an answer.



  • Owain Jones 21 posts 198 karma points MVP c-trib
    Oct 18, 2021 @ 16:49
    Owain Jones

    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

    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 21 posts 198 karma points MVP c-trib
    Oct 19, 2021 @ 09:08
    Owain Jones

    Hey Simon,

    Thanks for your suggestion, no change though unfortunately :(

  • Nik 1480 posts 6517 karma points MVP 4x c-trib
    Oct 18, 2021 @ 21:07

    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.ScopeManagerProvider = new PerLogicalCallContextScopeManagerProvider();

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


  • Owain Jones 21 posts 198 karma points MVP c-trib
    Oct 19, 2021 @ 08:58
    Owain Jones

    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

Please Sign in or register to post replies

Write your reply to: