AddCors service options settings not working .net Core 3 Web API

128 views Asked by At

I am absolutely pulling my hair out and I'm sure its something really silly. But I've been working through some .net Core tutorials and I'm trying to implement CORS. I'm going through all the help pages but on this piece of code

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy(name: MyAllowSpecificOrigins,
                                  builder =>
                                  {
                                      builder.WithOrigins("https://localhost:1111/",
                                                          "https://localhost:2222/");
                                  });
            });

            // services.AddResponseCaching();
            services.AddControllers();
        }

I can't get any options to be available for my 'options' (apart from making it ToString etc rather than do something) and I'm getting:

'object' does not contain a definition for 'AddPolicy' and no accessible extension method 'AddPolicy' accepting a first argument of type 'object' could be found

I have installed various Nuget packages but nothing seems to be working?

Edit As requested, IServiceCollection includes:

public interface IServiceCollection
    {
        void AddControllers();
        void AddCors(Action<object> p);
        void AddMvc();
    }
1

There are 1 answers

2
ΩmegaMan On

When it says object it is unable to determine the type. Try casting the object beforehand with the actual type or interface such as:

services.Cast<{TheClassThatImplemntsAnItemInCollection | ITypeInCollection }>()
        .AddCors( options =>...

The type of collection is not what goes into the cast but its members.