Search code examples
asp.net-coreinversion-of-controlcastle-windsorregistrationaspnetboilerplate

How to register services in ASP.NET Boilerplate using IocManager?


For ASP.NET Core, I know how to register services using ConfigureServices method in Startup class.

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddScoped<IUrlHelper>(x => {
    var actionContext = x.GetRequiredService<IActionContextAccessor>().ActionContext;
    var factory = x.GetRequiredService<IUrlHelperFactory>();
    return factory.GetUrlHelper(actionContext);
});

How can I register same services using IocManager in my ASP.NET Boilerplate (ASP.NET Core) project so that I can use them in my application services?


Solution

  • AddSingleton

    IocManager.Register<IActionContextAccessor, ActionContextAccessor>();
    

    AddScoped + Factory

    IocManager.IocContainer.Register(
        Component
            .For<IUrlHelper>()
            .UsingFactoryMethod(kernel =>
            {
                var actionContext = kernel.Resolve<IActionContextAccessor>().ActionContext;
                var factory = kernel.Resolve<IUrlHelperFactory>();
                return factory.GetUrlHelper(actionContext);
            })
            .LifestyleCustom<MsScopedLifestyleManager>() // ASP.NET Core
         // .LifestylePerWebRequest()                    // ASP.NET MVC 5
    );
    

    References