Taylor Goodall

moon indicating dark mode
sun indicating light mode

Extracting Service Configuration in Dotnet 5

December 08, 2020

This blog posts is a simple guide on how we can extract services from our startup.cs file separate configuration files allowing us to have dedicated configuration and keep our main startup.cs file as minimal as possible.

by default in ASP.net 5 we get Swagger out of the box, let’s start with extracting this out to to it’s own configuration file

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ExtractConfigBlogPost", Version = "v1" });
});
}

We can extract out the above code to a SwaggerConfig.cs file which will contain all of our swagger specific configuration as required. I have created a Configurations folder in the root of my project

using Microsoft.Extensions.DependencyInjection;
public static void AddSwaggerConfiguration(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ExtractConfigBlogPost", Version = "v1" });
});
}

We can then go ahead and Register this to our services in startup.cs Don’t forget to import the namespace of your configuration as well.

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerConfiguration();
}

we can also extract Swagger Configuration shown below

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ExtractConfigBlogPost v1"));
}
// Code removed for brevity
}

This can be done by adding the below method to our SwaggerConfig file

public static void UseSwaggerSetup(this IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ExtractConfigBlogPost v1"));
}

then update the ConfigureServices method to be

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwaggerSetup();
}

We can also use this pattern to bootstrap our own services and keep our startup as minimal as possible, You can break your services out as needed, for example you may want all of your repositories in a separate configuration file

using Microsoft.Extensions.DependencyInjection;
namespace ExtractConfigBlogPost.Configurations
{
public static class RepositoryBootStrapper
{
public static void RegisterRepositoryServices(this IServiceCollection services)
{
services.AddScoped(typeof(IOrderRepository), typeof(OrderRepository));
services.AddScoped(typeof(ICustomerRepository), typeof(CustomerRepository));
services.AddScoped(typeof(IStockRepository), typeof(StockRepository));
}
}
}

Update startup.cs as follows

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerConfiguration();
services.RegisterRepositoryServices();
}

Conclusion

Splitting our service configuration into separate files is an extremely useful pattern to use within your codebase and can make maintenance of your project a lot easier when it comes to knowing where to look for changes. It may not be worth it when setting up a fresh project, however if you notice your startup.cs start to become muddied I would recommend reaching for this pattern.


software Engineer, living in Adelaide, Australia Interested in building engaging products and solving problems