// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.Extensions.DependencyInjection.Extensions; namespace Microsoft.Extensions.DependencyInjection { /// /// Extension methods for setting up cross-origin resource sharing services in an . /// public static class CorsServiceCollectionExtensions { /// /// Adds cross-origin resource sharing services to the specified . /// /// The to add services to. /// The so that additional calls can be chained. public static IServiceCollection AddCors(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddOptions(); services.TryAdd(ServiceDescriptor.Transient()); services.TryAdd(ServiceDescriptor.Transient()); return services; } /// /// Adds cross-origin resource sharing services to the specified . /// /// The to add services to. /// An to configure the provided . /// The so that additional calls can be chained. public static IServiceCollection AddCors(this IServiceCollection services, Action setupAction) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (setupAction == null) { throw new ArgumentNullException(nameof(setupAction)); } services.AddCors(); services.Configure(setupAction); return services; } } }