diff --git a/src/Microsoft.AspNet.Mvc.Core/IScopedInstance.cs b/src/Microsoft.AspNet.Mvc.Core/IScopedInstance.cs new file mode 100644 index 0000000000..1afaafccf2 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/IScopedInstance.cs @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.AspNet.Mvc +{ + public interface IScopedInstance : IDisposable + { + TValue Value { get; set; } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/ScopedInstance.cs b/src/Microsoft.AspNet.Mvc.Core/ScopedInstance.cs new file mode 100644 index 0000000000..80e8dd6086 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/ScopedInstance.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.AspNet.Mvc +{ + public class ScopedInstance : IScopedInstance + { + public T Value { get; set; } + + public void Dispose() + { + var disposable = Value as IDisposable; + if (disposable != null) + { + disposable.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs index 46c48bc64e..1865b80ae5 100644 --- a/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs @@ -15,16 +15,14 @@ namespace Microsoft.Framework.DependencyInjection { public static class MvcServiceCollectionExtensions { - public static IServiceCollection AddMvc([NotNull] this IServiceCollection services) + public static IServiceCollection AddMvc([NotNull] this IServiceCollection services, IConfiguration config) { - return AddMvc(services, configuration: null); + return services.AddMvc(); } - public static IServiceCollection AddMvc( - [NotNull] this IServiceCollection services, - IConfiguration configuration) + public static IServiceCollection AddMvc([NotNull] this IServiceCollection services) { - ConfigureDefaultServices(services, configuration); + ConfigureDefaultServices(services); services.TryAdd(MvcServices.GetDefaultServices()); return services; } @@ -90,12 +88,12 @@ namespace Microsoft.Framework.DependencyInjection return WithControllersAsServices(services, controllerTypes.Select(type => type.AsType())); } - private static void ConfigureDefaultServices(IServiceCollection services, IConfiguration configuration) + private static void ConfigureDefaultServices(IServiceCollection services) { services.AddOptions(); services.AddDataProtection(); services.AddRouting(); - services.AddAuthorization(configuration); + services.AddAuthorization(); services.AddWebEncoders(); services.Configure( routeOptions => routeOptions.ConstraintMap.Add("exists", typeof(KnownRouteValueConstraint))); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ScopedInstanceTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ScopedInstanceTest.cs new file mode 100644 index 0000000000..b59fd356aa --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ScopedInstanceTest.cs @@ -0,0 +1,68 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Xunit; + +namespace Microsoft.AspNet.Mvc +{ + public class ScopedInstanceTest + { + [Fact] + public void ScopedInstanceDisposesIDisposables() + { + var disposable = new Disposable(); + + // Arrange + var scopedInstance = new ScopedInstance + { + Value = disposable, + }; + + // Act + scopedInstance.Dispose(); + + // Assert + Assert.True(disposable.IsDisposed); + } + + [Fact] + public void ScopedInstanceDoesNotThrowOnNonIDisposable() + { + // Arrange + var scopedInstance = new ScopedInstance() + { + Value = new object(), + }; + + // Act + scopedInstance.Dispose(); + } + + [Fact] + public void ScopedInstanceDoesNotThrowOnNull() + { + // Arrange + var scopedInstance = new ScopedInstance() + { + Value = null, // just making it explicit that there is not value set yet. + }; + + // Act + scopedInstance.Dispose(); + + // Assert + Assert.Null(scopedInstance.Value); + } + + private class Disposable : IDisposable + { + public bool IsDisposed { get; set; } + + public void Dispose() + { + IsDisposed = true; + } + } + } +} \ No newline at end of file