Copied to clipboard

Flag this post as spam?

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


  • Per Bolmstedt 84 posts 380 karma points
    Apr 02, 2022 @ 16:13
    Per Bolmstedt
    0

    Replacing IHostingEnvironment

    How do I replace the built-in IHostingEnvironment service?

    Umbraco registers this to be AspNetCoreHostingEnvironment using

    builder.Services.AddUnique<IHostingEnvironment, AspNetCoreHostingEnvironment>(); // Umbraco.Web.Common\DependencyInjection\UmbracoBuilderExtensions.cs
    

    but I would like to replace this service with my own.

    I'm not sure how or where to do this.

  • Per Bolmstedt 84 posts 380 karma points
    Apr 03, 2022 @ 15:25
    Per Bolmstedt
    0

    It seems this composer hack works:

    public void Compose(IUmbracoBuilder builder)    {
        var hostingEnvironmentServices = builder.Services.Where(service => service.ServiceType == typeof(IHostingEnvironment)).ToList();
        foreach (var hostingEnvironmentService in hostingEnvironmentServices)
            builder.Services.Remove(hostingEnvironmentService);
        builder.Services.AddUnique<
            IHostingEnvironment,
            MyHostingEnvironment>();
    }
    

    Surely there's a better way?

Please Sign in or register to post replies

Write your reply to:

Draft