locked
Defining options in Startup.ConfigureServices() vs Configure() methods RRS feed

  • 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

All replies