Umbraco 13 DI question, no longer using startup.cs...
# help-with-umbraco
m
In my Umb12 instance when I wanted to add a custom class to use in my razor pages I added it in the startup.cs file using
Copy code
services.AddScoped<>();
then the
Copy code
@inject
command in the razor page. In my new Umb13 there is no startup.cs, checking with ChatGPT it says I now need to make a custom "Composer" class and inject in there. Is this the correct way now? Here is the code it generated:
Copy code
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;

public class CustomComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.Services.AddScoped<IMyCustomService, MyCustomService>();
    }
}
d
A composer is a good place to do this. Can definitely recommend. Alternatively, you register your services inside program.cs now.
m
Do I need to do anything special or just make the compioser using the template ChatGPT gave and then I am golden? I suck at DI and the only way I have ever done it was in the startup.cs usign the addcsoped call
d
That's pretty much it. Umbraco automatically picks up the composers, so simply making the class is enough
I must admit, the Umbraco docs don't clearly specify how this works. Best I could find was this: https://docs.umbraco.com/umbraco-cms/reference/using-ioc And it barely answers your question
What it comes down to is that since Umbraco 13, the program.cs and startup.cs files have merged into a single program.cs file. In program.cs you can do the same things that you used to do in startup.cs
m
Thanks, I had seen that but was confused on it. I will make the composer and if I run into any issues I will post up here.
I was able to make the file and it just all worked. Thanks for the help.
22 Views