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:
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 :-)
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...
I know the query is correct as the exact same code returns results when used in an API Controller:
Does anyone know what would be causing this? And are there any workarounds?
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?
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
Hi Marc,
No dice, just tried that but also got 0 results :(
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...
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.
Hey Simon,
Thanks for your suggestion, no change though unfortunately :(
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:
See if doing the same makes a difference for you :-)
Nik
Hey Nik,
I just tried using that inside of my composer, but I get the exception "No factory has been set" on
Current.Factory
.is working on a reply...