Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

debug doesn't enter inside AddCors method

In startup.cs I have AddCors to services, I want to debug code from inside of AddCors, but Breackpoint doesn’t fire inside

 services.AddCors(options =>
            {
                //debug didn't fire here
                var corsOrigins = configuration["cors-origins"].Split(",");
                if ((corsOrigins.Length == 1)
                {
                    options.AddPolicy("CorsPolicy",
                        builder => builder.SetIsOriginAllowed(_ => true)
                            .AllowAnyMethod()
                            .AllowAnyHeader()
                            .AllowCredentials());
                }
                else
                {
                    options.AddPolicy("CorsPolicy",
                        builder => builder.WithOrigins(corsOrigins)
                            .AllowAnyMethod()
                            .AllowAnyHeader()
                            .AllowCredentials());
                }
            });

any ideas how can I stop code execution inside AddCors method?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The options builder inside will be evaluated when a request comes into the API. This is essentially because the middleware receives an IOptions<CorsOptions> from the container and evaluates its .Value property.

We can simulate this. Imagine you’re building the container:

ServiceCollection services = new ServiceCollection();
services.AddCors(options =>
    {
        var corsOrigins = configuration["cors-origins"].Split(",");
        if ((corsOrigins.Length == 1)
        {
            options.AddPolicy("CorsPolicy",
                builder => builder.SetIsOriginAllowed(_ => true)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
        }
        else
        {
            options.AddPolicy("CorsPolicy",
                builder => builder.WithOrigins(corsOrigins)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
        }
    });

IServiceProvider provider = services.BuildServiceProvider();
CorsOptions options = provider.GetRequiredService<IOptions<CorsOptions>>().Value;

Obviously you can allow ASP.NET Core to build the services, etc. you just need access to the IServiceProvider somewhere to retrieve the options from the container. This should hit your breakpoint inside the lambda function, or let you directly check the resulting CorsOptions object.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading