Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write ConfigureServices Method In Another Class File

We have a template for our projects in our company in which they write AddTransient<>() methods inside another class. I want to know how to put ConfigureServices method of startup inside another class.

See we have a very simple startup project like this:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddTransient<LocationService, LocationService>();
        services.AddTransient<PersonService, PersonService>();
        services.AddTransient<UserService, UserService>();
        services.AddAutoMapper(typeof(Startup));
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

I want to move my (dependent class registration) to another class inside my project.

So this will be my new Startup.cs:

 public class Startup
{
public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddAutoMapper(typeof(Startup));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}
}

And this will be my ExampleFileName.cs :

 public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<LocationService, LocationService>();
    services.AddTransient<PersonService, PersonService>();
    services.AddTransient<UserService, UserService>();
}
like image 970
Ali.Rashidi Avatar asked Oct 20 '25 19:10

Ali.Rashidi


1 Answers

First, you have to create a folder named Extensions and there you will create a file name whatever you want. I give my file name is ApplicationService.

Extensions/ApplicationServiceExtensions.cs

namespace API.Extensions
{
    public static class ApplicationServiceExtensions
    {
        public static IServiceCollection AddApplicationServices(this IServiceCollection services, IConfiguration config)
        {
             
            services.AddTransient<LocationService, LocationService>();
            services.AddTransient<PersonService, PersonService>();
            services.AddTransient<UserService, UserService>();

            return services;

        }
    }
}

startup.cs

 private readonly IConfiguration _config;
        public Startup(IConfiguration config)
        {
            _config = config;
           
        }

        public IConfiguration Configuration { get; }

        //clarify code



 public void ConfigureServices(IServiceCollection services)
        {
           services.AddApplicationServices(_config);
           
            //clarify code
        }

So,it's an example of putting the ConfigureServices method of startup inside another class. and which you want.

like image 69
Pritom Sarkar Avatar answered Oct 22 '25 11:10

Pritom Sarkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!