Answered by:
Defining options in Startup.ConfigureServices() vs Configure() methods

Question
-
User1160092068 posted
I am new to .net core framework. I found that defining options can be done interchangeably in ConfigureServices() and Configure() methods. For e.g. to enable CORS we can do either of the following :
public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddDefaultPolicy(builder => { builder.AllowAnyOrigin(); }); }); ... } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ... app.UseCors(); ... }
We can also achieve the same thing with this:
public void ConfigureServices(IServiceCollection services) { services.AddCors(); ... } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ... app.UseCors(x => x.AllowAnyOrigin()); ... }
I want to understand the difference between these two types of implementation.
Friday, May 28, 2021 12:25 PM
Answers
-
User475983607 posted
arnab_das
Thanks for the explanation. However I don't get why there is option to configure CORS in both ConfigureServices() as well as Configure(). Is this something by design. Any example where it would be useful to configure CORS in one vs the other?
You're making assumptions. The CORS service is registered and configured in ConfigurServices() then implemented as middleware in Configure(). Middleware. Most all the built-in ASP.NET Core Middleware components follow this pattern.
Middleware reference
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-5.0
Keep in mind, this is a just pattern. It possible to have one method in startup that handles all the configuration or all the configuration could be in the program.cs. This information is in the first link I shared...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 28, 2021 6:03 PM
All replies
-
User475983607 posted
ConfigureServices registers services for dependency injection. Configure is used to setup middleware.
See the docs; https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-5.0
Friday, May 28, 2021 1:32 PM -
User1160092068 posted
Thanks for the explanation. However I don't get why there is option to configure CORS in both ConfigureServices() as well as Configure(). Is this something by design. Any example where it would be useful to configure CORS in one vs the other?
Friday, May 28, 2021 4:51 PM -
User475983607 posted
arnab_das
Thanks for the explanation. However I don't get why there is option to configure CORS in both ConfigureServices() as well as Configure(). Is this something by design. Any example where it would be useful to configure CORS in one vs the other?
You're making assumptions. The CORS service is registered and configured in ConfigurServices() then implemented as middleware in Configure(). Middleware. Most all the built-in ASP.NET Core Middleware components follow this pattern.
Middleware reference
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-5.0
Keep in mind, this is a just pattern. It possible to have one method in startup that handles all the configuration or all the configuration could be in the program.cs. This information is in the first link I shared...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 28, 2021 6:03 PM