Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ASP.NET Core's Startup class not an interface or abstract class?

This is in regards to the design principals behind the Startup class explained here:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-2.1

I understand that the class needs to include methods like ConfigureServices or Configure.

Why CreateDefaultBuilder(args).UseStartup<Startup>() does not mandate any base class or interface for better readability?

With this design approach, someone must read the documentation and know about the magic method names like ConfigureServices or Configure.

If this is part of a new class design mindset, then where can I read more about it?

like image 425
Allan Xu Avatar asked Nov 12 '18 00:11

Allan Xu


People also ask

What is Startup class in ASP.NET Core?

The Startup class contains the ConfigureServices and Configure methods. While the former is used to configure the required services, the latter is used to configure the request processing pipeline. The Configure method is executed immediately after the ConfigureServices method.

What is Startup class in C#?

The Startup class is the entry point to the application, setting up configuration and wiring up services the application will use. Developers configure a request pipeline in the Startup class that is used to handle all requests made to the application. Sections: The Startup class.

Is startup class mandatory in ASP.NET Core?

Is Startup Class mandatory? Yes, this class is mandatory in ASP.net core application. It can have any access modifier (public, private, internal). This is the entry point of the ASP.net application.

Is ASP.NET Core good for startup?

Yes, it's great for startups. The . NET platform is incredibly productive, with lots of included functionality, very reliable, very fast, and is used by almost all of the Fortune 500 companies so developer talent and third-party resources are never hard to find. ASP.NET Core is just a web framework on top of the .

What is the startup class in ASP NET Core?

The Startup class ASP.NET Core apps use a Startup class, which is named Startup by convention.

What is ASP NET Core configuration?

Configuration in ASP.NET Core. The Startup class. ASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class: Optionally includes a ConfigureServices method to configure the app's services. A service is a reusable component that provides app functionality.

Is it necessary that the class name be startup in ASP NET?

No, it is not necessary that the class name be "Startup". The ASP.net core application is a Console app and we have to configure a web host to start listening. The "Program" class does this configuration. The web host is created in the main method of the Program class and here we have configuration of startup class using method "UseStartup".

How to configure a web host in ASP NET Core Application?

The ASP.net core application is a Console app and we have to configure a web host to start listening. The "Program" class does this configuration. The web host is created in the main method of the Program class and here we have configuration of startup class using method "UseStartup". So it is not necessary that class name is "Startup".


2 Answers

There are several reasons why its done the way its done. One of the more obvious reasons is, because you can inject services into Configure method, such as

public void Configure(IAppBuilder app, IMyService myService) {     myService.DoSomething(); } 

Obviously, you can't do that with interfaces, abstract classes or inheritence.

The second reason why its done by convention method is, that there is not only Configure/ConfigureServices method, there is an infinite number of environment-dependent configure methods.

public void Configure(IAppBuilder app) { } public void ConfigureDevelopment(IAppBuilder app) { } public void ConfigureProduction(IAppBuilder app) { } public void ConfigureStaging(IAppBuilder app) { } public void ConfigureSomethingElse(IAppBuilder app) { } 

and depending on your environment variable for ASPNET_ENVIRONMENT a different method will be chosen and executed (or the default Configure/ConfigureServices if no matching environment specific method was found).

None of this is possible with traditional OOP (inheritance/interfaces/abstract classes).

The same applies to other parts of ASP.NET Core, like Middlewares and the Invoke Method. The Invoke method can also have dependencies injected into it, but in order to call the next middleware you simply do

await next?.Invoke(); 

and do not have to worry which dependencies the next middleware requires or may take.

And to be complete, one can also have multiple Startup classes with the default method names (Configure/ConfigureServices) named StartupDevelopment, StartupProduction, Startup (as fallback) and ASP.NET Core will pick up the correct one based on Environment variable set.

like image 77
Tseng Avatar answered Sep 28 '22 15:09

Tseng


Startup class can be inherited from IStartup interface.

// \packages\microsoft.aspnetcore.hosting.abstractions\2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Hosting.Abstractions.dll namespace Microsoft.AspNetCore.Hosting {  public interface IStartup   {    IServiceProvider ConfigureServices(IServiceCollection services);    void Configure(IApplicationBuilder app);   } } 

By default wizard doesn't create template file with implementation from IStartup. Why not - probably mistake or influence of non-typed languages..

like image 44
Vitaliy Markitanov Avatar answered Sep 28 '22 16:09

Vitaliy Markitanov