We have a background task that runs a sync service. When this service is finished syncing I want to add / remove umbraco content items based on that sync. The problem is that because this task runs in the background and is a fire and forget, I cannot access the umbraco context of the created scope. Because this seems to be disposed.
It works as follows. In the backoffice we have a button which calls an UmbracoAuthorizedApiController. This will run a Task fire and forget style.
Now I either need to create own scope in this new thread? How do I do this, or what is another way to access the umbraco context from this thread so that I can add and remove IContent items using the IContentService?
The way I normally do syncs in Umbraco is to have a controller (could be surface or API) that can be called to trigger the sync. So your scheduled task makes an HTTP request to the controller endpoint (you can secure with a header or query string) and you set a long time out. That way you have access to the server HTTP Context and all the other contexts, such as Umbraco Context.
Having said that, you shouldn't need to access the Umbraco context to use IContentService. You should be able to inject this in your own class and call it.
I can inject this, but when it reaches that code I get the message that context is not called from web request. Meaning the previous scope is disposed. Right now I have a workaround where I do not use a background thread but just run it from the API controller directly. Now the code does work, but ideally I want the handler (the controller) to instantly respond back to the caller, and execute the code in the background.
What Dan says should work, but I would not recommend it, it's generally not recommended to create endpoints with long response times (I remember reading this somewhere, though I couldn't find the source, so verification required). That being said, fire and forget tasks are also generally not recommended. Instead, one should create a background worker that can perform your synchronisation.
Getting back to your question though: You can easily create your own scope with the IUmbracoContextFactory like so:
public class MyService
{
private readonly IUmbracoContextFactory _umbraco;
public MyService(IUmbracoContextFactory umbraco) // injected here with Dependency Injection
{
_umbraco = umbraco;
}
public Task MyActionAsync()
{
using(var cref = _umbraco.EnsureUmbracoContext())
{
// your logic here
}
}
}
Access umbraco context in background thread
Hello,
We have a background task that runs a sync service. When this service is finished syncing I want to add / remove umbraco content items based on that sync. The problem is that because this task runs in the background and is a fire and forget, I cannot access the umbraco context of the created scope. Because this seems to be disposed.
It works as follows. In the backoffice we have a button which calls an
UmbracoAuthorizedApiController
. This will run aTask
fire and forget style.Now I either need to create own scope in this new thread? How do I do this, or what is another way to access the umbraco context from this thread so that I can add and remove
IContent
items using theIContentService
?The way I normally do syncs in Umbraco is to have a controller (could be surface or API) that can be called to trigger the sync. So your scheduled task makes an HTTP request to the controller endpoint (you can secure with a header or query string) and you set a long time out. That way you have access to the server HTTP Context and all the other contexts, such as Umbraco Context.
Having said that, you shouldn't need to access the Umbraco context to use
IContentService
. You should be able to inject this in your own class and call it.I can inject this, but when it reaches that code I get the message that context is not called from web request. Meaning the previous scope is disposed. Right now I have a workaround where I do not use a background thread but just run it from the API controller directly. Now the code does work, but ideally I want the handler (the controller) to instantly respond back to the caller, and execute the code in the background.
What Dan says should work, but I would not recommend it, it's generally not recommended to create endpoints with long response times (I remember reading this somewhere, though I couldn't find the source, so verification required). That being said, fire and forget tasks are also generally not recommended. Instead, one should create a background worker that can perform your synchronisation.
Getting back to your question though: You can easily create your own scope with the
IUmbracoContextFactory
like so:Hello Roy
I have just been in the same case, where with the background each 30 min, I need to add/edit or delete data.
I have the task look like this:
is working on a reply...