[Fixes #33]AddCors should adopt new pattern
This commit is contained in:
parent
dca5829b29
commit
96c7850e4c
|
|
@ -9,9 +9,7 @@ namespace UseOptions
|
||||||
{
|
{
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddCors();
|
services.AddCors(options =>
|
||||||
services.ConfigureCors(
|
|
||||||
options =>
|
|
||||||
options.AddPolicy("allowSingleOrigin", builder => builder.WithOrigins("http://example.com")));
|
options.AddPolicy("allowSingleOrigin", builder => builder.WithOrigins("http://example.com")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,11 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.AspNet.Cors;
|
||||||
using Microsoft.AspNet.Cors.Core;
|
using Microsoft.AspNet.Cors.Core;
|
||||||
|
using Microsoft.Framework.Configuration;
|
||||||
using Microsoft.Framework.DependencyInjection.Extensions;
|
using Microsoft.Framework.DependencyInjection.Extensions;
|
||||||
|
using Microsoft.Framework.Internal;
|
||||||
|
|
||||||
namespace Microsoft.Framework.DependencyInjection
|
namespace Microsoft.Framework.DependencyInjection
|
||||||
{
|
{
|
||||||
|
|
@ -13,12 +16,30 @@ namespace Microsoft.Framework.DependencyInjection
|
||||||
public static class CorsServiceCollectionExtensions
|
public static class CorsServiceCollectionExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Can be used to configure services in the <paramref name="serviceCollection"/>.
|
/// Add services needed to support CORS to the given <paramref name="serviceCollection"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="serviceCollection">The service collection which needs to be configured.</param>
|
/// <param name="serviceCollection">The service collection to which CORS services are added.</param>
|
||||||
|
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
||||||
|
public static IServiceCollection AddCors(this IServiceCollection serviceCollection)
|
||||||
|
{
|
||||||
|
if (serviceCollection == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(serviceCollection));
|
||||||
|
}
|
||||||
|
|
||||||
|
serviceCollection.AddOptions();
|
||||||
|
serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsService, CorsService>());
|
||||||
|
serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsPolicyProvider, DefaultCorsPolicyProvider>());
|
||||||
|
return serviceCollection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add services needed to support CORS to the given <paramref name="serviceCollection"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serviceCollection">The service collection to which CORS services are added.</param>
|
||||||
/// <param name="configure">A delegate which is run to configure the services.</param>
|
/// <param name="configure">A delegate which is run to configure the services.</param>
|
||||||
/// <returns></returns>
|
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
||||||
public static IServiceCollection ConfigureCors(
|
public static IServiceCollection AddCors(
|
||||||
this IServiceCollection serviceCollection,
|
this IServiceCollection serviceCollection,
|
||||||
Action<CorsOptions> configure)
|
Action<CorsOptions> configure)
|
||||||
{
|
{
|
||||||
|
|
@ -32,20 +53,8 @@ namespace Microsoft.Framework.DependencyInjection
|
||||||
throw new ArgumentNullException(nameof(configure));
|
throw new ArgumentNullException(nameof(configure));
|
||||||
}
|
}
|
||||||
|
|
||||||
return serviceCollection.Configure(configure);
|
serviceCollection.Configure(configure);
|
||||||
}
|
return serviceCollection.AddCors();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Add services needed to support CORS to the given <paramref name="serviceCollection"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="serviceCollection">The service collection to which CORS services are added.</param>
|
|
||||||
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
|
||||||
public static IServiceCollection AddCors(this IServiceCollection serviceCollection)
|
|
||||||
{
|
|
||||||
serviceCollection.AddOptions();
|
|
||||||
serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsService, CorsService>());
|
|
||||||
serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsPolicyProvider, DefaultCorsPolicyProvider>());
|
|
||||||
return serviceCollection;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -70,8 +70,7 @@ namespace Microsoft.AspNet.Cors.Test
|
||||||
},
|
},
|
||||||
services =>
|
services =>
|
||||||
{
|
{
|
||||||
services.AddCors();
|
services.AddCors(options =>
|
||||||
services.ConfigureCors(options =>
|
|
||||||
{
|
{
|
||||||
options.AddPolicy("customPolicy", policy);
|
options.AddPolicy("customPolicy", policy);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue