I'm pretty new to DI and haven't worked with it before. I need to be able to call a function that creating some nodes in umbraco without sending all the surfacecontroller parameters:
I've tried to read the documentation, but can't get it through my skull how i can make this work.
Right now i have my method in a SurfaceController, but i need to run the method en a schedule task setup. And i can't just instantiate a new surfacecontroller object and call the function (i dont have access to send all the parameters when instantiate).
I think what you need to create is a 'service', an independent piece of code that does your stuff, that can be called from a SurfaceController or from your Scheduled Task.
It would have a constructor that takes in ONLY the dependencies required to achieve the task - so probably wouldn't need all the dependencies that a SurfaceController needs and so would be easier to create in your Scheduled Task.
There is quite a good article about Services and Helpers here: https://our.umbraco.com/documentation/implementation/Services/ (that is long but has examples!!! - if you look at how the 'SiteService' works in the example, it shows you how to create an Interface, an implementation of the interface and the registration of the service with dependency injection which allows you to 'inject it' into places like your SurfaceController...
eg
public class DownloadingFeedService : IDownloadingFeedService {
// if your feed needs to query umbraco you'll need the factory to get access to the Umbraco Context
private readonly IUmbracoContextFactory _umbracoContextFactory;
public DownloadingFeedService(IUmbracoContextFactory umbracoContextFactory)
{
_umbracoContextFactory = umbracoContextFactory;
}
public async Task<string> DoStuff(){
// do stuff
}
}
Then in a composer or your StartUp file you can register your service
Thank you Mark, that helped me a little bit further.
But i do really need all the surface controller properties. my methods need CRUD access to both content and media sections in Umbraco.
Maybe I just don't get it. Normaly i call the function from a scheduled task in Plesk (by url). But this new project is hosted on the Umbraco Cloud, and they don't offer this "url scheduled task" option.
And i can't get my head around the best way to do this :/
Hi, I think you want to inject the IContentService and IMediaService. It doesn't matter if you inject this in to a controller, or a service you've set up yourself.
Yes, I want to use the RescurringHostedService. But i'm still a little confused. I have addede the IContentService and IMediaService to the constructor:
Umbraco 10 Dependency injection question
Hi all
I'm pretty new to DI and haven't worked with it before. I need to be able to call a function that creating some nodes in umbraco without sending all the surfacecontroller parameters:
I've tried to read the documentation, but can't get it through my skull how i can make this work.
Right now i have my method in a SurfaceController, but i need to run the method en a schedule task setup. And i can't just instantiate a new surfacecontroller object and call the function (i dont have access to send all the parameters when instantiate).
Hi Henrik
I think what you need to create is a 'service', an independent piece of code that does your stuff, that can be called from a SurfaceController or from your Scheduled Task.
It would have a constructor that takes in ONLY the dependencies required to achieve the task - so probably wouldn't need all the dependencies that a SurfaceController needs and so would be easier to create in your Scheduled Task.
There is quite a good article about Services and Helpers here: https://our.umbraco.com/documentation/implementation/Services/ (that is long but has examples!!! - if you look at how the 'SiteService' works in the example, it shows you how to create an Interface, an implementation of the interface and the registration of the service with dependency injection which allows you to 'inject it' into places like your SurfaceController...
eg
Then in a composer or your StartUp file you can register your service
Then you'll be able to inject into your SurfaceController:
or at least that is the gist of it!
regards
Marc
Thank you Mark, that helped me a little bit further.
But i do really need all the surface controller properties. my methods need CRUD access to both content and media sections in Umbraco.
Maybe I just don't get it. Normaly i call the function from a scheduled task in Plesk (by url). But this new project is hosted on the Umbraco Cloud, and they don't offer this "url scheduled task" option.
And i can't get my head around the best way to do this :/
Hi, I think you want to inject the
IContentService
andIMediaService
. It doesn't matter if you inject this in to a controller, or a service you've set up yourself.https://our.umbraco.com/documentation/Fundamentals/Code/Umbraco-Services/
Hi Henrik
If it's a scheduled task you need to run, do you need the scheduling aspect to occur 'outside fo Umbraco'?
Because you could use the inbuilt RecurringHostedService functionality?
There is an example here in the docs:
https://our.umbraco.com/documentation/reference/Scheduling/
You can see they've injected the IContentService into the task, you can do the same with IMediaService to be able to perform your CRUD
regards
marc
Hi Marc
Yes, I want to use the RescurringHostedService. But i'm still a little confused. I have addede the IContentService and IMediaService to the constructor:
But I still need access to MediaFileManager and MediaUrlGeneratorCollection (I have access to these in my current surfacecontroller).
Hi Henrik
I reckon, that you could inject them too?
hopefully...
regards
Marc
is working on a reply...