Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • pbl_dk 150 posts 551 karma points
    Jan 12, 2024 @ 15:44
    pbl_dk
    0

    Register custom service in program.cs Umbraco 13

    Hi there umbracos I am trying to figure out how to register a simple singleton service in the program.cs, but I am a bit lost, since there is a builder, but no services.

    How is it supposed to be done? What is the .AddNotificationHandler doing in this context? I have done this, but it gives an error:

    using Umbraco.Cms.Core.DependencyInjection;
    using Microsoft.Extensions.DependencyInjection;
    
    
    WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
    
    
    builder.CreateUmbracoBuilder()
        .AddBackOffice()
        .AddWebsite()
        .AddDeliveryApi()
        .AddComposers()
        .Services.AddSingleton<IDataService, DataService>()
        .Build();
    
    
    WebApplication app = builder.Build();
    

    The docs says: "

    Registering dependencies for your site

    When working with your site, and not a package, the recommended way to register dependencies is with the Program.cs file:

    builder.CreateUmbracoBuilder()
        .AddBackOffice()
        .AddWebsite()
        .AddDeliveryApi()
        .AddComposers()
        // If you need to add something Umbraco specific, do it in the "AddUmbraco" builder chain, using the IUmbracoBuilder extension methods.
        .AddNotificationHandler<ContentTypeSavedNotification, ContentTypeSavedHandler>()
        .Build();"
    
  • pbl_dk 150 posts 551 karma points
    Jan 12, 2024 @ 16:12
    pbl_dk
    100

    Seems I got it to work with:

    WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
    
    builder.CreateUmbracoBuilder()
        .AddBackOffice()
        .AddWebsite()
        .AddDeliveryApi()
        .AddComposers()
        .Build();
    
    builder.Services.AddSingleton<IDataService, DataService>();
    WebApplication app = builder.Build();
    

    Still not quite sure that I havent missed something..

Please Sign in or register to post replies

Write your reply to:

Draft