From 8d7b95655de4f6e33a56965c4d869fa2934a8767 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Sat, 3 Oct 2015 11:57:35 -0700 Subject: [PATCH] Removing Autofac dependency --- Mvc.sln | 13 --- .../Monitoring/MonitoringMiddlware.cs | 110 ------------------ .../Monitoring/MonitoringModule.cs | 78 ------------- samples/MvcSample.Web/Startup.cs | 45 +------ samples/MvcSample.Web/project.json | 1 - .../DependencyResolverTests.cs | 31 ----- .../project.json | 1 - .../AutofacWebSite/AutofacWebSite.xproj | 18 --- .../Controllers/BasicController.cs | 15 --- .../Controllers/DIController.cs | 22 ---- .../AutofacWebSite/HelloWorldBuilder.cs | 13 --- test/WebSites/AutofacWebSite/Startup.cs | 39 ------- .../AutofacWebSite/Views/Basic/Index.cshtml | 1 - .../AutofacWebSite/Views/DI/Index.cshtml | 3 - test/WebSites/AutofacWebSite/project.json | 20 ---- test/WebSites/AutofacWebSite/readme.md | 4 - .../AutofacWebSite/wwwroot/HelloWorld.htm | 1 - 17 files changed, 1 insertion(+), 414 deletions(-) delete mode 100644 samples/MvcSample.Web/Monitoring/MonitoringMiddlware.cs delete mode 100644 samples/MvcSample.Web/Monitoring/MonitoringModule.cs delete mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/DependencyResolverTests.cs delete mode 100644 test/WebSites/AutofacWebSite/AutofacWebSite.xproj delete mode 100644 test/WebSites/AutofacWebSite/Controllers/BasicController.cs delete mode 100644 test/WebSites/AutofacWebSite/Controllers/DIController.cs delete mode 100644 test/WebSites/AutofacWebSite/HelloWorldBuilder.cs delete mode 100644 test/WebSites/AutofacWebSite/Startup.cs delete mode 100644 test/WebSites/AutofacWebSite/Views/Basic/Index.cshtml delete mode 100644 test/WebSites/AutofacWebSite/Views/DI/Index.cshtml delete mode 100644 test/WebSites/AutofacWebSite/project.json delete mode 100644 test/WebSites/AutofacWebSite/readme.md delete mode 100644 test/WebSites/AutofacWebSite/wwwroot/HelloWorld.htm diff --git a/Mvc.sln b/Mvc.sln index 5e9a1d721d..6d9205325a 100644 --- a/Mvc.sln +++ b/Mvc.sln @@ -35,8 +35,6 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ActivatorWebSite", "test\We EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "InlineConstraintsWebSite", "test\WebSites\InlineConstraintsWebSite\InlineConstraintsWebSite.xproj", "{EA34877F-1AC1-42B7-B4E6-15A093F40CAE}" EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AutofacWebSite", "test\WebSites\AutofacWebSite\AutofacWebSite.xproj", "{07C0E921-FCBB-458C-AC11-3D01CE68B16B}" -EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Mvc.TestConfiguration", "test\WebSites\Microsoft.AspNet.Mvc.TestConfiguration\Microsoft.AspNet.Mvc.TestConfiguration.xproj", "{680D75ED-601F-4D86-B01B-1072D0C31B8C}" EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "RoutingWebSite", "test\WebSites\RoutingWebSite\RoutingWebSite.xproj", "{42CDBF4A-E238-4C0F-A416-44588363EB4C}" @@ -314,16 +312,6 @@ Global {EA34877F-1AC1-42B7-B4E6-15A093F40CAE}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {EA34877F-1AC1-42B7-B4E6-15A093F40CAE}.Release|Mixed Platforms.Build.0 = Release|Any CPU {EA34877F-1AC1-42B7-B4E6-15A093F40CAE}.Release|x86.ActiveCfg = Release|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|x86.ActiveCfg = Debug|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Any CPU.Build.0 = Release|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|x86.ActiveCfg = Release|Any CPU {680D75ED-601F-4D86-B01B-1072D0C31B8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {680D75ED-601F-4D86-B01B-1072D0C31B8C}.Debug|Any CPU.Build.0 = Debug|Any CPU {680D75ED-601F-4D86-B01B-1072D0C31B8C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -1143,7 +1131,6 @@ Global {34DF1487-12C6-476C-BE0A-F31DF1939AE5} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {DB79BCBA-9538-4A53-87D9-77728E2BAA39} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {EA34877F-1AC1-42B7-B4E6-15A093F40CAE} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} - {07C0E921-FCBB-458C-AC11-3D01CE68B16B} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {680D75ED-601F-4D86-B01B-1072D0C31B8C} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {42CDBF4A-E238-4C0F-A416-44588363EB4C} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {5F945B82-FE5F-425C-956C-8BC2F2020254} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1} diff --git a/samples/MvcSample.Web/Monitoring/MonitoringMiddlware.cs b/samples/MvcSample.Web/Monitoring/MonitoringMiddlware.cs deleted file mode 100644 index 67873feff3..0000000000 --- a/samples/MvcSample.Web/Monitoring/MonitoringMiddlware.cs +++ /dev/null @@ -1,110 +0,0 @@ -// 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 System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using Autofac.Core; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; - -namespace MvcSample.Web -{ - /// - /// Summary description for MonitoringMiddlware - /// - public class MonitoringMiddlware - { - private RequestDelegate _next; - private IServiceProvider _services; - - public MonitoringMiddlware(RequestDelegate next, IServiceProvider services) - { - _next = next; - _services = services; - } - - public async Task Invoke(HttpContext httpContext) - { - var url = httpContext.Request.Path.Value; - - if (url.Equals("/Monitoring/Clear", StringComparison.OrdinalIgnoreCase)) - { - MonitoringModule.Clear(); - httpContext.Response.ContentType = "text/plain"; - var buffer = Encoding.ASCII.GetBytes("Cleared"); - httpContext.Response.Body.Write(buffer, 0, buffer.Length); - } - else if (url.Equals("/Monitoring/ActivatedTypes", StringComparison.OrdinalIgnoreCase)) - { - var data = ActivatedTypes(); - httpContext.Response.ContentType = "text/plain charset=utf-8"; - var buffer = Encoding.UTF8.GetBytes(data); - - httpContext.Response.Body.Write(buffer, 0, buffer.Length); - } - else - { - await _next(httpContext); - } - } - - public string ActivatedTypes() - { - var values = MonitoringModule.InstanceCount.ToArray(); - - var builder = new StringBuilder(); - - Array.Sort(values, new InstancesComparer()); - - foreach (var item in values) - { - builder.AppendLine(GetTypeName(item.Key.Item1) + " " + item.Value); - } - - return builder.ToString(); - } - - private string GetTypeName(Type type) - { - var name = type.Name; - var isArray = false; - - if (typeof(Array).IsAssignableFrom(type)) - { - isArray = true; - name = ChopLast2(name); - } - - var genericArgs = type.GetGenericArguments().Select(t => t.Name).ToArray(); - - if (genericArgs.Length > 0) - { - name = ChopLast2(name) + "<" + string.Join(",", genericArgs) + ">"; - } - - if (isArray) - { - name += "[]"; - } - - return name; - } - - private static string ChopLast2(string name) - { - return name.Remove(name.Length - 2); - } - - private class InstancesComparer : IComparer, int>> - { - public int Compare(KeyValuePair, int> x, KeyValuePair, int> y) - { - return y.Value.CompareTo(x.Value); - } - } - } -} diff --git a/samples/MvcSample.Web/Monitoring/MonitoringModule.cs b/samples/MvcSample.Web/Monitoring/MonitoringModule.cs deleted file mode 100644 index 4a1c570873..0000000000 --- a/samples/MvcSample.Web/Monitoring/MonitoringModule.cs +++ /dev/null @@ -1,78 +0,0 @@ -// 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 System.Collections.Concurrent; -using Autofac; -using Autofac.Core; - -namespace MvcSample.Web -{ - /// - /// Summary description for MonitoringModule - /// - public class MonitoringModule : Module - { - private static ConcurrentDictionary, int> _registrations - = new ConcurrentDictionary, int>(); - - private static ConcurrentDictionary, object> _instances - = new ConcurrentDictionary, object>(); - - public static readonly ConcurrentDictionary, int> InstanceCount - = new ConcurrentDictionary, int>(); - - protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, - IComponentRegistration registration) - { - registration.Activating += Registration_Activating; - registration.Activated += Registration_Activated; - } - - public Tuple GetKey(IComponentRegistration context) - { - var activator = context.Activator; - var lifeTime = context.Lifetime; - var limitType = context.Activator.LimitType; - - var key = new Tuple(limitType, lifeTime); - - return key; - } - private void Registration_Activated(object sender, ActivatedEventArgs e) - { - object instance; - var key = GetKey(e.Component); - if (_instances.TryGetValue(key, out instance)) - { - bool same = (e.Instance == instance); - InstanceCount.AddOrUpdate(key, 1, (_, count) => same ? 1 : count + 1); - } - } - - private void Registration_Activating(object sender, ActivatingEventArgs e) - { - var key = GetKey(e.Component); - _registrations.AddOrUpdate(key, 1, (k, value) => value + 1); - _instances.GetOrAdd(key, e.Instance); - } - - private void Registration_Preparing(object sender, PreparingEventArgs e) - { - foreach (var param in e.Parameters) - { - Console.WriteLine(param.ToString()); - } - } - - public static void Clear() - { - //string count = InstanceCount.Select(kvp => kvp.Value).Aggregate((c, n) => c + n).ToString() + " instances from " + InstanceCount.Count + " types"; - InstanceCount.Clear(); - _instances.Clear(); - _registrations.Clear(); - - // return count; - } - } -} diff --git a/samples/MvcSample.Web/Startup.cs b/samples/MvcSample.Web/Startup.cs index fa8fea4827..e43d9a717b 100644 --- a/samples/MvcSample.Web/Startup.cs +++ b/samples/MvcSample.Web/Startup.cs @@ -2,15 +2,10 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.IO; -using Autofac; -using Autofac.Framework.DependencyInjection; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.Razor; -using Microsoft.Framework.Configuration; using Microsoft.Framework.DependencyInjection; -using Microsoft.Dnx.Runtime; using MvcSample.Web.Filters; using MvcSample.Web.Services; @@ -18,8 +13,6 @@ namespace MvcSample.Web { public class Startup { - private bool _autoFac; - public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddCaching(); @@ -36,40 +29,8 @@ namespace MvcSample.Web services.AddSingleton(); services.AddSingleton(); services.AddTransient(); - - var applicationEnvironment = services.BuildServiceProvider().GetRequiredService(); - var configurationPath = Path.Combine(applicationEnvironment.ApplicationBasePath, "config.json"); - - // Set up configuration sources. - var configBuilder = new ConfigurationBuilder() - .AddJsonFile(configurationPath) - .AddEnvironmentVariables(); - - var configuration = configBuilder.Build(); - - var diSystem = configuration["DependencyInjection"]; - if (!string.IsNullOrEmpty(diSystem) && - diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase)) - { - _autoFac = true; - - // Create the autofac container - var builder = new ContainerBuilder(); - - // Create the container and use the default application services as a fallback - builder.Populate(services); - - builder.RegisterModule(); - - var container = builder.Build(); - - return container.Resolve(); - } - else - { - return services.BuildServiceProvider(); - } + return services.BuildServiceProvider(); } public void Configure(IApplicationBuilder app) @@ -77,10 +38,6 @@ namespace MvcSample.Web app.UseStatusCodePages(); app.UseFileServer(); - if (_autoFac) - { - app.UseMiddleware(); - } app.UseRequestLocalization(); app.UseSession(); diff --git a/samples/MvcSample.Web/project.json b/samples/MvcSample.Web/project.json index 53ac6ff07f..9367a317de 100644 --- a/samples/MvcSample.Web/project.json +++ b/samples/MvcSample.Web/project.json @@ -7,7 +7,6 @@ "warningsAsErrors": true }, "dependencies": { - "Autofac.Framework.DependencyInjection": "4.0.0-beta6-110", "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", "Microsoft.AspNet.Diagnostics": "1.0.0-*", "Microsoft.AspNet.Localization": "1.0.0-*", diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/DependencyResolverTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/DependencyResolverTests.cs deleted file mode 100644 index 5ed21ef7d9..0000000000 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/DependencyResolverTests.cs +++ /dev/null @@ -1,31 +0,0 @@ -// 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.Net.Http; -using System.Threading.Tasks; -using Xunit; - -namespace Microsoft.AspNet.Mvc.FunctionalTests -{ - public class DependencyResolverTests : IClassFixture> - { - public DependencyResolverTests(MvcTestFixture fixture) - { - Client = fixture.Client; - } - - public HttpClient Client { get; } - - [Theory] - [InlineData("http://localhost/di", "

Builder Output: Hello from builder.

")] - [InlineData("http://localhost/basic", "

Hello From Basic View

")] - public async Task AutofacDIContainerCanUseMvc(string url, string expectedResponseBody) - { - // Arrange & Act & Assert (does not throw) - // Make a request to start resolving DI pieces - var responseText = await Client.GetStringAsync(url); - - Assert.Equal(expectedResponseBody, responseText); - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index ba87883a98..98f204f163 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -14,7 +14,6 @@ "AntiforgeryTokenWebSite": "1.0.0", "ApiExplorerWebSite": "1.0.0", "ApplicationModelWebSite": "1.0.0", - "AutofacWebSite": "1.0.0", "BasicWebSite": "1.0.0", "BestEffortLinkGenerationWebSite": "1.0.0", "CompositeViewEngineWebSite": "1.0.0", diff --git a/test/WebSites/AutofacWebSite/AutofacWebSite.xproj b/test/WebSites/AutofacWebSite/AutofacWebSite.xproj deleted file mode 100644 index 044af8546f..0000000000 --- a/test/WebSites/AutofacWebSite/AutofacWebSite.xproj +++ /dev/null @@ -1,18 +0,0 @@ - - - - 14.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - 07c0e921-fcbb-458c-ac11-3d01ce68b16b - ..\..\..\artifacts\obj\$(MSBuildProjectName) - ..\..\..\artifacts\bin\$(MSBuildProjectName)\ - - - 2.0 - 49623 - - - \ No newline at end of file diff --git a/test/WebSites/AutofacWebSite/Controllers/BasicController.cs b/test/WebSites/AutofacWebSite/Controllers/BasicController.cs deleted file mode 100644 index 4deb3e593f..0000000000 --- a/test/WebSites/AutofacWebSite/Controllers/BasicController.cs +++ /dev/null @@ -1,15 +0,0 @@ -// 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 Microsoft.AspNet.Mvc; - -namespace AutofacWebSite.Controllers -{ - public class BasicController : Controller - { - public IActionResult Index() - { - return View(); - } - } -} diff --git a/test/WebSites/AutofacWebSite/Controllers/DIController.cs b/test/WebSites/AutofacWebSite/Controllers/DIController.cs deleted file mode 100644 index 0f085f2ab8..0000000000 --- a/test/WebSites/AutofacWebSite/Controllers/DIController.cs +++ /dev/null @@ -1,22 +0,0 @@ -// 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 Microsoft.AspNet.Mvc; - -namespace AutofacWebSite.Controllers -{ - public class DIController : Controller - { - public DIController(HelloWorldBuilder builder) - { - Builder = builder; - } - - public HelloWorldBuilder Builder { get; private set; } - - public IActionResult Index() - { - return View(model: Builder.Build()); - } - } -} diff --git a/test/WebSites/AutofacWebSite/HelloWorldBuilder.cs b/test/WebSites/AutofacWebSite/HelloWorldBuilder.cs deleted file mode 100644 index 6a94f4ed55..0000000000 --- a/test/WebSites/AutofacWebSite/HelloWorldBuilder.cs +++ /dev/null @@ -1,13 +0,0 @@ -// 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. - -namespace AutofacWebSite -{ - public class HelloWorldBuilder - { - public string Build() - { - return "Hello from builder."; - } - } -} \ No newline at end of file diff --git a/test/WebSites/AutofacWebSite/Startup.cs b/test/WebSites/AutofacWebSite/Startup.cs deleted file mode 100644 index fdf2577501..0000000000 --- a/test/WebSites/AutofacWebSite/Startup.cs +++ /dev/null @@ -1,39 +0,0 @@ -// 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 Autofac; -using Autofac.Framework.DependencyInjection; -using Microsoft.AspNet.Builder; -using Microsoft.Framework.DependencyInjection; - -namespace AutofacWebSite -{ - public class Startup - { - // Set up application services - public IServiceProvider ConfigureServices(IServiceCollection services) - { - services.AddMvc(); - services.AddTransient(); - - var builder = new ContainerBuilder(); - builder.Populate(services); - - var container = builder.Build(); - - return container.Resolve(); - } - - public void Configure(IApplicationBuilder app) - { - app.UseCultureReplacer(); - - app.UseMvc(routes => - { - // This default route is for running the project directly. - routes.MapRoute("default", "{controller=DI}/{action=Index}"); - }); - } - } -} diff --git a/test/WebSites/AutofacWebSite/Views/Basic/Index.cshtml b/test/WebSites/AutofacWebSite/Views/Basic/Index.cshtml deleted file mode 100644 index 2457ac3a83..0000000000 --- a/test/WebSites/AutofacWebSite/Views/Basic/Index.cshtml +++ /dev/null @@ -1 +0,0 @@ -

Hello From Basic View

\ No newline at end of file diff --git a/test/WebSites/AutofacWebSite/Views/DI/Index.cshtml b/test/WebSites/AutofacWebSite/Views/DI/Index.cshtml deleted file mode 100644 index d4c4236f10..0000000000 --- a/test/WebSites/AutofacWebSite/Views/DI/Index.cshtml +++ /dev/null @@ -1,3 +0,0 @@ -@model string - -

Builder Output: @Model

\ No newline at end of file diff --git a/test/WebSites/AutofacWebSite/project.json b/test/WebSites/AutofacWebSite/project.json deleted file mode 100644 index ea5b2ec684..0000000000 --- a/test/WebSites/AutofacWebSite/project.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "commands": { - "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001", - "kestrel": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:5000" - }, - "dependencies": { - "Autofac.Framework.DependencyInjection": "4.0.0-beta6-110", - "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", - "Microsoft.AspNet.Mvc": "6.0.0-*", - "Microsoft.AspNet.Mvc.TestConfiguration": "1.0.0", - "Microsoft.AspNet.Server.IIS": "1.0.0-*", - "Microsoft.AspNet.Server.WebListener": "1.0.0-*", - "Microsoft.AspNet.StaticFiles": "1.0.0-*" - }, - "frameworks": { - "dnx451": { }, - "dnxcore50": { } - }, - "webroot": "wwwroot" -} diff --git a/test/WebSites/AutofacWebSite/readme.md b/test/WebSites/AutofacWebSite/readme.md deleted file mode 100644 index c0a1ca6c4e..0000000000 --- a/test/WebSites/AutofacWebSite/readme.md +++ /dev/null @@ -1,4 +0,0 @@ -AutofacWebSite -=== - -This web site illustrates how to use Autofac as the DI container for an MVC application. diff --git a/test/WebSites/AutofacWebSite/wwwroot/HelloWorld.htm b/test/WebSites/AutofacWebSite/wwwroot/HelloWorld.htm deleted file mode 100644 index 3da1ec26e9..0000000000 --- a/test/WebSites/AutofacWebSite/wwwroot/HelloWorld.htm +++ /dev/null @@ -1 +0,0 @@ -HelloWorld