OdeToCode IC Logo

.NET Core Opinion 7 – Startup Responsibilities

Thursday, February 14, 2019

Over the years I’ve noticed that application startup code tends to attract smaller bits of code in the same way that a protostar accretes cosmic material until reaching the point where nuclear fusion begins. I’ve seen this happen in the main function of C programs, and (back when we never had enough HRESUTs to hold the HINSTANCEs of our HWINDOWs), in the WinMain function of C++ programs. I’ve also seen this happen inside of global.asa in classic ASP, and in global.asax.cs for ASP.NET. It’s as if we say to ourselves, "I only have two new lines of code to execute when the program starts up, so what could it hurt to jam these two lines in the middle of the 527 method calls we already have in the startup function?"

This post is a plea to avoid nuclear fusion in the Startup and Program files of ASP.NET Core.

Starting Up

There is a lengthy list of startup tasks for modern server applications. Warm up the cache, create the connection pool, configure the IoC container, instantiate the logging sinks, and all of this happens before you getting to the actual business of application startup. In ASP.NET Core, I used to see most of this logic end up inside of Startup.cs. Some of this code is moving over to Program.cs as developers start to recognize Program.Main as a usable entry point for a web application.

The next few opinionated posts will discuss strategies for organizing startup and entry code, and look at approaches you can use for clean startup code.

To get started, let’s talk about the Startup class in ASP.NET Core. If you believe every class should have a single responsibility, then it is easy to think the Startup class should manage all startup tasks. But, as I’ve already pointed out, there is a lot of work to do when starting up today’s apps. The Startup class, despite its name, should not take responsibility for any of these tasks.

In Startup, there are three significant methods with specific, limited assignments:

  • The constructor, where you can inject dependencies to use in the other methods.

  • ConfigureServices, where you can setup the service provider for the app

  • Configure, which should be limited to arranging middleware components in the correct order.

public class Startup
    public Startup(IConfiguration configuration)
         // ...

    public void ConfigureServices(IServiceCollection services)
         // ...

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
         // ...

Of course, you can also have environment specific methods, like ConfigureDevelopment and ConfigureProduction, but the rules remain the same. Initialization tasks like warming the cache don’t need to live in Startup, which already has enough to do. Where should these tasks go? That’s the topic for the next post.

Gravatar Ryan Tauriainen Friday, February 15, 2019
This seems to be the direction .NET Core is headed. For example, logging was moved from Startup.Configure() with ILoggerFactory to the WebHostBuilder.ConfigureLogging() extension method.
Gravatar Ygal Bellaiche Sunday, February 17, 2019
I have exactly to same problem you wrote about. I have a real application in production, startup.cs is more than 300 lines, and include routing, security, authentication and dependency injection. DI part must go out to another file.
Gravatar Aftab Ansari Friday, March 29, 2019
Dear Scott, where is the next post. Could not find. This is the opnion#7 can see opinion#9 but not #8.
Gravatar scott Sunday, March 31, 2019
The posts are all here, but out of order. See the "Archive" page from the menu entry at the top of this page.
Comments are closed.