├── .markdownlint.json ├── Autofac.snk ├── bench ├── Autofac.Extensions.DependencyInjection.Bench │ ├── xunit.runner.json │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Benchmarks.cs │ ├── Program.cs │ ├── AutofacWebApplicationFactory.cs │ ├── Harness.cs │ ├── BenchmarkConfig.cs │ ├── RequestBenchmark.cs │ └── Autofac.Extensions.DependencyInjection.Bench.csproj └── projects │ └── Bench.AutofacApiServer │ ├── Properties │ ├── AssemblyInfo.cs │ └── launchSettings.json │ ├── appsettings.json │ ├── Controllers │ └── ValuesController.cs │ ├── Program.cs │ ├── Services.cs │ ├── BenchProject.AutofacApiServer.csproj │ └── DefaultStartup.cs ├── global.json ├── test ├── Integration.Net6 │ ├── appsettings.json │ ├── appsettings.Development.json │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ └── launchSettings.json │ ├── IDateProvider.cs │ ├── DateProvider.cs │ ├── Controllers │ │ └── DateController.cs │ ├── Program.cs │ ├── Startup.cs │ └── Integration.Net6.csproj ├── Integration.Net7 │ ├── appsettings.json │ ├── appsettings.Development.json │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ └── launchSettings.json │ ├── IDateProvider.cs │ ├── DateProvider.cs │ ├── Controllers │ │ └── DateController.cs │ ├── Program.cs │ ├── Startup.cs │ └── Integration.Net7.csproj ├── Integration.Net8 │ ├── appsettings.json │ ├── appsettings.Development.json │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ └── launchSettings.json │ ├── IDateProvider.cs │ ├── DateProvider.cs │ ├── Controllers │ │ └── DateController.cs │ ├── Program.cs │ ├── Startup.cs │ └── Integration.Net8.csproj ├── Autofac.Extensions.DependencyInjection.Test │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Specification │ │ ├── MicrosoftAssumedBehaviorTests.cs │ │ ├── FactoryAssumedBehaviorTests.cs │ │ ├── BuilderAssumedBehaviorTests.cs │ │ ├── FactorySpecificationTests.cs │ │ ├── FactoryKeyedSpecificationTests.cs │ │ ├── ChildScopeFactoryAssumedBehaviorTests.cs │ │ ├── BuilderSpecificationTests.cs │ │ ├── BuilderKeyedSpecificationTests.cs │ │ ├── ChildScopeFactorySpecificationTests.cs │ │ ├── ChildScopeFactoryKeyedSpecificationTests.cs │ │ └── AssumedBehaviorTests.cs │ ├── AutofacChildLifetimeScopeConfigurationAdapterTests.cs │ ├── ServiceProviderExtensionsTests.cs │ ├── TestCulture.cs │ ├── ServiceCollectionExtensionsTests.cs │ ├── Assertions.cs │ ├── Autofac.Extensions.DependencyInjection.Test.csproj │ ├── AutofacServiceProviderFactoryTests.cs │ ├── TypeManipulationFixture.cs │ ├── AutofacChildLifetimeScopeServiceProviderFactoryTests.cs │ └── AutofacRegistrationTests.cs └── Autofac.Extensions.DependencyInjection.Integration.Test │ ├── Properties │ └── AssemblyInfo.cs │ ├── IntegrationTests.cs │ └── Autofac.Extensions.DependencyInjection.Integration.Test.csproj ├── codecov.yml ├── .vscode ├── extensions.json ├── settings.json ├── launch.json └── tasks.json ├── Directory.Build.props ├── .github └── workflows │ ├── pre-commit.yml │ ├── dotnet-format.yml │ ├── build.yml │ ├── ci.yml │ └── publish.yml ├── NuGet.Config ├── Autofac.Extensions.DependencyInjection.sln.DotSettings ├── src └── Autofac.Extensions.DependencyInjection │ ├── Properties │ └── AssemblyInfo.cs │ ├── ServiceCollectionExtensions.cs │ ├── ServiceProviderExtensions.cs │ ├── AutofacChildLifetimeScopeConfigurationAdapter.cs │ ├── ServiceDescriptorExtensions.cs │ ├── AutofacServiceScopeFactory.cs │ ├── AutofacServiceScope.cs │ ├── FromKeyedServicesAttributeExtensions.cs │ ├── AutofacServiceProviderFactory.cs │ ├── AutofacChildLifetimeScopeServiceProviderFactory.cs │ ├── KeyedServiceMiddleware.cs │ ├── Autofac.Extensions.DependencyInjection.csproj │ ├── AnyKeyRegistrationSource.cs │ ├── KeyTypeConversionException.cs │ ├── KeyTypeConversionExceptionResources.resx │ ├── ServiceProviderExtensionsResources.resx │ ├── KeyTypeManipulationResources.resx │ ├── AutofacServiceProvider.cs │ └── KeyTypeManipulation.cs ├── .pre-commit-config.yaml ├── .gitattributes ├── LICENSE ├── .gitignore ├── default.proj ├── README.md ├── .editorconfig └── Autofac.Extensions.DependencyInjection.sln /.markdownlint.json: -------------------------------------------------------------------------------- 1 | { 2 | "MD013": false 3 | } 4 | -------------------------------------------------------------------------------- /Autofac.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/autofac/Autofac.Extensions.DependencyInjection/HEAD/Autofac.snk -------------------------------------------------------------------------------- /bench/Autofac.Extensions.DependencyInjection.Bench/xunit.runner.json: -------------------------------------------------------------------------------- 1 | { 2 | "shadowCopy": false 3 | } 4 | -------------------------------------------------------------------------------- /global.json: -------------------------------------------------------------------------------- 1 | { 2 | "sdk": { 3 | "rollForward": "latestFeature", 4 | "version": "8.0.414" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/Integration.Net6/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "None" 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/Integration.Net7/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "None" 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/Integration.Net8/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "None" 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/Integration.Net6/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information" 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/Integration.Net7/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information" 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/Integration.Net8/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information" 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | codecov: 2 | branch: develop 3 | require_ci_to_pass: true 4 | coverage: 5 | status: 6 | project: 7 | default: 8 | threshold: 1% 9 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "davidanson.vscode-markdownlint", 4 | "editorconfig.editorconfig", 5 | "ms-dotnettools.csdevkit", 6 | "travisillig.vscode-json-stable-stringify" 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | -------------------------------------------------------------------------------- /bench/projects/Bench.AutofacApiServer/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | [assembly: CLSCompliant(false)] 5 | -------------------------------------------------------------------------------- /test/Integration.Net6/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | [assembly: CLSCompliant(false)] 7 | -------------------------------------------------------------------------------- /test/Integration.Net7/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | [assembly: CLSCompliant(false)] 7 | -------------------------------------------------------------------------------- /test/Integration.Net8/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | [assembly: CLSCompliant(false)] 7 | -------------------------------------------------------------------------------- /bench/Autofac.Extensions.DependencyInjection.Bench/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | [assembly: CLSCompliant(false)] 5 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | [assembly: CLSCompliant(false)] 5 | -------------------------------------------------------------------------------- /bench/projects/Bench.AutofacApiServer/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "AllowedHosts": "*", 3 | "Logging": { 4 | "LogLevel": { 5 | "Default": "Information", 6 | "Microsoft": "Warning", 7 | "Microsoft.Hosting.Lifetime": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Integration.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | [assembly: CLSCompliant(false)] 7 | -------------------------------------------------------------------------------- /.github/workflows/pre-commit.yml: -------------------------------------------------------------------------------- 1 | name: pre-commit 2 | on: 3 | workflow_call: 4 | jobs: 5 | pre-commit: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v4 9 | - uses: actions/setup-python@v5 10 | with: 11 | python-version: 3.x 12 | - uses: pre-commit/action@v3.0.1 13 | -------------------------------------------------------------------------------- /test/Integration.Net6/IDateProvider.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | namespace Integration.Net6; 7 | 8 | public interface IDateProvider 9 | { 10 | DateTimeOffset GetDate(); 11 | } 12 | -------------------------------------------------------------------------------- /test/Integration.Net7/IDateProvider.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | namespace Integration.Net7; 7 | 8 | public interface IDateProvider 9 | { 10 | DateTimeOffset GetDate(); 11 | } 12 | -------------------------------------------------------------------------------- /test/Integration.Net8/IDateProvider.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | namespace Integration.Net8; 7 | 8 | public interface IDateProvider 9 | { 10 | DateTimeOffset GetDate(); 11 | } 12 | -------------------------------------------------------------------------------- /NuGet.Config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /test/Integration.Net6/DateProvider.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | namespace Integration.Net6; 7 | 8 | public class DateProvider : IDateProvider 9 | { 10 | public DateTimeOffset GetDate() => DateTimeOffset.UtcNow; 11 | } 12 | -------------------------------------------------------------------------------- /test/Integration.Net7/DateProvider.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | namespace Integration.Net7; 7 | 8 | public class DateProvider : IDateProvider 9 | { 10 | public DateTimeOffset GetDate() => DateTimeOffset.UtcNow; 11 | } 12 | -------------------------------------------------------------------------------- /test/Integration.Net8/DateProvider.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | 6 | namespace Integration.Net8; 7 | 8 | public class DateProvider : IDateProvider 9 | { 10 | public DateTimeOffset GetDate() => DateTimeOffset.UtcNow; 11 | } 12 | -------------------------------------------------------------------------------- /test/Integration.Net6/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "Integration.Net6": { 4 | "applicationUrl": "https://localhost:7154;http://localhost:7155", 5 | "commandName": "Project", 6 | "dotnetRunMessages": true, 7 | "environmentVariables": { 8 | "ASPNETCORE_ENVIRONMENT": "Development" 9 | }, 10 | "launchBrowser": true 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/Integration.Net7/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "Integration.Net6": { 4 | "applicationUrl": "https://localhost:7154;http://localhost:7155", 5 | "commandName": "Project", 6 | "dotnetRunMessages": true, 7 | "environmentVariables": { 8 | "ASPNETCORE_ENVIRONMENT": "Development" 9 | }, 10 | "launchBrowser": true 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/Integration.Net8/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "Integration.Net6": { 4 | "applicationUrl": "https://localhost:7154;http://localhost:7155", 5 | "commandName": "Project", 6 | "dotnetRunMessages": true, 7 | "environmentVariables": { 8 | "ASPNETCORE_ENVIRONMENT": "Development" 9 | }, 10 | "launchBrowser": true 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /bench/Autofac.Extensions.DependencyInjection.Bench/Benchmarks.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace Autofac.Extensions.DependencyInjection.Bench; 5 | 6 | public static class Benchmarks 7 | { 8 | public static readonly Type[] All = 9 | { 10 | typeof(RequestBenchmark), 11 | }; 12 | } 13 | -------------------------------------------------------------------------------- /Autofac.Extensions.DependencyInjection.sln.DotSettings: -------------------------------------------------------------------------------- 1 | 2 | True 3 | -------------------------------------------------------------------------------- /.github/workflows/dotnet-format.yml: -------------------------------------------------------------------------------- 1 | name: dotnet format 2 | on: 3 | workflow_call: 4 | jobs: 5 | dotnet-format: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v4 9 | - name: Setup .NET 8 10 | uses: actions/setup-dotnet@v4 11 | with: 12 | dotnet-version: 8.0.x 13 | - name: dotnet format 14 | run: dotnet format Autofac.Extensions.DependencyInjection.sln --verify-no-changes 15 | -------------------------------------------------------------------------------- /bench/Autofac.Extensions.DependencyInjection.Bench/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using BenchmarkDotNet.Running; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Bench; 7 | 8 | internal static class Program 9 | { 10 | internal static void Main(string[] args) => 11 | new BenchmarkSwitcher(Benchmarks.All).Run(args, new BenchmarkConfig()); 12 | } 13 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/MicrosoftAssumedBehaviorTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Test.Specification; 7 | 8 | public class MicrosoftAssumedBehaviorTests : AssumedBehaviorTests 9 | { 10 | protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 11 | { 12 | return serviceCollection.BuildServiceProvider(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /bench/projects/Bench.AutofacApiServer/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.AspNetCore.Mvc; 5 | 6 | namespace BenchProject.AutofacApiServer.Controllers; 7 | 8 | [Route("api/[controller]")] 9 | [ApiController] 10 | public class ValuesController : ControllerBase 11 | { 12 | private readonly A _service; 13 | 14 | public ValuesController(A service) 15 | { 16 | _service = service; 17 | } 18 | 19 | [HttpGet] 20 | public IActionResult Get() 21 | { 22 | return Ok(200); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Runtime.CompilerServices; 5 | 6 | [assembly: InternalsVisibleTo("Autofac.Extensions.DependencyInjection.Test, PublicKey=00240000048000009400000006020000002400005253413100040000010001008728425885ef385e049261b18878327dfaaf0d666dea3bd2b0e4f18b33929ad4e5fbc9087e7eda3c1291d2de579206d9b4292456abffbe8be6c7060b36da0c33b883e3878eaf7c89fddf29e6e27d24588e81e86f3a22dd7b1a296b5f06fbfb500bbd7410faa7213ef4e2ce7622aefc03169b0324bcd30ccfe9ac8204e4960be6")] 7 | [assembly: CLSCompliant(false)] 8 | -------------------------------------------------------------------------------- /test/Integration.Net6/Controllers/DateController.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace Integration.Net6.Controllers; 8 | 9 | [ApiController] 10 | [Route("[controller]")] 11 | public class DateController : ControllerBase 12 | { 13 | public DateController(IDateProvider dateProvider) 14 | { 15 | DateProvider = dateProvider; 16 | } 17 | 18 | public IDateProvider DateProvider { get; } 19 | 20 | [HttpGet] 21 | public ActionResult Get() => DateProvider.GetDate(); 22 | } 23 | -------------------------------------------------------------------------------- /test/Integration.Net7/Controllers/DateController.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace Integration.Net7.Controllers; 8 | 9 | [ApiController] 10 | [Route("[controller]")] 11 | public class DateController : ControllerBase 12 | { 13 | public DateController(IDateProvider dateProvider) 14 | { 15 | DateProvider = dateProvider; 16 | } 17 | 18 | public IDateProvider DateProvider { get; } 19 | 20 | [HttpGet] 21 | public ActionResult Get() => DateProvider.GetDate(); 22 | } 23 | -------------------------------------------------------------------------------- /test/Integration.Net8/Controllers/DateController.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace Integration.Net8.Controllers; 8 | 9 | [ApiController] 10 | [Route("[controller]")] 11 | public class DateController : ControllerBase 12 | { 13 | public DateController(IDateProvider dateProvider) 14 | { 15 | DateProvider = dateProvider; 16 | } 17 | 18 | public IDateProvider DateProvider { get; } 19 | 20 | [HttpGet] 21 | public ActionResult Get() => DateProvider.GetDate(); 22 | } 23 | -------------------------------------------------------------------------------- /bench/projects/Bench.AutofacApiServer/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace BenchProject.AutofacApiServer; 5 | 6 | public static class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | var host = CreateHostBuilder(args).Build(); 11 | 12 | host.Run(); 13 | } 14 | 15 | private static IHostBuilder CreateHostBuilder(string[] args) => 16 | Host.CreateDefaultBuilder(args) 17 | .ConfigureWebHostDefaults(webBuilder => 18 | { 19 | webBuilder.UseStartup(); 20 | }); 21 | } 22 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/FactoryAssumedBehaviorTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Test.Specification; 7 | 8 | public class FactoryAssumedBehaviorTests : AssumedBehaviorTests 9 | { 10 | protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 11 | { 12 | var factory = new AutofacServiceProviderFactory(); 13 | return factory.CreateServiceProvider(factory.CreateBuilder(serviceCollection)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pre-commit/pre-commit-hooks 3 | rev: "cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b" # v5.0.0 4 | hooks: 5 | - id: check-json 6 | - id: check-yaml 7 | - id: check-merge-conflict 8 | - id: end-of-file-fixer 9 | - id: trailing-whitespace 10 | - repo: https://github.com/igorshubovych/markdownlint-cli 11 | rev: "192ad822316c3a22fb3d3cc8aa6eafa0b8488360" # v0.45.0 12 | hooks: 13 | - id: markdownlint 14 | args: 15 | - --fix 16 | - repo: https://github.com/tillig/json-sort-cli 17 | rev: "009ab2ab49e1f2fa9d6b9dfc31009ceeca055204" # v3.0.0 18 | hooks: 19 | - id: json-sort 20 | args: 21 | - --autofix 22 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/BuilderAssumedBehaviorTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Test.Specification; 7 | 8 | public class BuilderAssumedBehaviorTests : AssumedBehaviorTests 9 | { 10 | protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 11 | { 12 | var builder = new ContainerBuilder(); 13 | builder.Populate(serviceCollection); 14 | var container = builder.Build(); 15 | return container.Resolve(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /bench/Autofac.Extensions.DependencyInjection.Bench/AutofacWebApplicationFactory.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.AspNetCore.Mvc.Testing; 5 | using Microsoft.Extensions.Hosting; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Bench; 8 | 9 | public class AutofacWebApplicationFactory : WebApplicationFactory 10 | where TStartup : class 11 | { 12 | protected override IHost CreateHost(IHostBuilder builder) 13 | { 14 | ArgumentNullException.ThrowIfNull(builder); 15 | builder.UseServiceProviderFactory(new AutofacServiceProviderFactory()); 16 | 17 | return base.CreateHost(builder); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/Integration.Net6/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Threading.Tasks; 5 | using Autofac.Extensions.DependencyInjection; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Hosting; 8 | 9 | namespace Integration.Net6; 10 | 11 | public static class Program 12 | { 13 | public static IHostBuilder CreateHostBuilder(string[] args) => 14 | Host.CreateDefaultBuilder(args) 15 | .UseServiceProviderFactory(new AutofacServiceProviderFactory()) 16 | .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup()); 17 | 18 | public static async Task Main(string[] args) => await CreateHostBuilder(args).Build().RunAsync().ConfigureAwait(false); 19 | } 20 | -------------------------------------------------------------------------------- /test/Integration.Net7/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Threading.Tasks; 5 | using Autofac.Extensions.DependencyInjection; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Hosting; 8 | 9 | namespace Integration.Net7; 10 | 11 | public static class Program 12 | { 13 | public static IHostBuilder CreateHostBuilder(string[] args) => 14 | Host.CreateDefaultBuilder(args) 15 | .UseServiceProviderFactory(new AutofacServiceProviderFactory()) 16 | .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup()); 17 | 18 | public static async Task Main(string[] args) => await CreateHostBuilder(args).Build().RunAsync().ConfigureAwait(false); 19 | } 20 | -------------------------------------------------------------------------------- /test/Integration.Net8/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Threading.Tasks; 5 | using Autofac.Extensions.DependencyInjection; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Hosting; 8 | 9 | namespace Integration.Net8; 10 | 11 | public static class Program 12 | { 13 | public static IHostBuilder CreateHostBuilder(string[] args) => 14 | Host.CreateDefaultBuilder(args) 15 | .UseServiceProviderFactory(new AutofacServiceProviderFactory()) 16 | .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup()); 17 | 18 | public static async Task Main(string[] args) => await CreateHostBuilder(args).Build().RunAsync().ConfigureAwait(false); 19 | } 20 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/FactorySpecificationTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.DependencyInjection.Specification; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Test; 8 | 9 | public class FactorySpecificationTests : DependencyInjectionSpecificationTests 10 | { 11 | public override bool SupportsIServiceProviderIsService => true; 12 | 13 | protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 14 | { 15 | var factory = new AutofacServiceProviderFactory(); 16 | return factory.CreateServiceProvider(factory.CreateBuilder(serviceCollection)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/FactoryKeyedSpecificationTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.DependencyInjection.Specification; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Test; 8 | 9 | public class FactoryKeyedSpecificationTests : KeyedDependencyInjectionSpecificationTests 10 | { 11 | public override bool SupportsIServiceProviderIsKeyedService => true; 12 | 13 | protected override IServiceProvider CreateServiceProvider(IServiceCollection collection) 14 | { 15 | var factory = new AutofacServiceProviderFactory(); 16 | return factory.CreateServiceProvider(factory.CreateBuilder(collection)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/ChildScopeFactoryAssumedBehaviorTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Test.Specification; 7 | 8 | public class ChildScopeFactoryAssumedBehaviorTests : AssumedBehaviorTests 9 | { 10 | protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 11 | { 12 | var container = new ContainerBuilder().Build(); 13 | var rootScope = container.BeginLifetimeScope(); 14 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(() => rootScope); 15 | return factory.CreateServiceProvider(factory.CreateBuilder(serviceCollection)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/BuilderSpecificationTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.DependencyInjection.Specification; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Test; 8 | 9 | public class BuilderSpecificationTests : DependencyInjectionSpecificationTests 10 | { 11 | public override bool SupportsIServiceProviderIsService => true; 12 | 13 | protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 14 | { 15 | var builder = new ContainerBuilder(); 16 | builder.Populate(serviceCollection); 17 | var container = builder.Build(); 18 | return container.Resolve(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/AutofacChildLifetimeScopeConfigurationAdapterTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace Autofac.Extensions.DependencyInjection.Test; 5 | 6 | public sealed class AutofacChildLifetimeScopeConfigurationAdapterTests 7 | { 8 | [Fact] 9 | public void AddMultipleConfigurationContainsConfigurations() 10 | { 11 | var adapter = new AutofacChildLifetimeScopeConfigurationAdapter(); 12 | adapter.Add(builder => { }); 13 | adapter.Add(builder => { }); 14 | 15 | Assert.Equal(2, adapter.ConfigurationActions.Count); 16 | } 17 | 18 | [Fact] 19 | public void AddNullConfigurationThrows() 20 | => Assert.Throws(() => new AutofacChildLifetimeScopeConfigurationAdapter().Add(null)); 21 | } 22 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/BuilderKeyedSpecificationTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.DependencyInjection.Specification; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Test; 8 | 9 | public class BuilderKeyedSpecificationTests : KeyedDependencyInjectionSpecificationTests 10 | { 11 | public override bool SupportsIServiceProviderIsKeyedService => true; 12 | 13 | protected override IServiceProvider CreateServiceProvider(IServiceCollection collection) 14 | { 15 | var builder = new ContainerBuilder(); 16 | builder.Populate(collection); 17 | var container = builder.Build(); 18 | return container.Resolve(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /bench/projects/Bench.AutofacApiServer/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:55009", 7 | "sslPort": 0 8 | }, 9 | "windowsAuthentication": false 10 | }, 11 | "profiles": { 12 | "Bench.AutofacApiServer": { 13 | "applicationUrl": "http://localhost:5000", 14 | "commandName": "Project", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | }, 18 | "launchBrowser": true, 19 | "launchUrl": "weatherforecast" 20 | }, 21 | "IIS Express": { 22 | "commandName": "IISExpress", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "launchBrowser": true, 27 | "launchUrl": "weatherforecast" 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /bench/Autofac.Extensions.DependencyInjection.Bench/Harness.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using BenchmarkDotNet.Running; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Bench; 7 | 8 | public class Harness 9 | { 10 | [Fact] 11 | public void Request() => RunBenchmark(); 12 | 13 | /// 14 | /// This method is used to enforce that benchmark types are added to 15 | /// so that they can be used directly from the command line in as well. 16 | /// 17 | private static void RunBenchmark() 18 | { 19 | var targetType = typeof(TBenchmark); 20 | var benchmarkType = Benchmarks.All.Single(type => type == targetType); 21 | BenchmarkRunner.Run(benchmarkType, new BenchmarkConfig()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /test/Integration.Net6/Startup.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Diagnostics.CodeAnalysis; 5 | using Autofac; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.Extensions.DependencyInjection; 8 | 9 | namespace Integration.Net6; 10 | 11 | [SuppressMessage("CA1052", "CA1052", Justification = "Startup must not be a static class.")] 12 | public class Startup 13 | { 14 | public static void Configure(IApplicationBuilder app) 15 | { 16 | app.UseRouting() 17 | .UseEndpoints(endpoints => endpoints.MapControllers()); 18 | } 19 | 20 | public static void ConfigureServices(IServiceCollection services) 21 | { 22 | services.AddMvc(); 23 | } 24 | 25 | public static void ConfigureContainer(ContainerBuilder builder) 26 | { 27 | builder.RegisterType().As(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/Integration.Net7/Startup.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Diagnostics.CodeAnalysis; 5 | using Autofac; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.Extensions.DependencyInjection; 8 | 9 | namespace Integration.Net7; 10 | 11 | [SuppressMessage("CA1052", "CA1052", Justification = "Startup must not be a static class.")] 12 | public class Startup 13 | { 14 | public static void Configure(IApplicationBuilder app) 15 | { 16 | app.UseRouting() 17 | .UseEndpoints(endpoints => endpoints.MapControllers()); 18 | } 19 | 20 | public static void ConfigureServices(IServiceCollection services) 21 | { 22 | services.AddMvc(); 23 | } 24 | 25 | public static void ConfigureContainer(ContainerBuilder builder) 26 | { 27 | builder.RegisterType().As(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/Integration.Net8/Startup.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Diagnostics.CodeAnalysis; 5 | using Autofac; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.Extensions.DependencyInjection; 8 | 9 | namespace Integration.Net8; 10 | 11 | [SuppressMessage("CA1052", "CA1052", Justification = "Startup must not be a static class.")] 12 | public class Startup 13 | { 14 | public static void Configure(IApplicationBuilder app) 15 | { 16 | app.UseRouting() 17 | .UseEndpoints(endpoints => endpoints.MapControllers()); 18 | } 19 | 20 | public static void ConfigureServices(IServiceCollection services) 21 | { 22 | services.AddMvc(); 23 | } 24 | 25 | public static void ConfigureContainer(ContainerBuilder builder) 26 | { 27 | builder.RegisterType().As(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /bench/projects/Bench.AutofacApiServer/Services.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | // Disable the "file may only contain one type" warnings for these tiny 5 | // logic-free stubs. If they grow to hold logic, we should split them up. 6 | #pragma warning disable SA1402, SA1649 7 | 8 | namespace BenchProject.AutofacApiServer; 9 | 10 | public class A 11 | { 12 | public A(B1 b1, B2 b2) 13 | { 14 | } 15 | } 16 | 17 | public class B1 18 | { 19 | public B1(B2 b2, C1 c1, C2 c2) 20 | { 21 | } 22 | } 23 | 24 | public class B2 25 | { 26 | public B2(C1 c1, C2 c2) 27 | { 28 | } 29 | } 30 | 31 | public class C1 32 | { 33 | public C1(C2 c2, D1 d1, D2 d2) 34 | { 35 | } 36 | } 37 | 38 | public class C2 39 | { 40 | public C2(D1 d1, D2 d2) 41 | { 42 | } 43 | } 44 | 45 | public class D1 46 | { 47 | } 48 | 49 | public class D2 50 | { 51 | } 52 | 53 | #pragma warning restore SA1402, SA1649 54 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/ChildScopeFactorySpecificationTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.DependencyInjection.Specification; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Test.Specification; 8 | 9 | public class ChildScopeFactorySpecificationTests : DependencyInjectionSpecificationTests 10 | { 11 | public override bool SupportsIServiceProviderIsService => true; 12 | 13 | protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 14 | { 15 | var container = new ContainerBuilder().Build(); 16 | var rootScope = container.BeginLifetimeScope(); 17 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(() => rootScope); 18 | return factory.CreateServiceProvider(factory.CreateBuilder(serviceCollection)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/ChildScopeFactoryKeyedSpecificationTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.DependencyInjection.Specification; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Test.Specification; 8 | 9 | public class ChildScopeFactoryKeyedSpecificationTests : KeyedDependencyInjectionSpecificationTests 10 | { 11 | public override bool SupportsIServiceProviderIsKeyedService => true; 12 | 13 | protected override IServiceProvider CreateServiceProvider(IServiceCollection collection) 14 | { 15 | var container = new ContainerBuilder().Build(); 16 | var rootScope = container.BeginLifetimeScope(); 17 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(() => rootScope); 18 | return factory.CreateServiceProvider(factory.CreateBuilder(collection)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.doc diff=astextplain 2 | *.DOC diff=astextplain 3 | *.docx diff=astextplain 4 | *.DOCX diff=astextplain 5 | *.dot diff=astextplain 6 | *.DOT diff=astextplain 7 | *.pdf diff=astextplain 8 | *.PDF diff=astextplain 9 | *.rtf diff=astextplain 10 | *.RTF diff=astextplain 11 | 12 | *.jpg binary 13 | *.png binary 14 | *.gif binary 15 | 16 | *.cs text=auto diff=csharp 17 | *.vb text=auto 18 | *.resx text=auto 19 | *.c text=auto 20 | *.cpp text=auto 21 | *.cxx text=auto 22 | *.h text=auto 23 | *.hxx text=auto 24 | *.py text=auto 25 | *.rb text=auto 26 | *.java text=auto 27 | *.html text=auto 28 | *.htm text=auto 29 | *.css text=auto 30 | *.scss text=auto 31 | *.sass text=auto 32 | *.less text=auto 33 | *.js text=auto 34 | *.lisp text=auto 35 | *.clj text=auto 36 | *.sql text=auto 37 | *.php text=auto 38 | *.lua text=auto 39 | *.m text=auto 40 | *.asm text=auto 41 | *.erl text=auto 42 | *.fs text=auto 43 | *.fsx text=auto 44 | *.hs text=auto 45 | 46 | *.csproj text=auto 47 | *.vbproj text=auto 48 | *.fsproj text=auto 49 | *.dbproj text=auto 50 | *.sln text=auto eol=crlf 51 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/ServiceProviderExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Test; 7 | 8 | public sealed class ServiceProviderExtensionsTests 9 | { 10 | [Fact] 11 | public void GetAutofacRootReturnsLifetimeScope() 12 | { 13 | var containerBuilder = new ContainerBuilder(); 14 | containerBuilder.Populate(new ServiceCollection()); 15 | 16 | var container = containerBuilder.Build(); 17 | var serviceProvider = container.Resolve(); 18 | 19 | Assert.NotNull(serviceProvider.GetAutofacRoot()); 20 | } 21 | 22 | [Fact] 23 | public void GetAutofacRootServiceProviderNotAutofacServiceProviderThrows() 24 | => Assert.Throws(() => 25 | new ServiceCollection().BuildServiceProvider().GetAutofacRoot()); 26 | } 27 | -------------------------------------------------------------------------------- /bench/Autofac.Extensions.DependencyInjection.Bench/BenchmarkConfig.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using BenchmarkDotNet.Configs; 5 | using BenchmarkDotNet.Diagnosers; 6 | using BenchmarkDotNet.Jobs; 7 | 8 | namespace Autofac.Extensions.DependencyInjection.Bench; 9 | 10 | internal sealed class BenchmarkConfig : ManualConfig 11 | { 12 | private const string BenchmarkArtifactsFolder = "BenchmarkDotNet.Artifacts"; 13 | 14 | internal BenchmarkConfig() 15 | { 16 | Add(DefaultConfig.Instance); 17 | 18 | AddJob(Job.InProcess); 19 | 20 | var rootFolder = AppContext.BaseDirectory.Substring(0, AppContext.BaseDirectory.LastIndexOf("bin", StringComparison.OrdinalIgnoreCase)); 21 | var runFolder = DateTime.Now.ToString("u").Replace(' ', '_').Replace(':', '-'); 22 | ArtifactsPath = Path.Combine(rootFolder, BenchmarkArtifactsFolder, runFolder); 23 | 24 | AddDiagnoser(MemoryDiagnoser.Default); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /bench/projects/Bench.AutofacApiServer/BenchProject.AutofacApiServer.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net8.0 4 | Exe 5 | net6.0 6 | $(NoWarn);CS1591 7 | true 8 | latest 9 | ../../../build/Test.ruleset 10 | latest 11 | AllEnabledByDefault 12 | enable 13 | 14 | 15 | 16 | all 17 | runtime; build; native; contentfiles; analyzers; buildtransitive 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build and Test 2 | on: 3 | workflow_call: 4 | secrets: 5 | CODECOV_TOKEN: 6 | description: Token for uploading code coverage metrics to CodeCov.io. 7 | required: true 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout 13 | uses: actions/checkout@v4 14 | - name: Setup .NET 15 | uses: actions/setup-dotnet@v4 16 | with: 17 | dotnet-version: | 18 | 6.0.x 19 | 7.0.x 20 | 8.0.x 21 | - name: Build and test 22 | run: dotnet msbuild ./default.proj 23 | - name: Upload coverage 24 | uses: codecov/codecov-action@v5 25 | with: 26 | fail_ci_if_error: true 27 | files: artifacts/logs/*/coverage.cobertura.xml 28 | token: ${{ secrets.CODECOV_TOKEN }} 29 | - name: Upload package artifacts 30 | uses: actions/upload-artifact@v4 31 | with: 32 | name: packages 33 | path: | 34 | artifacts/packages/*.nupkg 35 | artifacts/packages/*.snupkg 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2014 Autofac Project 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Integration.Test/IntegrationTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Mvc.Testing; 7 | using Xunit; 8 | 9 | #if NET8_0 10 | using Integration.Net8; 11 | #endif 12 | #if NET7_0 13 | using Integration.Net7; 14 | #endif 15 | #if NET6_0 16 | using Integration.Net6; 17 | #endif 18 | 19 | namespace Autofac.Extensions.DependencyInjection.Integration.Test; 20 | 21 | public class IntegrationTests : IClassFixture> 22 | { 23 | public IntegrationTests(WebApplicationFactory appFactory) 24 | { 25 | AppFactory = appFactory; 26 | } 27 | 28 | public WebApplicationFactory AppFactory { get; } 29 | 30 | [Fact] 31 | public async Task GetDate() 32 | { 33 | var client = AppFactory.CreateClient(); 34 | var response = await client.GetAsync(new Uri("/Date", UriKind.Relative)); 35 | response.EnsureSuccessStatusCode(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Continuous Integration 2 | on: 3 | pull_request: 4 | branches: 5 | - develop 6 | - master 7 | push: 8 | branches: 9 | - develop 10 | - master 11 | - feature/* 12 | tags: 13 | - v[0-9]+.[0-9]+.[0-9]+ 14 | # If multiple pushes happen quickly in succession, cancel the running build and 15 | # start a new one. 16 | concurrency: 17 | group: ${{ github.workflow }}-${{ github.ref }} 18 | cancel-in-progress: true 19 | jobs: 20 | # Linting 21 | dotnet-format: 22 | uses: ./.github/workflows/dotnet-format.yml 23 | pre-commit: 24 | uses: ./.github/workflows/pre-commit.yml 25 | # Build and test 26 | build: 27 | uses: ./.github/workflows/build.yml 28 | secrets: 29 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} 30 | # Publish beta and release packages. 31 | publish: 32 | uses: ./.github/workflows/publish.yml 33 | needs: 34 | - build 35 | - dotnet-format 36 | - pre-commit 37 | if: ${{ github.ref == 'refs/heads/develop' || startsWith(github.ref, 'refs/tags/v') }} 38 | secrets: 39 | NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} 40 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "cSpell.words": [ 3 | "autofac", 4 | "cref", 5 | "diagnoser", 6 | "diagnosers", 7 | "inheritdoc", 8 | "langword", 9 | "paramref", 10 | "seealso", 11 | "typeparam", 12 | "unmanaged", 13 | "xunit" 14 | ], 15 | "coverage-gutters.coverageBaseDir": "artifacts/logs", 16 | "coverage-gutters.coverageFileNames": [ 17 | "**/coverage.cobertura.xml" 18 | ], 19 | "dotnet.defaultSolution": "Autofac.Extensions.DependencyInjection.sln", 20 | "dotnet.preferRuntimeFromSDK": true, 21 | "dotnet.unitTestDebuggingOptions": { 22 | "enableStepFiltering": false, 23 | "justMyCode": false, 24 | "requireExactSource": false, 25 | "sourceLinkOptions": { 26 | "*": { 27 | "enabled": true 28 | } 29 | }, 30 | "suppressJITOptimizations": true, 31 | "symbolOptions": { 32 | "searchNuGetOrgSymbolServer": true 33 | } 34 | }, 35 | "explorer.fileNesting.enabled": true, 36 | "explorer.fileNesting.patterns": { 37 | "*.resx": "$(capture).*.resx, $(capture).designer.cs, $(capture).designer.vb" 38 | }, 39 | "files.watcherExclude": { 40 | "**/target": true 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/TestCulture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Globalization; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Test; 7 | 8 | public static class TestCulture 9 | { 10 | public static void With(CultureInfo culture, Action test) 11 | { 12 | var originalCulture = Thread.CurrentThread.CurrentCulture; 13 | var originalUICulture = Thread.CurrentThread.CurrentUICulture; 14 | Thread.CurrentThread.CurrentCulture = culture; 15 | Thread.CurrentThread.CurrentUICulture = culture; 16 | CultureInfo.CurrentCulture.ClearCachedData(); 17 | CultureInfo.CurrentUICulture.ClearCachedData(); 18 | try 19 | { 20 | test?.Invoke(); 21 | } 22 | finally 23 | { 24 | Thread.CurrentThread.CurrentCulture = originalCulture; 25 | Thread.CurrentThread.CurrentUICulture = originalUICulture; 26 | CultureInfo.CurrentCulture.ClearCachedData(); 27 | CultureInfo.CurrentUICulture.ClearCachedData(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /test/Integration.Net6/Integration.Net6.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net6.0 4 | $(NoWarn);CS1591 5 | true 6 | ../../Autofac.snk 7 | true 8 | enable 9 | ../../build/Test.ruleset 10 | AllEnabledByDefault 11 | true 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | all 22 | runtime; build; native; contentfiles; analyzers; buildtransitive 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /test/Integration.Net7/Integration.Net7.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net7.0 4 | $(NoWarn);CS1591 5 | true 6 | ../../Autofac.snk 7 | true 8 | enable 9 | ../../build/Test.ruleset 10 | AllEnabledByDefault 11 | true 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | all 22 | runtime; build; native; contentfiles; analyzers; buildtransitive 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /test/Integration.Net8/Integration.Net8.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net8.0 4 | $(NoWarn);CS1591 5 | true 6 | ../../Autofac.snk 7 | true 8 | enable 9 | ../../build/Test.ruleset 10 | AllEnabledByDefault 11 | true 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | all 22 | runtime; build; native; contentfiles; analyzers; buildtransitive 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | on: 3 | workflow_call: 4 | secrets: 5 | NUGET_API_KEY: 6 | description: Token for publishing packages to NuGet. 7 | required: true 8 | jobs: 9 | publish: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Setup .NET 13 | uses: actions/setup-dotnet@v4 14 | with: 15 | dotnet-version: 8.0.x 16 | - name: Download package artifacts 17 | uses: actions/download-artifact@v4 18 | with: 19 | name: packages 20 | path: artifacts/packages 21 | - name: Publish to GitHub Packages 22 | run: | 23 | dotnet nuget add source --username USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/autofac/index.json" 24 | dotnet nuget push ./artifacts/packages/*.nupkg --skip-duplicate --source github --api-key ${{ secrets.GITHUB_TOKEN }} 25 | dotnet nuget push ./artifacts/packages/*.snupkg --skip-duplicate --source github --api-key ${{ secrets.GITHUB_TOKEN }} 26 | - name: Publish to NuGet 27 | if: ${{ startsWith(github.ref, 'refs/tags/v') }} 28 | run: | 29 | dotnet nuget push ./artifacts/packages/*.nupkg --skip-duplicate --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }} 30 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/ServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection; 7 | 8 | /// 9 | /// Extension methods on to register the . 10 | /// 11 | public static class ServiceCollectionExtensions 12 | { 13 | /// 14 | /// Adds the to the service collection. ONLY FOR PRE-ASP.NET 3.0 HOSTING. THIS WON'T WORK 15 | /// FOR ASP.NET CORE 3.0+ OR GENERIC HOSTING. 16 | /// 17 | /// The service collection to add the factory to. 18 | /// Action on a that adds component registrations to the container. 19 | /// The service collection. 20 | public static IServiceCollection AddAutofac(this IServiceCollection services, Action? configurationAction = null) 21 | { 22 | return services.AddSingleton>(new AutofacServiceProviderFactory(configurationAction)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /bench/projects/Bench.AutofacApiServer/DefaultStartup.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace BenchProject.AutofacApiServer; 5 | 6 | public class DefaultStartup 7 | { 8 | public DefaultStartup(IConfiguration configuration) 9 | { 10 | Configuration = configuration; 11 | } 12 | 13 | public IConfiguration Configuration { get; } 14 | 15 | // This method gets called by the runtime. Use this method to add services to the container. 16 | public void ConfigureServices(IServiceCollection services) 17 | { 18 | services.AddControllers(); 19 | 20 | services.AddTransient(); 21 | services.AddTransient(); 22 | services.AddTransient(); 23 | services.AddTransient(); 24 | services.AddTransient(); 25 | services.AddTransient(); 26 | services.AddTransient(); 27 | } 28 | 29 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 30 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 31 | { 32 | if (env.IsDevelopment()) 33 | { 34 | app.UseDeveloperExceptionPage(); 35 | } 36 | 37 | app.UseRouting(); 38 | 39 | app.UseAuthorization(); 40 | 41 | app.UseEndpoints(endpoints => 42 | { 43 | endpoints.MapControllers(); 44 | }); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/ServiceProviderExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Globalization; 5 | 6 | namespace Autofac.Extensions.DependencyInjection; 7 | 8 | /// 9 | /// Extension methods for use with the . 10 | /// 11 | public static class ServiceProviderExtensions 12 | { 13 | /// 14 | /// Tries to cast the instance of from when possible. 15 | /// 16 | /// The instance of . 17 | /// Throws an when instance of can't be assigned to . 18 | /// Returns the instance of exposed by . 19 | public static ILifetimeScope GetAutofacRoot(this IServiceProvider serviceProvider) 20 | { 21 | if (serviceProvider is not AutofacServiceProvider autofacServiceProvider) 22 | { 23 | throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, ServiceProviderExtensionsResources.WrongProviderType, serviceProvider?.GetType())); 24 | } 25 | 26 | return autofacServiceProvider.LifetimeScope; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/AutofacChildLifetimeScopeConfigurationAdapter.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace Autofac.Extensions.DependencyInjection; 5 | 6 | /// 7 | /// Configuration adapter for . 8 | /// 9 | public class AutofacChildLifetimeScopeConfigurationAdapter 10 | { 11 | private readonly List> _configurationActions = new(); 12 | 13 | /// 14 | /// Gets the list of configuration actions to be executed on the for the child . 15 | /// 16 | public IReadOnlyList> ConfigurationActions => _configurationActions; 17 | 18 | /// 19 | /// Adds a configuration action that will be executed when the child is created. 20 | /// 21 | /// Action on a that adds component registrations to the container. 22 | /// Throws when the passed configuration-action is null. 23 | public void Add(Action configurationAction) 24 | { 25 | if (configurationAction == null) 26 | { 27 | throw new ArgumentNullException(nameof(configurationAction)); 28 | } 29 | 30 | _configurationActions.Add(configurationAction); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/ServiceDescriptorExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection; 7 | 8 | /// 9 | /// Extensions for working with . 10 | /// 11 | internal static class ServiceDescriptorExtensions 12 | { 13 | /// 14 | /// Normalizes the implementation instance data between keyed and not keyed services. 15 | /// 16 | /// 17 | /// The to normalize. 18 | /// 19 | /// 20 | /// The appropriate implementation instance from the service descriptor. 21 | /// 22 | public static object? NormalizedImplementationInstance(this ServiceDescriptor descriptor) => descriptor.IsKeyedService ? descriptor.KeyedImplementationInstance : descriptor.ImplementationInstance; 23 | 24 | /// 25 | /// Normalizes the implementation type data between keyed and not keyed services. 26 | /// 27 | /// 28 | /// The to normalize. 29 | /// 30 | /// 31 | /// The appropriate implementation type from the service descriptor. 32 | /// 33 | public static Type? NormalizedImplementationType(this ServiceDescriptor descriptor) => descriptor.IsKeyedService ? descriptor.KeyedImplementationType : descriptor.ImplementationType; 34 | } 35 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/AutofacServiceScopeFactory.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection; 7 | 8 | /// 9 | /// Autofac implementation of the ASP.NET Core . 10 | /// 11 | /// 12 | internal class AutofacServiceScopeFactory : IServiceScopeFactory 13 | { 14 | private readonly ILifetimeScope _lifetimeScope; 15 | 16 | /// 17 | /// Initializes a new instance of the class. 18 | /// 19 | /// The lifetime scope. 20 | public AutofacServiceScopeFactory(ILifetimeScope lifetimeScope) 21 | { 22 | _lifetimeScope = lifetimeScope; 23 | } 24 | 25 | /// 26 | /// Creates an which contains an 27 | /// used to resolve dependencies within 28 | /// the scope. 29 | /// 30 | /// 31 | /// An controlling the lifetime of the scope. Once 32 | /// this is disposed, any scoped services that have been resolved 33 | /// from the 34 | /// will also be disposed. 35 | /// 36 | public IServiceScope CreateScope() 37 | { 38 | return new AutofacServiceScope(_lifetimeScope.BeginLifetimeScope()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/ServiceCollectionExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Test; 7 | 8 | public sealed class ServiceCollectionExtensionsTests 9 | { 10 | [Fact] 11 | public void AddAutofacReturnsProvidedServiceCollection() 12 | { 13 | var collection = new ServiceCollection(); 14 | 15 | var returnedCollection = collection.AddAutofac(); 16 | 17 | Assert.Same(collection, returnedCollection); 18 | } 19 | 20 | [Fact] 21 | public void AddAutofacAddsAutofacServiceProviderFactoryToCollection() 22 | { 23 | var collection = new ServiceCollection(); 24 | 25 | collection.AddAutofac(); 26 | 27 | var service = collection.FirstOrDefault(s => s.ServiceType == typeof(IServiceProviderFactory)); 28 | Assert.NotNull(service); 29 | Assert.Equal(ServiceLifetime.Singleton, service.Lifetime); 30 | } 31 | 32 | [Fact] 33 | public void AddAutofacPassesConfigurationActionToAutofacServiceProviderFactory() 34 | { 35 | var collection = new ServiceCollection(); 36 | 37 | collection.AddAutofac(config => config.Register(c => "Foo")); 38 | 39 | var serviceProvider = collection.BuildServiceProvider(); 40 | var factory = (IServiceProviderFactory)serviceProvider.GetService(typeof(IServiceProviderFactory)); 41 | var builder = factory.CreateBuilder(collection); 42 | Assert.Equal("Foo", builder.Build().Resolve()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Assertions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Autofac.Core; 5 | 6 | namespace Autofac.Extensions.DependencyInjection.Test; 7 | 8 | internal static class Assertions 9 | { 10 | public static void AssertRegistered(this IComponentContext context) 11 | { 12 | Assert.True(context.IsRegistered()); 13 | } 14 | 15 | public static void AssertNotRegistered(this IComponentContext context) 16 | { 17 | Assert.False(context.IsRegistered()); 18 | } 19 | 20 | public static void AssertImplementation(this IComponentContext context) 21 | { 22 | var service = context.Resolve(); 23 | Assert.IsAssignableFrom(service); 24 | } 25 | 26 | public static void AssertSharing(this IComponentContext context, InstanceSharing sharing) 27 | { 28 | var cr = context.RegistrationFor(); 29 | Assert.Equal(sharing, cr.Sharing); 30 | } 31 | 32 | public static void AssertLifetime(this IComponentContext context) 33 | { 34 | var cr = context.RegistrationFor(); 35 | Assert.IsType(cr.Lifetime); 36 | } 37 | 38 | public static void AssertOwnership(this IComponentContext context, InstanceOwnership ownership) 39 | { 40 | var cr = context.RegistrationFor(); 41 | Assert.Equal(ownership, cr.Ownership); 42 | } 43 | 44 | public static IComponentRegistration RegistrationFor(this IComponentContext context) 45 | { 46 | Assert.True(context.ComponentRegistry.TryGetRegistration(new TypedService(typeof(TService)), out IComponentRegistration r)); 47 | return r; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "args": [ 5 | ], 6 | "cwd": "${workspaceFolder}/test/Integration.Net6", 7 | "env": { 8 | "ASPNETCORE_ENVIRONMENT": "Development" 9 | }, 10 | "name": "Launch .NET 6 Integration Site", 11 | "preLaunchTask": "build", 12 | "program": "${workspaceFolder}/test/Integration.Net6/bin/Debug/net6.0/Integration.Net6.dll", 13 | "request": "launch", 14 | "serverReadyAction": { 15 | "action": "openExternally", 16 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)", 17 | "uriFormat": "%s/date" 18 | }, 19 | "stopAtEntry": false, 20 | "type": "coreclr" 21 | }, 22 | { 23 | "args": [ 24 | ], 25 | "cwd": "${workspaceFolder}/test/Integration.Net7", 26 | "env": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | }, 29 | "name": "Launch .NET 7 Integration Site", 30 | "preLaunchTask": "build", 31 | "program": "${workspaceFolder}/test/Integration.Net7/bin/Debug/net7.0/Integration.Net7.dll", 32 | "request": "launch", 33 | "serverReadyAction": { 34 | "action": "openExternally", 35 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)", 36 | "uriFormat": "%s/date" 37 | }, 38 | "stopAtEntry": false, 39 | "type": "coreclr" 40 | }, 41 | { 42 | "args": [ 43 | ], 44 | "cwd": "${workspaceFolder}/test/Integration.Net8", 45 | "env": { 46 | "ASPNETCORE_ENVIRONMENT": "Development" 47 | }, 48 | "name": "Launch .NET 8 Integration Site", 49 | "preLaunchTask": "build", 50 | "program": "${workspaceFolder}/test/Integration.Net8/bin/Debug/net8.0/Integration.Net8.dll", 51 | "request": "launch", 52 | "serverReadyAction": { 53 | "action": "openExternally", 54 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)", 55 | "uriFormat": "%s/date" 56 | }, 57 | "stopAtEntry": false, 58 | "type": "coreclr" 59 | } 60 | ], 61 | "version": "0.2.0" 62 | } 63 | -------------------------------------------------------------------------------- /bench/Autofac.Extensions.DependencyInjection.Bench/RequestBenchmark.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Diagnostics.CodeAnalysis; 5 | using System.Net; 6 | using BenchProject.AutofacApiServer; 7 | using Microsoft.AspNetCore.Mvc.Testing; 8 | 9 | namespace Autofac.Extensions.DependencyInjection.Bench; 10 | 11 | [SuppressMessage("CA1001", "CA1001", Justification = "Benchmark disposal happens in a global cleanup method.")] 12 | public class RequestBenchmark 13 | { 14 | private static readonly Uri ValuesUri = new("/api/values", UriKind.Relative); 15 | private WebApplicationFactory _defaultFactory; 16 | private WebApplicationFactory _autofacFactory; 17 | private HttpClient _defaultClient; 18 | private HttpClient _autofacClient; 19 | 20 | [GlobalSetup] 21 | public void Setup() 22 | { 23 | _defaultFactory = new WebApplicationFactory(); 24 | _autofacFactory = new AutofacWebApplicationFactory(); 25 | 26 | _defaultClient = _defaultFactory.CreateClient(); 27 | _autofacClient = _autofacFactory.CreateClient(); 28 | } 29 | 30 | [Benchmark(Baseline = true)] 31 | public async Task RequestDefaultDI() 32 | { 33 | var response = await _defaultClient.GetAsync(ValuesUri).ConfigureAwait(false); 34 | 35 | if (response.StatusCode != HttpStatusCode.OK) 36 | { 37 | throw new HttpRequestException(); 38 | } 39 | } 40 | 41 | [Benchmark] 42 | public async Task RequestAutofacDI() 43 | { 44 | var response = await _autofacClient.GetAsync(ValuesUri).ConfigureAwait(false); 45 | 46 | if (response.StatusCode != HttpStatusCode.OK) 47 | { 48 | throw new HttpRequestException(); 49 | } 50 | } 51 | 52 | [GlobalCleanup] 53 | public void Cleanup() 54 | { 55 | _defaultFactory.Dispose(); 56 | _autofacFactory.Dispose(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "linux": { 3 | "options": { 4 | "shell": { 5 | "args": [ 6 | "-NoProfile", 7 | "-Command" 8 | ], 9 | "executable": "pwsh" 10 | } 11 | } 12 | }, 13 | "osx": { 14 | "options": { 15 | "shell": { 16 | "args": [ 17 | "-NoProfile", 18 | "-Command" 19 | ], 20 | "executable": "/usr/local/bin/pwsh" 21 | } 22 | } 23 | }, 24 | "tasks": [ 25 | { 26 | "command": "If (Test-Path ${workspaceFolder}/artifacts/logs) { Remove-Item ${workspaceFolder}/artifacts/logs -Recurse -Force }; New-Item -Path ${workspaceFolder}/artifacts/logs -ItemType Directory -Force | Out-Null", 27 | "label": "create log directory", 28 | "type": "shell" 29 | }, 30 | { 31 | "args": [ 32 | "build", 33 | "${workspaceFolder}/Autofac.Extensions.DependencyInjection.sln", 34 | "/property:GenerateFullPaths=true", 35 | "/consoleloggerparameters:NoSummary" 36 | ], 37 | "command": "dotnet", 38 | "group": { 39 | "isDefault": true, 40 | "kind": "build" 41 | }, 42 | "label": "build", 43 | "problemMatcher": "$msCompile", 44 | "type": "shell" 45 | }, 46 | { 47 | "args": [ 48 | "test", 49 | "${workspaceFolder}/Autofac.Extensions.DependencyInjection.sln", 50 | "/property:GenerateFullPaths=true", 51 | "/consoleloggerparameters:NoSummary", 52 | "--results-directory", 53 | "\"artifacts/logs\"", 54 | "--logger:trx", 55 | "--collect:\"XPlat Code Coverage\"", 56 | "--settings:build/Coverage.runsettings", 57 | "--filter", 58 | "FullyQualifiedName!~Bench" 59 | ], 60 | "command": "dotnet", 61 | "dependsOn": [ 62 | "create log directory" 63 | ], 64 | "group": { 65 | "isDefault": true, 66 | "kind": "test" 67 | }, 68 | "label": "test", 69 | "problemMatcher": "$msCompile", 70 | "type": "process" 71 | } 72 | ], 73 | "version": "2.0.0", 74 | "windows": { 75 | "options": { 76 | "shell": { 77 | "args": [ 78 | "-NoProfile", 79 | "-ExecutionPolicy", 80 | "Bypass", 81 | "-Command" 82 | ], 83 | "executable": "pwsh.exe" 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Autofac.Extensions.DependencyInjection.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net8.0;net7.0;net6.0 4 | $(NoWarn);CS1591 5 | true 6 | ../../Autofac.snk 7 | true 8 | true 9 | ../../build/Test.ruleset 10 | AllEnabledByDefault 11 | true 12 | false 13 | latest 14 | enable 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | all 28 | runtime; build; native; contentfiles; analyzers; buildtransitive 29 | 30 | 31 | 32 | 33 | 34 | 35 | all 36 | runtime; build; native; contentfiles; analyzers; buildtransitive 37 | 38 | 39 | 40 | all 41 | runtime; build; native; contentfiles; analyzers 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /bench/Autofac.Extensions.DependencyInjection.Bench/Autofac.Extensions.DependencyInjection.Bench.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net8.0 4 | $(NoWarn);CS1591 5 | Exe 6 | false 7 | $([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture) 8 | true 9 | true 10 | true 11 | ../../build/Test.ruleset 12 | AllEnabledByDefault 13 | true 14 | false 15 | enable 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | all 32 | runtime; build; native; contentfiles; analyzers; buildtransitive 33 | 34 | 35 | 36 | all 37 | runtime; build; native; contentfiles; analyzers 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Integration.Test/Autofac.Extensions.DependencyInjection.Integration.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net8.0;net7.0;net6.0 4 | $(NoWarn);CS1591 5 | true 6 | ../../Autofac.snk 7 | true 8 | true 9 | ../../build/Test.ruleset 10 | AllEnabledByDefault 11 | true 12 | false 13 | latest 14 | enable 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | all 39 | runtime; build; native; contentfiles; analyzers; buildtransitive 40 | 41 | 42 | 43 | all 44 | runtime; build; native; contentfiles; analyzers 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/AutofacServiceScope.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection; 7 | 8 | /// 9 | /// Autofac implementation of the ASP.NET Core . 10 | /// 11 | /// 12 | internal class AutofacServiceScope : IServiceScope, IAsyncDisposable 13 | { 14 | private readonly AutofacServiceProvider _serviceProvider; 15 | private bool _disposed; 16 | 17 | /// 18 | /// Initializes a new instance of the class. 19 | /// 20 | /// 21 | /// The lifetime scope from which services should be resolved for this service scope. 22 | /// 23 | public AutofacServiceScope(ILifetimeScope lifetimeScope) 24 | { 25 | _serviceProvider = new AutofacServiceProvider(lifetimeScope); 26 | } 27 | 28 | /// 29 | /// Gets an corresponding to this service scope. 30 | /// 31 | /// 32 | /// An that can be used to resolve dependencies from the scope. 33 | /// 34 | public IServiceProvider ServiceProvider 35 | { 36 | get 37 | { 38 | return _serviceProvider; 39 | } 40 | } 41 | 42 | /// 43 | /// Disposes of the lifetime scope and resolved disposable services. 44 | /// 45 | public void Dispose() 46 | { 47 | Dispose(true); 48 | GC.SuppressFinalize(this); 49 | } 50 | 51 | /// 52 | public async ValueTask DisposeAsync() 53 | { 54 | if (!_disposed) 55 | { 56 | _disposed = true; 57 | await _serviceProvider.DisposeAsync().ConfigureAwait(false); 58 | } 59 | } 60 | 61 | /// 62 | /// Releases unmanaged and - optionally - managed resources. 63 | /// 64 | /// 65 | /// to release both managed and unmanaged resources; to release only unmanaged resources. 66 | /// 67 | protected virtual void Dispose(bool disposing) 68 | { 69 | if (!_disposed) 70 | { 71 | _disposed = true; 72 | 73 | if (disposing) 74 | { 75 | _serviceProvider.Dispose(); 76 | } 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/FromKeyedServicesAttributeExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Reflection; 5 | using Microsoft.Extensions.DependencyInjection; 6 | 7 | namespace Autofac.Extensions.DependencyInjection; 8 | 9 | /// 10 | /// Extensions for working with . 11 | /// 12 | internal static class FromKeyedServicesAttributeExtensions 13 | { 14 | /// 15 | /// Resolves a constructor parameter based on keyed service requirements. 16 | /// 17 | /// The attribute marking the keyed service dependency in a constructor. 18 | /// The specific parameter being resolved that is marked with this attribute. 19 | /// The component context under which the parameter is being resolved. 20 | /// 21 | /// The instance of the object that should be used for the parameter value. 22 | /// 23 | /// 24 | /// Thrown if or is . 25 | /// 26 | public static object? ResolveParameter(this FromKeyedServicesAttribute attribute, ParameterInfo parameter, IComponentContext context) 27 | { 28 | // Adapter for FromKeyedServicesAttribute to work like Autofac.Features.AttributeFilters.KeyFilterAttribute. 29 | context.TryResolveKeyed(attribute.Key, parameter.ParameterType, out var value); 30 | return value; 31 | } 32 | 33 | /// 34 | /// Checks a constructor parameter can be resolved based on keyed service requirements. 35 | /// 36 | /// The attribute marking the keyed service dependency in a constructor. 37 | /// The specific parameter being resolved that is marked with this attribute. 38 | /// The component context under which the parameter is being resolved. 39 | /// true if parameter can be resolved; otherwise, false. 40 | public static bool CanResolveParameter(this FromKeyedServicesAttribute attribute, ParameterInfo parameter, IComponentContext context) 41 | { 42 | // Adapter for FromKeyedServicesAttribute to work like Autofac.Features.AttributeFilters.KeyFilterAttribute. 43 | return context.ComponentRegistry.IsRegistered(new Autofac.Core.KeyedService(attribute.Key, parameter.ParameterType)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/AutofacServiceProviderFactory.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Autofac.Builder; 5 | using Microsoft.Extensions.DependencyInjection; 6 | 7 | namespace Autofac.Extensions.DependencyInjection; 8 | 9 | /// 10 | /// A factory for creating a and an . 11 | /// 12 | public class AutofacServiceProviderFactory : IServiceProviderFactory 13 | { 14 | private readonly Action _configurationAction; 15 | private readonly ContainerBuildOptions _containerBuildOptions = ContainerBuildOptions.None; 16 | 17 | /// 18 | /// Initializes a new instance of the class. 19 | /// 20 | /// The container options to use when building the container. 21 | /// Action on a that adds component registrations to the container. 22 | public AutofacServiceProviderFactory( 23 | ContainerBuildOptions containerBuildOptions, 24 | Action? configurationAction = null) 25 | : this(configurationAction) => 26 | _containerBuildOptions = containerBuildOptions; 27 | 28 | /// 29 | /// Initializes a new instance of the class. 30 | /// 31 | /// Action on a that adds component registrations to the container.. 32 | public AutofacServiceProviderFactory(Action? configurationAction = null) => 33 | _configurationAction = configurationAction ?? (builder => { }); 34 | 35 | /// 36 | /// Creates a container builder from an . 37 | /// 38 | /// The collection of services. 39 | /// A container builder that can be used to create an . 40 | public ContainerBuilder CreateBuilder(IServiceCollection services) 41 | { 42 | var builder = new ContainerBuilder(); 43 | 44 | builder.Populate(services); 45 | 46 | _configurationAction(builder); 47 | 48 | return builder; 49 | } 50 | 51 | /// 52 | /// Creates an from the container builder. 53 | /// 54 | /// The container builder. 55 | /// An . 56 | public IServiceProvider CreateServiceProvider(ContainerBuilder containerBuilder) 57 | { 58 | if (containerBuilder == null) 59 | { 60 | throw new ArgumentNullException(nameof(containerBuilder)); 61 | } 62 | 63 | var container = containerBuilder.Build(_containerBuildOptions); 64 | 65 | return new AutofacServiceProvider(container); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # Project specific files 5 | artifacts/ 6 | BenchmarkDotNet.Artifacts/ 7 | 8 | # User-specific files 9 | *.suo 10 | *.user 11 | *.sln.docstates 12 | *.ide 13 | Index.dat 14 | Storage.dat 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Rr]elease/ 19 | x64/ 20 | [Bb]in/ 21 | [Oo]bj/ 22 | 23 | # Visual Studio 2015 cache/options directory 24 | .dotnet/ 25 | .vs/ 26 | .cr/ 27 | 28 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets 29 | !packages/*/build/ 30 | 31 | # MSTest test Results 32 | [Tt]est[Rr]esult*/ 33 | [Bb]uild[Ll]og.* 34 | *.TestResults.xml 35 | results/ 36 | 37 | *_i.c 38 | *_p.c 39 | *.ilk 40 | *.meta 41 | *.obj 42 | *.pch 43 | *.pdb 44 | *.pgc 45 | *.pgd 46 | *.rsp 47 | *.sbr 48 | *.tlb 49 | *.tli 50 | *.tlh 51 | *.tmp 52 | *.tmp_proj 53 | *.log 54 | *.vspscc 55 | *.vssscc 56 | .builds 57 | *.pidb 58 | *.log 59 | *.scc 60 | 61 | # Visual C++ cache files 62 | ipch/ 63 | *.aps 64 | *.ncb 65 | *.opensdf 66 | *.sdf 67 | *.cachefile 68 | 69 | # Visual Studio profiler 70 | *.psess 71 | *.vsp 72 | *.vspx 73 | 74 | # Guidance Automation Toolkit 75 | *.gpState 76 | 77 | # ReSharper is a .NET coding add-in 78 | _ReSharper*/ 79 | *.[Rr]e[Ss]harper 80 | 81 | # TeamCity is a build add-in 82 | _TeamCity* 83 | 84 | # DotCover is a Code Coverage Tool 85 | *.dotCover 86 | 87 | # NCrunch 88 | *.ncrunch* 89 | .*crunch*.local.xml 90 | 91 | # Installshield output folder 92 | [Ee]xpress/ 93 | 94 | # DocProject is a documentation generator add-in 95 | DocProject/buildhelp/ 96 | DocProject/Help/*.HxT 97 | DocProject/Help/*.HxC 98 | DocProject/Help/*.hhc 99 | DocProject/Help/*.hhk 100 | DocProject/Help/*.hhp 101 | DocProject/Help/Html2 102 | DocProject/Help/html 103 | 104 | # Click-Once directory 105 | publish/ 106 | 107 | # Publish Web Output 108 | *.[Pp]ublish.xml 109 | *.pubxml 110 | 111 | # NuGet Packages Directory 112 | packages/ 113 | 114 | # Windows Azure Build Output 115 | csx 116 | *.build.csdef 117 | 118 | # Windows Store app package directory 119 | AppPackages/ 120 | 121 | # Others 122 | sql/ 123 | *.Cache 124 | ClientBin/ 125 | [Ss]tyle[Cc]op.* 126 | !stylecop.json 127 | ~$* 128 | *~ 129 | *.dbmdl 130 | *.pfx 131 | *.publishsettings 132 | node_modules/ 133 | bower_components/ 134 | wwwroot/ 135 | project.lock.json 136 | *.Designer.cs 137 | 138 | # RIA/Silverlight projects 139 | Generated_Code/ 140 | 141 | # Backup & report files from converting an old project file to a newer 142 | # Visual Studio version. Backup files are not needed, because we have git ;-) 143 | _UpgradeReport_Files/ 144 | Backup*/ 145 | UpgradeLog*.XML 146 | UpgradeLog*.htm 147 | 148 | # SQL Server files 149 | App_Data/*.mdf 150 | App_Data/*.ldf 151 | 152 | # ========================= 153 | # Windows detritus 154 | # ========================= 155 | 156 | # Windows image file caches 157 | Thumbs.db 158 | ehthumbs.db 159 | 160 | # Folder config file 161 | Desktop.ini 162 | 163 | # Recycle Bin used on file shares 164 | $RECYCLE.BIN/ 165 | 166 | # Mac crap 167 | .DS_Store 168 | 169 | # JetBrains Rider 170 | .idea 171 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/AutofacChildLifetimeScopeServiceProviderFactory.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Autofac.Extensions.DependencyInjection; 7 | 8 | /// 9 | /// A factory for creating a that wraps a child created from an existing parent . 10 | /// 11 | public class AutofacChildLifetimeScopeServiceProviderFactory : IServiceProviderFactory 12 | { 13 | private static readonly Action FallbackConfigurationAction = builder => { }; 14 | private readonly Action _containerConfigurationAction; 15 | private readonly ILifetimeScope _rootLifetimeScope; 16 | 17 | /// 18 | /// Initializes a new instance of the class. 19 | /// 20 | /// A function to retrieve the root instance. 21 | /// Action on a that adds component registrations to the container. 22 | public AutofacChildLifetimeScopeServiceProviderFactory(Func rootLifetimeScopeAccessor, Action? configurationAction = null) 23 | { 24 | if (rootLifetimeScopeAccessor == null) 25 | { 26 | throw new ArgumentNullException(nameof(rootLifetimeScopeAccessor)); 27 | } 28 | 29 | _rootLifetimeScope = rootLifetimeScopeAccessor(); 30 | _containerConfigurationAction = configurationAction ?? FallbackConfigurationAction; 31 | } 32 | 33 | /// 34 | /// Initializes a new instance of the class. 35 | /// 36 | /// The root instance. 37 | /// Action on a that adds component registrations to the container. 38 | public AutofacChildLifetimeScopeServiceProviderFactory(ILifetimeScope rootLifetimeScope, Action? configurationAction = null) 39 | { 40 | _rootLifetimeScope = rootLifetimeScope ?? throw new ArgumentNullException(nameof(rootLifetimeScope)); 41 | _containerConfigurationAction = configurationAction ?? FallbackConfigurationAction; 42 | } 43 | 44 | /// 45 | /// Creates a container builder from an . 46 | /// 47 | /// The collection of services. 48 | /// A container builder that can be used to create an . 49 | public AutofacChildLifetimeScopeConfigurationAdapter CreateBuilder(IServiceCollection services) 50 | { 51 | var actions = new AutofacChildLifetimeScopeConfigurationAdapter(); 52 | 53 | actions.Add(builder => builder.Populate(services)); 54 | actions.Add(builder => _containerConfigurationAction(builder)); 55 | 56 | return actions; 57 | } 58 | 59 | /// 60 | /// Creates an from the container builder. 61 | /// 62 | /// The adapter holding configuration applied to creating the . 63 | /// An . 64 | public IServiceProvider CreateServiceProvider(AutofacChildLifetimeScopeConfigurationAdapter containerBuilder) 65 | { 66 | if (containerBuilder == null) 67 | { 68 | throw new ArgumentNullException(nameof(containerBuilder)); 69 | } 70 | 71 | var scope = _rootLifetimeScope.BeginLifetimeScope(scopeBuilder => 72 | { 73 | foreach (var action in containerBuilder.ConfigurationActions) 74 | { 75 | action(scopeBuilder); 76 | } 77 | }); 78 | 79 | return new AutofacServiceProvider(scope); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/KeyedServiceMiddleware.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Reflection; 5 | using Autofac.Core; 6 | using Autofac.Core.Resolving.Pipeline; 7 | using Microsoft.Extensions.DependencyInjection; 8 | 9 | namespace Autofac.Extensions.DependencyInjection; 10 | 11 | /// 12 | /// Middleware that supports keyed service compatibility. 13 | /// 14 | internal class KeyedServiceMiddleware : IResolveMiddleware 15 | { 16 | // [FromKeyedServices("key")] - Specifies a keyed service 17 | // for injection into a constructor. This is similar to the 18 | // Autofac [KeyFilter] attribute. 19 | private static readonly Parameter FromKeyedServicesParameter = new ResolvedParameter( 20 | (p, c) => 21 | { 22 | var filter = p.GetCustomAttributes(true).FirstOrDefault(); 23 | return filter is not null && filter.CanResolveParameter(p, c); 24 | }, 25 | (p, c) => 26 | { 27 | var filter = p.GetCustomAttributes(true).First(); 28 | return filter.ResolveParameter(p, c); 29 | }); 30 | 31 | private readonly bool _addFromKeyedServiceParameter; 32 | 33 | /// 34 | /// Initializes a new instance of the class. 35 | /// 36 | /// Whether or not the from-keyed-service parameter should be added. 37 | public KeyedServiceMiddleware(bool addFromKeyedServiceParameter) 38 | { 39 | _addFromKeyedServiceParameter = addFromKeyedServiceParameter; 40 | } 41 | 42 | /// 43 | /// Gets a single instance of this middleware that does not add the keyed services parameter. 44 | /// 45 | public static KeyedServiceMiddleware InstanceWithoutFromKeyedServicesParameter { get; } = new(addFromKeyedServiceParameter: false); 46 | 47 | /// 48 | /// Gets a single instance of this middleware that adds the keyed services parameter. 49 | /// 50 | public static KeyedServiceMiddleware InstanceWithFromKeyedServicesParameter { get; } = new(addFromKeyedServiceParameter: true); 51 | 52 | /// 53 | public PipelinePhase Phase => PipelinePhase.Activation; 54 | 55 | /// 56 | public void Execute(ResolveRequestContext context, Action next) 57 | { 58 | List? newParameters = null; 59 | 60 | if (context.Service is Autofac.Core.KeyedService keyedService) 61 | { 62 | newParameters = new List(context.Parameters); 63 | 64 | var key = keyedService.ServiceKey; 65 | 66 | // [ServiceKey] - indicates that the parameter value should 67 | // be the service key used during resolution. 68 | newParameters.Add(new ResolvedParameter( 69 | (p, c) => 70 | { 71 | return p.GetCustomAttributes(true).FirstOrDefault() is not null; 72 | }, 73 | (p, c) => 74 | { 75 | // If the key is an object but the constructor takes 76 | // a string, we need to safely convert that. This is 77 | // particularly interesting in the AnyKey scenario. 78 | return KeyTypeManipulation.ChangeToCompatibleType(key, p.ParameterType, p); 79 | })); 80 | } 81 | 82 | if (_addFromKeyedServiceParameter) 83 | { 84 | newParameters ??= new List(context.Parameters); 85 | 86 | // [FromKeyedServices("key")] - Specifies a keyed service 87 | // for injection into a constructor. This is similar to the 88 | // Autofac [KeyFilter] attribute. 89 | newParameters.Add(FromKeyedServicesParameter); 90 | } 91 | 92 | if (newParameters is not null) 93 | { 94 | context.ChangeParameters(newParameters); 95 | } 96 | 97 | next(context); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /default.proj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10.0.1 6 | Autofac.Extensions.DependencyInjection 7 | Release 8 | $([System.IO.Path]::Combine($(MSBuildProjectDirectory),"artifacts")) 9 | $([System.IO.Path]::Combine($(ArtifactDirectory),"packages")) 10 | $([System.IO.Path]::Combine($(ArtifactDirectory),"logs")) 11 | $([System.IO.Path]::Combine($(MSBuildProjectDirectory),'build/Coverage.runsettings')) 12 | $([System.DateTimeOffset]::UtcNow.ToString('yyyyMMddTHHmmssZ')) 13 | 14 | 15 | 16 | 17 | $(Version)-local 18 | 19 | 20 | 21 | 22 | $(Version) 23 | 24 | 25 | 26 | 27 | $(Version)-beta$(BuildDateTime) 28 | 29 | 30 | 31 | 32 | $(Version)-alpha$(BuildDateTime) 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/Autofac.Extensions.DependencyInjection.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Autofac.Extensions.DependencyInjection 5 | Autofac.Extensions.DependencyInjection 6 | Autofac implementation of the interfaces in Microsoft.Extensions.DependencyInjection.Abstractions, the .NET Framework dependency injection abstraction. 7 | Copyright © 2015 Autofac Contributors 8 | Autofac Contributors 9 | Autofac 10 | Autofac 11 | ../../Autofac.snk 12 | true 13 | en-US 14 | 15 | net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0 16 | latest 17 | enable 18 | true 19 | ../../build/Source.ruleset 20 | true 21 | AllEnabledByDefault 22 | enable 23 | 24 | Autofac.Extensions.DependencyInjection 25 | autofac;di;ioc;dependencyinjection;aspnet;aspnetcore 26 | Release notes are at https://github.com/autofac/Autofac.Extensions.DependencyInjection/releases 27 | icon.png 28 | https://autofac.org 29 | MIT 30 | README.md 31 | git 32 | https://github.com/autofac/Autofac.Extensions.DependencyInjection 33 | true 34 | true 35 | true 36 | true 37 | snupkg 38 | 39 | PrepareResources;$(CompileDependsOn) 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | all 55 | 56 | 57 | all 58 | 59 | 60 | 61 | 62 | 63 | MSBuild:Compile 64 | CSharp 65 | $(IntermediateOutputPath)%(Filename).Designer.cs 66 | %(Filename) 67 | 68 | 69 | 70 | 71 | Autofac.Extensions.DependencyInjection 72 | 73 | 74 | Autofac.Extensions.DependencyInjection 75 | 76 | 77 | Autofac.Extensions.DependencyInjection 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/AutofacServiceProviderFactoryTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Autofac.Builder; 5 | using Microsoft.Extensions.DependencyInjection; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Test; 8 | 9 | public class AutofacServiceProviderFactoryTests 10 | { 11 | [Fact] 12 | public void CreateBuilderReturnsNewInstance() 13 | { 14 | var factory = new AutofacServiceProviderFactory(); 15 | 16 | var builder = factory.CreateBuilder(new ServiceCollection()); 17 | 18 | Assert.NotNull(builder); 19 | } 20 | 21 | [Fact] 22 | public void CreateBuilderExecutesConfigurationActionWhenProvided() 23 | { 24 | var factory = new AutofacServiceProviderFactory(config => config.Register(c => "Foo")); 25 | 26 | var builder = factory.CreateBuilder(new ServiceCollection()); 27 | 28 | Assert.Equal("Foo", builder.Build().Resolve()); 29 | } 30 | 31 | [Fact] 32 | public void CreateBuilderAllowsForNullConfigurationAction() 33 | { 34 | var factory = new AutofacServiceProviderFactory(); 35 | 36 | var builder = factory.CreateBuilder(new ServiceCollection()); 37 | 38 | Assert.NotNull(builder); 39 | } 40 | 41 | [Fact] 42 | public void CreateBuilderReturnsInstanceWithServicesPopulated() 43 | { 44 | var factory = new AutofacServiceProviderFactory(); 45 | var services = new ServiceCollection(); 46 | services.AddTransient(); 47 | 48 | var builder = factory.CreateBuilder(services); 49 | 50 | Assert.True(builder.Build().IsRegistered()); 51 | } 52 | 53 | [Fact] 54 | public void CreateServiceProviderBuildsServiceProviderUsingContainerBuilder() 55 | { 56 | var factory = new AutofacServiceProviderFactory(); 57 | var services = new ServiceCollection().AddTransient(); 58 | var builder = factory.CreateBuilder(services); 59 | 60 | var serviceProvider = factory.CreateServiceProvider(builder); 61 | 62 | Assert.NotNull(serviceProvider.GetService(typeof(object))); 63 | } 64 | 65 | [Fact] 66 | public void CreateServiceProviderThrowsWhenProvidedNullContainerBuilder() 67 | { 68 | var factory = new AutofacServiceProviderFactory(); 69 | 70 | var exception = Assert.Throws(() => factory.CreateServiceProvider(null)); 71 | 72 | Assert.Equal("containerBuilder", exception.ParamName); 73 | } 74 | 75 | [Fact] 76 | public void CreateServiceProviderReturnsAutofacServiceProvider() 77 | { 78 | var factory = new AutofacServiceProviderFactory(); 79 | 80 | var serviceProvider = factory.CreateServiceProvider(new ContainerBuilder()); 81 | 82 | Assert.IsType(serviceProvider); 83 | } 84 | 85 | [Fact] 86 | public void CreateServiceProviderUsesDefaultContainerBuildOptionsWhenNotProvided() 87 | { 88 | var factory = new AutofacServiceProviderFactory(); 89 | var services = new ServiceCollection().AddSingleton("Foo"); 90 | var builder = factory.CreateBuilder(services); 91 | 92 | var serviceProvider = factory.CreateServiceProvider(builder); 93 | 94 | Assert.NotNull(serviceProvider.GetService>()); 95 | } 96 | 97 | [Fact] 98 | public void CreateServiceProviderUsesContainerBuildOptionsWhenProvided() 99 | { 100 | var options = ContainerBuildOptions.ExcludeDefaultModules; 101 | var factory = new AutofacServiceProviderFactory(options); 102 | var services = new ServiceCollection().AddSingleton("Foo"); 103 | var builder = factory.CreateBuilder(services); 104 | 105 | var serviceProvider = factory.CreateServiceProvider(builder); 106 | 107 | Assert.Null(serviceProvider.GetService>()); 108 | } 109 | 110 | [Fact] 111 | public void CanProvideContainerBuildOptionsAndConfigurationAction() 112 | { 113 | var factory = new AutofacServiceProviderFactory( 114 | ContainerBuildOptions.ExcludeDefaultModules, 115 | config => config.Register(c => "Foo")); 116 | var builder = factory.CreateBuilder(new ServiceCollection()); 117 | 118 | var serviceProvider = factory.CreateServiceProvider(builder); 119 | 120 | Assert.NotNull(serviceProvider.GetService()); 121 | Assert.Null(serviceProvider.GetService>()); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/TypeManipulationFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.ComponentModel; 5 | using System.Diagnostics.CodeAnalysis; 6 | using System.Globalization; 7 | using System.Net; 8 | 9 | namespace Autofac.Extensions.DependencyInjection.Test; 10 | 11 | public class TypeManipulationFixture 12 | { 13 | [Fact] 14 | public void ChangeToCompatibleTypeLooksForTryParseMethod() 15 | { 16 | var address = "127.0.0.1"; 17 | var value = KeyTypeManipulation.ChangeToCompatibleType(address, typeof(IPAddress)); 18 | Assert.Equal(value, IPAddress.Parse(address)); 19 | } 20 | 21 | [Fact] 22 | public void ChangeToCompatibleTypeUsesTypeConverterOnParameter() 23 | { 24 | var ctor = typeof(HasTypeConverterAttributes).GetConstructor(new Type[] { typeof(Convertible) }); 25 | var member = ctor.GetParameters().First(); 26 | var actual = KeyTypeManipulation.ChangeToCompatibleType("25", typeof(Convertible), member) as Convertible; 27 | Assert.NotNull(actual); 28 | Assert.Equal(25, actual.Value); 29 | } 30 | 31 | [Fact] 32 | public void ChangeToCompatibleTypeUsesTypeConverterOnProperty() 33 | { 34 | var member = typeof(HasTypeConverterAttributes).GetProperty("Property"); 35 | var actual = KeyTypeManipulation.ChangeToCompatibleType("25", typeof(Convertible), member) as Convertible; 36 | Assert.NotNull(actual); 37 | Assert.Equal(25, actual.Value); 38 | } 39 | 40 | [Fact] 41 | public void ChangeToCompatibleTypeNullReferenceType() 42 | { 43 | var actual = KeyTypeManipulation.ChangeToCompatibleType(null, typeof(string)); 44 | Assert.Null(actual); 45 | } 46 | 47 | [Fact] 48 | public void ChangeToCompatibleTypeNullValueType() 49 | { 50 | var actual = KeyTypeManipulation.ChangeToCompatibleType(null, typeof(int)); 51 | Assert.Equal(0, actual); 52 | } 53 | 54 | [Fact] 55 | public void ChangeToCompatibleTypeNoConversionNeeded() 56 | { 57 | var actual = KeyTypeManipulation.ChangeToCompatibleType(15, typeof(int)); 58 | Assert.Equal(15, actual); 59 | } 60 | 61 | [Theory] 62 | [MemberData(nameof(ParsingCultures))] 63 | public void ChangeToCompatibleTypeUsesInvariantCulture(CultureInfo culture) 64 | { 65 | TestCulture.With( 66 | culture, 67 | () => 68 | { 69 | var actual = KeyTypeManipulation.ChangeToCompatibleType("123.456", typeof(double)); 70 | Assert.Equal(123.456, actual); 71 | }); 72 | } 73 | 74 | public static IEnumerable ParsingCultures() 75 | { 76 | yield return new object[] { new CultureInfo("en-US") }; 77 | yield return new object[] { new CultureInfo("es-MX") }; 78 | yield return new object[] { new CultureInfo("it-IT") }; 79 | yield return new object[] { CultureInfo.InvariantCulture }; 80 | } 81 | 82 | [SuppressMessage("CA1812", "CA1812", Justification = "Class instantiated through reflection.")] 83 | private class Convertible 84 | { 85 | public int Value { get; set; } 86 | } 87 | 88 | [SuppressMessage("CA1812", "CA1812", Justification = "Class instantiated through reflection.")] 89 | private class ConvertibleConverter : TypeConverter 90 | { 91 | public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 92 | { 93 | return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); 94 | } 95 | 96 | public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 97 | { 98 | if (value == null) 99 | { 100 | return null; 101 | } 102 | 103 | if (value is not string str) 104 | { 105 | return base.ConvertFrom(context, culture, value); 106 | } 107 | 108 | var converter = TypeDescriptor.GetConverter(typeof(int)); 109 | return new Convertible { Value = (int)converter.ConvertFromString(context, culture, str) }; 110 | } 111 | } 112 | 113 | [SuppressMessage("CA1812", "CA1812", Justification = "Class instantiated through reflection.")] 114 | private class HasTypeConverterAttributes 115 | { 116 | public HasTypeConverterAttributes([TypeConverter(typeof(ConvertibleConverter))] Convertible parameter) 117 | { 118 | Property = parameter; 119 | } 120 | 121 | [TypeConverter(typeof(ConvertibleConverter))] 122 | public Convertible Property { get; set; } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/AnyKeyRegistrationSource.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Numerics; 5 | using Autofac.Core; 6 | using Autofac.Core.Registration; 7 | 8 | namespace Autofac.Extensions.DependencyInjection; 9 | 10 | /// 11 | /// Handles fallback for an individual component registered with . 12 | /// 13 | public class AnyKeyRegistrationSource : IRegistrationSource 14 | { 15 | /// 16 | public bool IsAdapterForIndividualComponents => false; 17 | 18 | /// 19 | /// Provides a registration for a keyed service that's registered with . Allows the registration to respond to any keyed service request, not just one with a specific key. 20 | /// 21 | /// The service that was requested. 22 | /// A function that will return existing registrations for a service. 23 | /// Registrations providing the service. 24 | public IEnumerable RegistrationsFor(Service service, Func> registrationAccessor) 25 | { 26 | if (registrationAccessor == null) 27 | { 28 | throw new ArgumentNullException(nameof(registrationAccessor)); 29 | } 30 | 31 | if (service is not KeyedService keyedService) 32 | { 33 | // It's not a keyed service, bail. If it is, we ALWAYS have to look for an AnyKey service. 34 | return Enumerable.Empty(); 35 | } 36 | 37 | if (keyedService.ServiceKey.Equals(Microsoft.Extensions.DependencyInjection.KeyedService.AnyKey)) 38 | { 39 | // Don't self-generate. 40 | return Enumerable.Empty(); 41 | } 42 | 43 | // Use collection checks borrowed (without caching) from core Autofac. 44 | if (IsCollection(keyedService.ServiceType)) 45 | { 46 | // We don't want to look for an anykey equivalent service when the target of the resolve is an enumerable. 47 | // It will always return something, meaning we'll generate a result collection for 'anykey' instead of for the actual key. 48 | // The normal collection registration source will do the work and call back into this source having stripped the collection type. 49 | return Enumerable.Empty(); 50 | } 51 | 52 | var anyKeyService = new KeyedService(Microsoft.Extensions.DependencyInjection.KeyedService.AnyKey, keyedService.ServiceType); 53 | var anyKeyRegistrationSet = registrationAccessor(anyKeyService).ToArray(); 54 | if (anyKeyRegistrationSet.Length == 0) 55 | { 56 | return Enumerable.Empty(); 57 | } 58 | 59 | var anyKeyRegistration = anyKeyRegistrationSet[0]; 60 | 61 | // Use a fresh Guid so activated instances aren't shared. 62 | var registrationMappedToOriginalService = new ComponentRegistration( 63 | Guid.NewGuid(), 64 | anyKeyRegistration.Registration.Activator, 65 | anyKeyRegistration.Registration.Lifetime, 66 | anyKeyRegistration.Registration.Sharing, 67 | anyKeyRegistration.Registration.Ownership, 68 | new[] { service }, 69 | anyKeyRegistration.Registration.Metadata); 70 | 71 | return new[] { registrationMappedToOriginalService }; 72 | } 73 | 74 | private static bool IsCollection(Type serviceType) 75 | { 76 | if (IsGenericTypeDefinedBy(serviceType, typeof(IEnumerable<>))) 77 | { 78 | return true; 79 | } 80 | else if (serviceType.IsArray) 81 | { 82 | // GetElementType always non-null if IsArray is true. 83 | return true; 84 | } 85 | else if (IsGenericListOrCollectionInterfaceType(serviceType)) 86 | { 87 | return true; 88 | } 89 | 90 | return false; 91 | } 92 | 93 | private static bool IsGenericTypeDefinedBy(Type type, Type openGeneric) 94 | { 95 | return !type.ContainsGenericParameters 96 | && type.IsGenericType 97 | && type.GetGenericTypeDefinition() == openGeneric; 98 | } 99 | 100 | private static bool IsGenericListOrCollectionInterfaceType(Type type) 101 | { 102 | return IsGenericTypeDefinedBy(type, typeof(IList<>)) 103 | || IsGenericTypeDefinedBy(type, typeof(ICollection<>)) 104 | || IsGenericTypeDefinedBy(type, typeof(IReadOnlyCollection<>)) 105 | || IsGenericTypeDefinedBy(type, typeof(IReadOnlyList<>)); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/KeyTypeConversionException.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Globalization; 5 | 6 | namespace Autofac.Extensions.DependencyInjection; 7 | 8 | /// 9 | /// Exception indicating that type conversion failed when trying to inject a key 10 | /// using the 11 | /// . 12 | /// 13 | public class KeyTypeConversionException : Exception 14 | { 15 | /// 16 | /// Initializes a new instance of the class. 17 | /// 18 | /// 19 | /// The type of the key specified during service resolution. This is what 20 | /// would be injected into the constructor and needs to be converted. 21 | /// 22 | /// 23 | /// The type of parameter marked with the 24 | /// . 25 | /// This is what the key was trying to be converted to. 26 | /// 27 | public KeyTypeConversionException(Type resolutionKeyType, Type attributeKeyType) 28 | : this(resolutionKeyType, attributeKeyType, string.Format(CultureInfo.CurrentCulture, KeyTypeConversionExceptionResources.Message, resolutionKeyType, attributeKeyType)) 29 | { 30 | } 31 | 32 | /// 33 | /// Initializes a new instance of the class. 34 | /// 35 | /// 36 | /// The type of the key specified during service resolution. This is what 37 | /// would be injected into the constructor and needs to be converted. 38 | /// 39 | /// 40 | /// The type of parameter marked with the 41 | /// . 42 | /// This is what the key was trying to be converted to. 43 | /// 44 | /// 45 | /// A specific message for the exception to override the default. 46 | /// 47 | public KeyTypeConversionException(Type resolutionKeyType, Type attributeKeyType, string message) 48 | : base(message) 49 | { 50 | ResolutionKeyType = resolutionKeyType; 51 | AttributeKeyType = attributeKeyType; 52 | } 53 | 54 | /// 55 | /// Initializes a new instance of the class. 56 | /// 57 | /// 58 | /// The type of the key specified during service resolution. This is what 59 | /// would be injected into the constructor and needs to be converted. 60 | /// 61 | /// 62 | /// The type of parameter marked with the 63 | /// . 64 | /// This is what the key was trying to be converted to. 65 | /// 66 | /// The inner exception. 67 | public KeyTypeConversionException(Type resolutionKeyType, Type attributeKeyType, Exception? innerException) 68 | : this(resolutionKeyType, attributeKeyType, string.Format(CultureInfo.CurrentCulture, KeyTypeConversionExceptionResources.Message, resolutionKeyType, attributeKeyType), innerException) 69 | { 70 | } 71 | 72 | /// 73 | /// Initializes a new instance of the class. 74 | /// 75 | /// 76 | /// The type of the key specified during service resolution. This is what 77 | /// would be injected into the constructor and needs to be converted. 78 | /// 79 | /// 80 | /// The type of parameter marked with the 81 | /// . 82 | /// This is what the key was trying to be converted to. 83 | /// 84 | /// 85 | /// A specific message for the exception to override the default. 86 | /// 87 | /// The inner exception. 88 | public KeyTypeConversionException(Type resolutionKeyType, Type attributeKeyType, string message, Exception? innerException) 89 | : base(message, innerException) 90 | { 91 | ResolutionKeyType = resolutionKeyType; 92 | AttributeKeyType = attributeKeyType; 93 | } 94 | 95 | /// 96 | /// Gets the type of the parameter marked with the . 97 | /// 98 | /// 99 | /// The of parameter marked with the 100 | /// , 101 | /// which is the destination where the key should be injected. This should 102 | /// be compatible with the key provided during resolution. 103 | /// 104 | public Type AttributeKeyType { get; } 105 | 106 | /// 107 | /// Gets the type of the key specified during service resolution. 108 | /// 109 | /// 110 | /// The of key passed to the keyed service resolve 111 | /// operation. This is what would be injected as a constructor parameter to 112 | /// the service being resolved. 113 | /// 114 | public Type ResolutionKeyType { get; } 115 | } 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Autofac.Extensions.DependencyInjection 2 | 3 | Autofac is an [IoC container](http://martinfowler.com/articles/injection.html) for Microsoft .NET. It manages the dependencies between classes so that **applications stay easy to change as they grow** in size and complexity. This is achieved by treating regular .NET classes as *[components](https://autofac.readthedocs.io/en/latest/glossary.html)*. 4 | 5 | [![Build status](https://ci.appveyor.com/api/projects/status/1mhkjcqr1ug80lra/branch/develop?svg=true)](https://ci.appveyor.com/project/Autofac/autofac-extensions-dependencyinjection/branch/develop) [![codecov](https://codecov.io/gh/Autofac/Autofac.Extensions.DependencyInjection/branch/develop/graph/badge.svg)](https://codecov.io/gh/Autofac/Autofac.Extensions.DependencyInjection) 6 | 7 | Please file issues and pull requests for this package in this repository rather than in the Autofac core repo. 8 | 9 | - [Documentation - .NET Core Integration](https://autofac.readthedocs.io/en/latest/integration/netcore.html) 10 | - [Documentation - ASP.NET Core Integration](https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html) 11 | - [NuGet](https://www.nuget.org/packages/Autofac.Extensions.DependencyInjection) 12 | - [Contributing](https://autofac.readthedocs.io/en/latest/contributors.html) 13 | - [Open in Visual Studio Code](https://open.vscode.dev/autofac/Autofac.Extensions.DependencyInjection) 14 | 15 | ## Get Started in ASP.NET Core 16 | 17 | This quick start shows how to use the `IServiceProviderFactory{T}` integration that ASP.NET Core supports to help automatically build the root service provider for you. If you want more manual control, [check out the documentation for examples](https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html). 18 | 19 | - Reference the `Autofac.Extensions.DependencyInjection` package from NuGet. 20 | - In your `Program.Main` method, where you configure the `HostBuilder`, call `UseAutofac` to hook Autofac into the startup pipeline. 21 | - In the `ConfigureServices` method of your `Startup` class register things into the `IServiceCollection` using extension methods provided by other libraries. 22 | - In the `ConfigureContainer` method of your `Startup` class register things directly into an Autofac `ContainerBuilder`. 23 | 24 | The `IServiceProvider` will automatically be created for you, so there's nothing you have to do but *register things*. 25 | 26 | ```C# 27 | public class Program 28 | { 29 | public static async Task Main(string[] args) 30 | { 31 | // The service provider factory used here allows for 32 | // ConfigureContainer to be supported in Startup with 33 | // a strongly-typed ContainerBuilder. 34 | var host = Host.CreateDefaultBuilder(args) 35 | .UseServiceProviderFactory(new AutofacServiceProviderFactory()) 36 | .ConfigureWebHostDefaults(webHostBuilder => { 37 | webHostBuilder 38 | .UseContentRoot(Directory.GetCurrentDirectory()) 39 | .UseIISIntegration() 40 | .UseStartup() 41 | }) 42 | .Build(); 43 | 44 | await host.RunAsync(); 45 | } 46 | } 47 | 48 | public class Startup 49 | { 50 | public Startup(IWebHostEnvironment env) 51 | { 52 | var builder = new ConfigurationBuilder() 53 | .SetBasePath(env.ContentRootPath) 54 | .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 55 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 56 | .AddEnvironmentVariables(); 57 | this.Configuration = builder.Build(); 58 | } 59 | 60 | public IConfiguration Configuration { get; private set; } 61 | 62 | // ConfigureServices is where you register dependencies. This gets 63 | // called by the runtime before the ConfigureContainer method, below. 64 | public void ConfigureServices(IServiceCollection services) 65 | { 66 | // Add services to the collection. Don't build or return 67 | // any IServiceProvider or the ConfigureContainer method 68 | // won't get called. 69 | services.AddOptions(); 70 | } 71 | 72 | // ConfigureContainer is where you can register things directly 73 | // with Autofac. This runs after ConfigureServices so the things 74 | // here will override registrations made in ConfigureServices. 75 | // Don't build the container; that gets done for you. If you 76 | // need a reference to the container, you need to use the 77 | // "Without ConfigureContainer" mechanism shown later. 78 | public void ConfigureContainer(ContainerBuilder builder) 79 | { 80 | builder.RegisterModule(new AutofacModule()); 81 | } 82 | 83 | // Configure is where you add middleware. This is called after 84 | // ConfigureContainer. You can use IApplicationBuilder.ApplicationServices 85 | // here if you need to resolve things from the container. 86 | public void Configure( 87 | IApplicationBuilder app, 88 | ILoggerFactory loggerFactory) 89 | { 90 | loggerFactory.AddConsole(this.Configuration.GetSection("Logging")); 91 | loggerFactory.AddDebug(); 92 | app.UseMvc(); 93 | } 94 | } 95 | ``` 96 | 97 | Our [ASP.NET Core](https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html) integration documentation contains more information about using Autofac with ASP.NET Core. 98 | 99 | ## Get Help 100 | 101 | **Need help with Autofac?** We have [a documentation site](https://autofac.readthedocs.io/) as well as [API documentation](https://autofac.org/apidoc/). We're ready to answer your questions on [Stack Overflow](https://stackoverflow.com/questions/tagged/autofac) or check out the [discussion forum](https://groups.google.com/forum/#forum/autofac). 102 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/AutofacChildLifetimeScopeServiceProviderFactoryTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Diagnostics.CodeAnalysis; 5 | using Microsoft.Extensions.DependencyInjection; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Test; 8 | 9 | public sealed class AutofacChildLifetimeScopeServiceProviderFactoryTests 10 | { 11 | [Fact] 12 | public void CreateBuilderReturnsNewInstance() 13 | { 14 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); 15 | 16 | var configurationAdapter = factory.CreateBuilder(new ServiceCollection()); 17 | 18 | Assert.NotNull(configurationAdapter); 19 | } 20 | 21 | [Fact] 22 | public void CreateBuilderExecutesConfigurationActionWhenProvided() 23 | { 24 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope, b => b.Register(c => "Foo")); 25 | 26 | var configurationAdapter = factory.CreateBuilder(new ServiceCollection()); 27 | 28 | var builder = new ContainerBuilder(); 29 | 30 | foreach (var action in configurationAdapter.ConfigurationActions) 31 | { 32 | action(builder); 33 | } 34 | 35 | Assert.Equal("Foo", builder.Build().Resolve()); 36 | } 37 | 38 | [Fact] 39 | public void CreateBuilderAllowsForNullConfigurationAction() 40 | { 41 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); 42 | 43 | var configurationAdapter = factory.CreateBuilder(new ServiceCollection()); 44 | 45 | Assert.NotNull(configurationAdapter); 46 | } 47 | 48 | [Fact] 49 | public void CreateBuilderReturnsInstanceWithServicesPopulated() 50 | { 51 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); 52 | var services = new ServiceCollection().AddTransient(); 53 | 54 | var configurationAdapter = factory.CreateBuilder(services); 55 | 56 | var builder = new ContainerBuilder(); 57 | 58 | foreach (var action in configurationAdapter.ConfigurationActions) 59 | { 60 | action(builder); 61 | } 62 | 63 | Assert.True(builder.Build().IsRegistered()); 64 | } 65 | 66 | [Fact] 67 | public void CreateServiceProviderBuildsServiceProviderUsingAdapter() 68 | { 69 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); 70 | var services = new ServiceCollection().AddTransient(); 71 | var configurationAdapter = factory.CreateBuilder(services); 72 | 73 | var serviceProvider = factory.CreateServiceProvider(configurationAdapter); 74 | 75 | Assert.NotNull(serviceProvider.GetService(typeof(object))); 76 | } 77 | 78 | [Fact] 79 | public void CreateServiceProviderThrowsWhenProvidedNullAdapter() 80 | { 81 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); 82 | 83 | var exception = Assert.Throws(() => factory.CreateServiceProvider(null)); 84 | 85 | Assert.Equal("containerBuilder", exception.ParamName); 86 | } 87 | 88 | [Fact] 89 | public void CreateServiceProviderReturnsAutofacServiceProvider() 90 | { 91 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); 92 | 93 | var serviceProvider = factory.CreateServiceProvider(new AutofacChildLifetimeScopeConfigurationAdapter()); 94 | 95 | Assert.IsType(serviceProvider); 96 | } 97 | 98 | [Fact] 99 | public void CreateServiceProviderAddDepToServiceCollectionAndAddConfigurationTypesResolvable() 100 | { 101 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); 102 | 103 | var services = new ServiceCollection().AddTransient(); 104 | 105 | var configurationAdapter = factory.CreateBuilder(services); 106 | 107 | configurationAdapter.Add(builder => builder.RegisterType()); 108 | 109 | var serviceProvider = factory.CreateServiceProvider(configurationAdapter); 110 | 111 | serviceProvider.GetRequiredService(); 112 | serviceProvider.GetRequiredService(); 113 | } 114 | 115 | [Fact] 116 | public void CreateServiceProviderAddDepToRootContainerResolvable() 117 | { 118 | var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScopeWithDependency(typeof(DependencyOne))); 119 | 120 | var configurationAdapter = factory.CreateBuilder(new ServiceCollection()); 121 | 122 | var serviceProvider = factory.CreateServiceProvider(configurationAdapter); 123 | 124 | serviceProvider.GetRequiredService(); 125 | } 126 | 127 | private static ILifetimeScope GetRootLifetimeScope() => new ContainerBuilder().Build(); 128 | 129 | private static ILifetimeScope GetRootLifetimeScopeWithDependency(Type type) 130 | { 131 | var containerBuilder = new ContainerBuilder(); 132 | 133 | containerBuilder 134 | .RegisterType(type) 135 | .As(); 136 | 137 | return containerBuilder.Build(); 138 | } 139 | 140 | [SuppressMessage("CA1812", "CA1812", Justification = "Instantiated via dependency injection.")] 141 | private class DependencyOne 142 | { 143 | } 144 | 145 | [SuppressMessage("CA1812", "CA1812", Justification = "Instantiated via dependency injection.")] 146 | private class DependencyTwo 147 | { 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/KeyTypeConversionExceptionResources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | Unable to convert key of type '{0}' to type '{1}' for parameter injection. 122 | 123 | 124 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/ServiceProviderExtensionsResources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | Unable to retrieve Autofac root lifetime scope from service provider of type {0}. 122 | 123 | 124 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/KeyTypeManipulationResources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | Unable to convert object of type '{0}' to type '{1}'. 122 | 123 | 124 | The type '{0}' specified in the TypeConverterAttribute is not a TypeConverter. 125 | 126 | 127 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/AutofacServiceProvider.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using Autofac.Core; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using KeyedService = Autofac.Core.KeyedService; 7 | 8 | namespace Autofac.Extensions.DependencyInjection; 9 | 10 | /// 11 | /// Autofac implementation of the ASP.NET Core . 12 | /// 13 | /// 14 | /// 15 | public partial class AutofacServiceProvider : IServiceProvider, ISupportRequiredService, IKeyedServiceProvider, IServiceProviderIsService, IServiceProviderIsKeyedService, IDisposable, IAsyncDisposable 16 | { 17 | private readonly ILifetimeScope _lifetimeScope; 18 | 19 | private bool _disposed; 20 | 21 | /// 22 | /// Initializes a new instance of the class. 23 | /// 24 | /// 25 | /// The lifetime scope from which services will be resolved. 26 | /// 27 | public AutofacServiceProvider(ILifetimeScope lifetimeScope) 28 | { 29 | _lifetimeScope = lifetimeScope; 30 | } 31 | 32 | /// 33 | /// Gets the underlying instance of . 34 | /// 35 | public ILifetimeScope LifetimeScope => _lifetimeScope; 36 | 37 | /// 38 | /// Gets the service object of the specified type. 39 | /// 40 | /// 41 | /// An object that specifies the type of service object to get. 42 | /// 43 | /// 44 | /// An object that specifies the key of service object to get. 45 | /// 46 | /// 47 | /// A service object of type ; or 48 | /// if there is no service object of type . 49 | /// 50 | public object? GetKeyedService(Type serviceType, object? serviceKey) 51 | { 52 | if (serviceKey is null) 53 | { 54 | // A null key equates to "not keyed." 55 | return _lifetimeScope.ResolveOptional(serviceType); 56 | } 57 | else 58 | { 59 | try 60 | { 61 | return _lifetimeScope.ResolveOptionalService(new KeyedService(serviceKey, serviceType)); 62 | } 63 | catch (DependencyResolutionException ex) when (ex.InnerException is KeyTypeConversionException conversionException) 64 | { 65 | // If the issue was with converting the specified key type to 66 | // match a [ServiceKey] parameter type, the M.E.DI contract is 67 | // that it must be an InvalidOperationException. 68 | throw new InvalidOperationException(conversionException.Message, conversionException); 69 | } 70 | } 71 | } 72 | 73 | /// 74 | /// Gets service of type from the 75 | /// and requires it be present. 76 | /// 77 | /// 78 | /// An object that specifies the type of service object to get. 79 | /// 80 | /// 81 | /// An object that specifies the key of service object to get. 82 | /// 83 | /// 84 | /// A service object of type . 85 | /// 86 | /// 87 | /// Thrown if the isn't registered with the container. 88 | /// 89 | /// 90 | /// Thrown if the object can't be resolved from the container. 91 | /// 92 | public object GetRequiredKeyedService(Type serviceType, object? serviceKey) 93 | { 94 | if (serviceKey is null) 95 | { 96 | // A null key equates to "not keyed." 97 | return _lifetimeScope.Resolve(serviceType); 98 | } 99 | else 100 | { 101 | try 102 | { 103 | return _lifetimeScope.ResolveKeyed(serviceKey, serviceType); 104 | } 105 | catch (DependencyResolutionException ex) when (ex.InnerException is KeyTypeConversionException conversionException) 106 | { 107 | // If the issue was with converting the specified key type to 108 | // match a [ServiceKey] parameter type, the M.E.DI contract is 109 | // that it must be an InvalidOperationException. 110 | throw new InvalidOperationException(conversionException.Message, conversionException); 111 | } 112 | } 113 | } 114 | 115 | /// 116 | /// Gets service of type from the 117 | /// and requires it be present. 118 | /// 119 | /// 120 | /// An object that specifies the type of service object to get. 121 | /// 122 | /// 123 | /// A service object of type . 124 | /// 125 | /// 126 | /// Thrown if the isn't registered with the container. 127 | /// 128 | /// 129 | /// Thrown if the object can't be resolved from the container. 130 | /// 131 | public object GetRequiredService(Type serviceType) 132 | { 133 | return _lifetimeScope.Resolve(serviceType); 134 | } 135 | 136 | /// 137 | public bool IsKeyedService(Type serviceType, object? serviceKey) 138 | { 139 | // Null service key means non-keyed. 140 | if (serviceKey == null) 141 | { 142 | return IsService(serviceType); 143 | } 144 | 145 | return _lifetimeScope.ComponentRegistry.IsRegistered(new KeyedService(serviceKey, serviceType)); 146 | } 147 | 148 | /// 149 | public bool IsService(Type serviceType) => _lifetimeScope.ComponentRegistry.IsRegistered(new TypedService(serviceType)); 150 | 151 | /// 152 | /// Gets the service object of the specified type. 153 | /// 154 | /// 155 | /// An object that specifies the type of service object to get. 156 | /// 157 | /// 158 | /// A service object of type ; or 159 | /// if there is no service object of type . 160 | /// 161 | public object? GetService(Type serviceType) 162 | { 163 | return _lifetimeScope.ResolveOptional(serviceType); 164 | } 165 | 166 | /// 167 | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. 168 | /// 169 | public void Dispose() 170 | { 171 | Dispose(true); 172 | GC.SuppressFinalize(this); 173 | } 174 | 175 | /// 176 | /// Performs a dispose operation asynchronously. 177 | /// 178 | /// A task to await disposal. 179 | public async ValueTask DisposeAsync() 180 | { 181 | if (!_disposed) 182 | { 183 | _disposed = true; 184 | await _lifetimeScope.DisposeAsync().ConfigureAwait(false); 185 | GC.SuppressFinalize(this); 186 | } 187 | } 188 | 189 | /// 190 | /// Releases unmanaged and - optionally - managed resources. 191 | /// 192 | /// 193 | /// to release both managed and unmanaged resources; 194 | /// to release only unmanaged resources. 195 | /// 196 | protected virtual void Dispose(bool disposing) 197 | { 198 | if (!_disposed) 199 | { 200 | _disposed = true; 201 | if (disposing) 202 | { 203 | _lifetimeScope.Dispose(); 204 | } 205 | } 206 | } 207 | } 208 | -------------------------------------------------------------------------------- /src/Autofac.Extensions.DependencyInjection/KeyTypeManipulation.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.ComponentModel; 5 | using System.Globalization; 6 | using System.Reflection; 7 | 8 | namespace Autofac.Extensions.DependencyInjection; 9 | 10 | /// 11 | /// Utilities for converting keyed service key values into compatible types for 12 | /// injection using the . 13 | /// This logic originally came from Autofac.Configuration but there isn't a good 14 | /// "shared dependency" location for things like this other than core Autofac. 15 | /// 16 | internal class KeyTypeManipulation 17 | { 18 | /// 19 | /// Converts an object to a type compatible with a given parameter. 20 | /// 21 | /// The object value to convert. 22 | /// The destination to which should be converted. 23 | /// The parameter for which the is being converted. 24 | /// 25 | /// An of type , converted using 26 | /// type converters specified on if available. If 27 | /// is then the output will be for reference 28 | /// types and the default value for value types. 29 | /// 30 | /// 31 | /// Thrown if conversion of the value fails. 32 | /// 33 | public static object? ChangeToCompatibleType(object? value, Type destinationType, ParameterInfo memberInfo) 34 | { 35 | TypeConverterAttribute? attrib = null; 36 | if (memberInfo != null) 37 | { 38 | attrib = memberInfo.GetCustomAttributes(typeof(TypeConverterAttribute), true).Cast().FirstOrDefault(); 39 | } 40 | 41 | return ChangeToCompatibleType(value, destinationType, attrib); 42 | } 43 | 44 | /// 45 | /// Converts an object to a type compatible with a given parameter. 46 | /// 47 | /// The object value to convert. 48 | /// The destination to which should be converted. 49 | /// The parameter for which the is being converted. 50 | /// 51 | /// An of type , converted using 52 | /// type converters specified on if available. If 53 | /// is then the output will be for reference 54 | /// types and the default value for value types. 55 | /// 56 | /// 57 | /// Thrown if conversion of the value fails. 58 | /// 59 | public static object? ChangeToCompatibleType(object? value, Type destinationType, MemberInfo memberInfo) 60 | { 61 | TypeConverterAttribute? attrib = null; 62 | if (memberInfo != null) 63 | { 64 | attrib = memberInfo.GetCustomAttributes(typeof(TypeConverterAttribute), true).Cast().FirstOrDefault(); 65 | } 66 | 67 | return ChangeToCompatibleType(value, destinationType, attrib); 68 | } 69 | 70 | /// 71 | /// Converts an object to a type compatible with a given parameter. 72 | /// 73 | /// The object value to convert. 74 | /// The destination to which should be converted. 75 | /// A , if available, specifying the type of converter to use. is being converted. 76 | /// 77 | /// An of type , converted using 78 | /// any type converters specified in if available. If 79 | /// is then the output will be for reference 80 | /// types and the default value for value types. 81 | /// 82 | /// 83 | /// Thrown if conversion of the value fails. 84 | /// 85 | public static object? ChangeToCompatibleType(object? value, Type destinationType, TypeConverterAttribute? converterAttribute = null) 86 | { 87 | if (destinationType == null) 88 | { 89 | throw new ArgumentNullException(nameof(destinationType)); 90 | } 91 | 92 | if (value == null) 93 | { 94 | return destinationType.GetTypeInfo().IsValueType ? Activator.CreateInstance(destinationType) : null; 95 | } 96 | 97 | // Try implicit conversion. 98 | if (destinationType.IsInstanceOfType(value)) 99 | { 100 | return value; 101 | } 102 | 103 | TypeConverter converter; 104 | 105 | // Try to get custom type converter information. 106 | if (converterAttribute != null && !string.IsNullOrEmpty(converterAttribute.ConverterTypeName)) 107 | { 108 | try 109 | { 110 | converter = GetTypeConverterFromName(converterAttribute.ConverterTypeName); 111 | } 112 | catch (InvalidOperationException ex) 113 | { 114 | throw new KeyTypeConversionException(value.GetType(), destinationType, ex); 115 | } 116 | 117 | if (converter.CanConvertFrom(value.GetType())) 118 | { 119 | return converter.ConvertFrom(null, CultureInfo.InvariantCulture, value); 120 | } 121 | } 122 | 123 | // If there's not a custom converter specified via attribute, try for a default. 124 | converter = TypeDescriptor.GetConverter(value.GetType()); 125 | if (converter.CanConvertTo(destinationType)) 126 | { 127 | return converter.ConvertTo(null, CultureInfo.InvariantCulture, value, destinationType); 128 | } 129 | 130 | // Try explicit opposite conversion. 131 | converter = TypeDescriptor.GetConverter(destinationType); 132 | if (converter.CanConvertFrom(value.GetType())) 133 | { 134 | return converter.ConvertFrom(null, CultureInfo.InvariantCulture, value); 135 | } 136 | 137 | // Try a TryParse method. 138 | if (value is string) 139 | { 140 | // Some types in later frameworks have string TryParse and ReadOnlySpan TryParse 141 | // so they result in an AmbiguousMatchException unless we specify. 142 | var parser = destinationType.GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public, null, CallingConventions.Standard, new Type[] { typeof(string), destinationType.MakeByRefType() }, null); 143 | if (parser != null) 144 | { 145 | var parameters = new[] { value, null }; 146 | if ((bool)parser.Invoke(null, parameters)!) 147 | { 148 | return parameters[1]; 149 | } 150 | } 151 | } 152 | 153 | throw new KeyTypeConversionException(value.GetType(), destinationType); 154 | } 155 | 156 | /// 157 | /// Instantiates a type converter from its type name. 158 | /// 159 | /// 160 | /// The name of the of the . 161 | /// 162 | /// 163 | /// The instantiated . 164 | /// 165 | /// 166 | /// Thrown if does not correspond 167 | /// to a . 168 | /// 169 | private static TypeConverter GetTypeConverterFromName(string converterTypeName) 170 | { 171 | var converterType = Type.GetType(converterTypeName, true); 172 | return Activator.CreateInstance(converterType!) is not TypeConverter converter 173 | ? throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, KeyTypeManipulationResources.TypeConverterAttributeTypeNotConverter, converterTypeName)) 174 | : converter; 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | ; EditorConfig to support per-solution formatting. 2 | ; Use the EditorConfig VS add-in to make this work. 3 | ; http://editorconfig.org/ 4 | 5 | ; This is the default for the codeline. 6 | root = true 7 | 8 | [*] 9 | indent_style = space 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | 13 | ; .NET Code - almost, but not exactly, the same suggestions as corefx 14 | ; https://github.com/dotnet/corefx/blob/master/.editorconfig 15 | [*.cs] 16 | indent_size = 4 17 | charset = utf-8-bom 18 | 19 | ; New line preferences 20 | csharp_new_line_before_open_brace = all 21 | csharp_new_line_before_else = true 22 | csharp_new_line_before_catch = true 23 | csharp_new_line_before_finally = true 24 | csharp_new_line_before_members_in_object_initializers = true 25 | csharp_new_line_before_members_in_anonymous_types = true 26 | csharp_new_line_between_query_expression_clauses = true 27 | 28 | ; Indentation preferences 29 | csharp_indent_block_contents = true 30 | csharp_indent_braces = false 31 | csharp_indent_case_contents = true 32 | csharp_indent_case_contents_when_block = true 33 | csharp_indent_switch_labels = true 34 | csharp_indent_labels = one_less_than_current 35 | 36 | ; Modifier preferences 37 | csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async:warning 38 | 39 | ; Avoid this. unless absolutely necessary 40 | dotnet_style_qualification_for_field = false:suggestion 41 | dotnet_style_qualification_for_property = false:suggestion 42 | dotnet_style_qualification_for_method = false:suggestion 43 | dotnet_style_qualification_for_event = false:suggestion 44 | 45 | ; Types: use keywords instead of BCL types, using var is fine. 46 | csharp_style_var_when_type_is_apparent = false:none 47 | dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion 48 | dotnet_style_predefined_type_for_member_access = true:suggestion 49 | 50 | ; Name all constant fields using PascalCase 51 | dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = warning 52 | dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields 53 | dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style 54 | dotnet_naming_symbols.constant_fields.applicable_kinds = field 55 | dotnet_naming_symbols.constant_fields.required_modifiers = const 56 | dotnet_naming_style.pascal_case_style.capitalization = pascal_case 57 | 58 | ; Static fields should be _camelCase 59 | dotnet_naming_rule.static_fields_should_be_camel_case.severity = warning 60 | dotnet_naming_rule.static_fields_should_be_camel_case.symbols = static_fields 61 | dotnet_naming_rule.static_fields_should_be_camel_case.style = camel_case_underscore_style 62 | dotnet_naming_symbols.static_fields.applicable_kinds = field 63 | dotnet_naming_symbols.static_fields.required_modifiers = static 64 | dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected 65 | 66 | ; Static readonly fields should be PascalCase 67 | dotnet_naming_rule.static_readonly_fields_should_be_pascal_case.severity = warning 68 | dotnet_naming_rule.static_readonly_fields_should_be_pascal_case.symbols = static_readonly_fields 69 | dotnet_naming_rule.static_readonly_fields_should_be_pascal_case.style = pascal_case_style 70 | dotnet_naming_symbols.static_readonly_fields.applicable_kinds = field 71 | dotnet_naming_symbols.static_readonly_fields.required_modifiers = static, readonly 72 | dotnet_naming_symbols.static_readonly_fields.applicable_accessibilities = private, internal, private_protected 73 | 74 | ; Internal and private fields should be _camelCase 75 | dotnet_naming_rule.camel_case_for_private_internal_fields.severity = warning 76 | dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields 77 | dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style 78 | dotnet_naming_symbols.private_internal_fields.applicable_kinds = field 79 | dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal 80 | dotnet_naming_style.camel_case_underscore_style.required_prefix = _ 81 | dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case 82 | 83 | ; Code style defaults 84 | csharp_using_directive_placement = outside_namespace:suggestion 85 | dotnet_sort_system_directives_first = true 86 | csharp_prefer_braces = true:refactoring 87 | csharp_preserve_single_line_blocks = true:none 88 | csharp_preserve_single_line_statements = false:none 89 | csharp_prefer_static_local_function = true:suggestion 90 | csharp_prefer_simple_using_statement = false:none 91 | csharp_style_prefer_switch_expression = true:suggestion 92 | 93 | ; Code quality 94 | dotnet_style_readonly_field = true:suggestion 95 | dotnet_code_quality_unused_parameters = non_public:suggestion 96 | 97 | ; Expression-level preferences 98 | dotnet_style_object_initializer = true:suggestion 99 | dotnet_style_collection_initializer = true:suggestion 100 | dotnet_style_explicit_tuple_names = true:suggestion 101 | dotnet_style_coalesce_expression = true:suggestion 102 | dotnet_style_null_propagation = true:suggestion 103 | dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion 104 | dotnet_style_prefer_inferred_tuple_names = true:suggestion 105 | dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion 106 | dotnet_style_prefer_auto_properties = true:suggestion 107 | dotnet_style_prefer_conditional_expression_over_assignment = true:refactoring 108 | dotnet_style_prefer_conditional_expression_over_return = true:refactoring 109 | csharp_prefer_simple_default_expression = true:suggestion 110 | 111 | # Expression-bodied members 112 | csharp_style_expression_bodied_methods = true:refactoring 113 | csharp_style_expression_bodied_constructors = true:refactoring 114 | csharp_style_expression_bodied_operators = true:refactoring 115 | csharp_style_expression_bodied_properties = true:refactoring 116 | csharp_style_expression_bodied_indexers = true:refactoring 117 | csharp_style_expression_bodied_accessors = true:refactoring 118 | csharp_style_expression_bodied_lambdas = true:refactoring 119 | csharp_style_expression_bodied_local_functions = true:refactoring 120 | 121 | # Pattern matching 122 | csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion 123 | csharp_style_pattern_matching_over_as_with_null_check = true:suggestion 124 | csharp_style_inlined_variable_declaration = true:suggestion 125 | 126 | # Null checking preferences 127 | csharp_style_throw_expression = true:suggestion 128 | csharp_style_conditional_delegate_call = true:suggestion 129 | 130 | # Other features 131 | csharp_style_namespace_declarations = file_scoped:suggestion 132 | csharp_style_prefer_index_operator = false:none 133 | csharp_style_prefer_range_operator = false:none 134 | csharp_style_pattern_local_over_anonymous_function = false:none 135 | 136 | # Space preferences 137 | csharp_space_after_cast = false 138 | csharp_space_after_colon_in_inheritance_clause = true 139 | csharp_space_after_comma = true 140 | csharp_space_after_dot = false 141 | csharp_space_after_keywords_in_control_flow_statements = true 142 | csharp_space_after_semicolon_in_for_statement = true 143 | csharp_space_around_binary_operators = before_and_after 144 | csharp_space_around_declaration_statements = do_not_ignore 145 | csharp_space_before_colon_in_inheritance_clause = true 146 | csharp_space_before_comma = false 147 | csharp_space_before_dot = false 148 | csharp_space_before_open_square_brackets = false 149 | csharp_space_before_semicolon_in_for_statement = false 150 | csharp_space_between_empty_square_brackets = false 151 | csharp_space_between_method_call_empty_parameter_list_parentheses = false 152 | csharp_space_between_method_call_name_and_opening_parenthesis = false 153 | csharp_space_between_method_call_parameter_list_parentheses = false 154 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false 155 | csharp_space_between_method_declaration_name_and_open_parenthesis = false 156 | csharp_space_between_method_declaration_parameter_list_parentheses = false 157 | csharp_space_between_parentheses = false 158 | csharp_space_between_square_brackets = false 159 | 160 | ; .NET project files and MSBuild - match defaults for VS 161 | [*.{csproj,nuspec,proj,projitems,props,shproj,targets,vbproj,vcxproj,vcxproj.filters,vsixmanifest,vsct}] 162 | indent_size = 2 163 | 164 | ; .NET solution files - match defaults for VS 165 | [*.sln] 166 | end_of_line = crlf 167 | indent_style = tab 168 | 169 | ; Config - match XML and default nuget.config template 170 | [*.config] 171 | indent_size = 2 172 | 173 | ; Resources - match defaults for VS 174 | [*.resx] 175 | indent_size = 2 176 | 177 | ; Static analysis rulesets - match defaults for VS 178 | [*.ruleset] 179 | indent_size = 2 180 | 181 | ; HTML, XML - match defaults for VS 182 | [*.{cshtml,html,xml}] 183 | indent_size = 4 184 | 185 | ; JavaScript and JS mixes - match eslint settings; JSON also matches .NET Core templates 186 | [*.{js,json,ts,vue}] 187 | indent_size = 2 188 | 189 | ; Markdown - match markdownlint settings 190 | [*.{md,markdown}] 191 | indent_size = 2 192 | 193 | ; PowerShell - match defaults for New-ModuleManifest and PSScriptAnalyzer Invoke-Formatter 194 | [*.{ps1,psd1,psm1}] 195 | indent_size = 4 196 | charset = utf-8-bom 197 | 198 | ; ReStructuredText - standard indentation format from examples 199 | [*.rst] 200 | indent_size = 2 201 | 202 | # YAML - match standard YAML like Kubernetes and GitHub Actions 203 | [*.{yaml,yml}] 204 | indent_size = 2 205 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/Specification/AssumedBehaviorTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Diagnostics.CodeAnalysis; 5 | using Microsoft.Extensions.DependencyInjection; 6 | 7 | namespace Autofac.Extensions.DependencyInjection.Test.Specification; 8 | 9 | /// 10 | /// Additional tests to illustrate undocumented yet assumed behaviors in 11 | /// the Microsoft.Extensions.DependencyInjection container/scope. 12 | /// 13 | public abstract class AssumedBehaviorTests 14 | { 15 | [Fact] 16 | public void DisposingScopeAlsoDisposesServiceProvider() 17 | { 18 | // You can't resolve things from a scope's service provider 19 | // if you dispose the scope. 20 | var services = new ServiceCollection().AddScoped(); 21 | var rootProvider = CreateServiceProvider(services); 22 | var scope = rootProvider.CreateScope(); 23 | Assert.NotNull(scope.ServiceProvider.GetRequiredService()); 24 | scope.Dispose(); 25 | Assert.Throws(() => scope.ServiceProvider.GetRequiredService()); 26 | } 27 | 28 | [Fact] 29 | public void DisposingScopeAndProviderOnlyDisposesObjectsOnce() 30 | { 31 | // Disposing the service provider and then the scope only 32 | // runs one disposal on the resolved objects. 33 | var services = new ServiceCollection().AddScoped(); 34 | var rootProvider = CreateServiceProvider(services); 35 | var scope = rootProvider.CreateScope(); 36 | var tracker = scope.ServiceProvider.GetRequiredService(); 37 | ((IDisposable)scope.ServiceProvider).Dispose(); 38 | Assert.True(tracker.Disposed); 39 | Assert.Equal(1, tracker.DisposeCount); 40 | scope.Dispose(); 41 | Assert.Equal(1, tracker.DisposeCount); 42 | } 43 | 44 | [Fact] 45 | public void DisposingScopeServiceProviderStopsNewScopes() 46 | { 47 | // You can't create a new child scope if you've disposed of 48 | // the parent scope service provider. 49 | var rootProvider = CreateServiceProvider(new ServiceCollection()); 50 | using var scope = rootProvider.CreateScope(); 51 | ((IDisposable)scope.ServiceProvider).Dispose(); 52 | Assert.Throws(() => scope.ServiceProvider.CreateScope()); 53 | } 54 | 55 | [Fact] 56 | public void DisposingScopeServiceProviderStopsScopeResolutions() 57 | { 58 | // You can't resolve things from a scope if you dispose the 59 | // scope's service provider. 60 | var services = new ServiceCollection().AddScoped(); 61 | var rootProvider = CreateServiceProvider(services); 62 | using var scope = rootProvider.CreateScope(); 63 | Assert.NotNull(scope.ServiceProvider.GetRequiredService()); 64 | ((IDisposable)scope.ServiceProvider).Dispose(); 65 | Assert.Throws(() => scope.ServiceProvider.GetRequiredService()); 66 | } 67 | 68 | [Fact] 69 | public void ResolvedProviderNotSameAsParent() 70 | { 71 | // Resolving a provider from another provider yields a new object. 72 | // (It's not just returning "this" - it's a different IServiceProvider.) 73 | var parent = CreateServiceProvider(new ServiceCollection()); 74 | var resolved = parent.GetRequiredService(); 75 | Assert.NotSame(parent, resolved); 76 | } 77 | 78 | [Fact] 79 | public void ResolvedProviderUsesSameScopeAsParent() 80 | { 81 | // Resolving a provider from another provider will still resolve 82 | // items from the same scope. 83 | var services = new ServiceCollection().AddScoped(); 84 | var root = CreateServiceProvider(services); 85 | using var scope = root.CreateScope(); 86 | var parent = scope.ServiceProvider; 87 | var resolved = parent.GetRequiredService(); 88 | Assert.Same(parent.GetRequiredService(), resolved.GetRequiredService()); 89 | } 90 | 91 | [Fact] 92 | public void ServiceProviderWillNotResolveAfterDispose() 93 | { 94 | // You can't resolve things from a service provider 95 | // if you dispose it. 96 | var services = new ServiceCollection().AddScoped(); 97 | var rootProvider = CreateServiceProvider(services); 98 | Assert.NotNull(rootProvider.GetRequiredService()); 99 | ((IDisposable)rootProvider).Dispose(); 100 | Assert.Throws(() => rootProvider.GetRequiredService()); 101 | } 102 | 103 | [Fact] 104 | public async Task ServiceProviderDisposesAsync() 105 | { 106 | // You can't resolve things from a service provider 107 | // if you dispose it. 108 | var services = new ServiceCollection().AddScoped(); 109 | var rootProvider = CreateServiceProvider(services); 110 | var tracker = rootProvider.GetRequiredService(); 111 | var asyncDisposer = (IAsyncDisposable)rootProvider; 112 | 113 | await asyncDisposer.DisposeAsync(); 114 | 115 | Assert.True(tracker.AsyncDisposed); 116 | Assert.False(tracker.SyncDisposed); 117 | } 118 | 119 | [Fact] 120 | public async Task ServiceScopeDisposesAsync() 121 | { 122 | // You can't resolve things from a service provider 123 | // if you dispose it. 124 | var services = new ServiceCollection().AddScoped(); 125 | var rootProvider = CreateServiceProvider(services); 126 | 127 | AsyncDisposeTracker tracker; 128 | 129 | // Try out the new "CreateAsyncScope" method. 130 | var scope = rootProvider.CreateAsyncScope(); 131 | await using (scope.ConfigureAwait(false)) 132 | { 133 | tracker = scope.ServiceProvider.GetRequiredService(); 134 | } 135 | 136 | Assert.True(tracker.AsyncDisposed); 137 | Assert.False(tracker.SyncDisposed); 138 | } 139 | 140 | [Fact] 141 | public void ServiceScopeFactoryIsSingleton() 142 | { 143 | // Issue #83: M.E.DI assumes service scope factory is singleton, but 144 | // there's no compatibility test for it yet. 145 | var services = new ServiceCollection(); 146 | var rootProvider = CreateServiceProvider(services); 147 | var rootFactory1 = rootProvider.GetRequiredService(); 148 | var rootFactory2 = rootProvider.GetRequiredService(); 149 | Assert.Same(rootFactory1, rootFactory2); 150 | 151 | var childScope = rootFactory1.CreateScope(); 152 | var childFactory = childScope.ServiceProvider.GetRequiredService(); 153 | Assert.Same(rootFactory1, childFactory); 154 | } 155 | 156 | [Fact] 157 | public void ServiceScopesAreFlat() 158 | { 159 | // Issue #83: M.E.DI assumes service scopes are flat - disposing of 160 | // a "parent" scope won't actually invalidate a "child" because 161 | // they're not related in that fashion. 162 | var services = new ServiceCollection().AddSingleton(); 163 | var rootProvider = CreateServiceProvider(services); 164 | 165 | var outerScope = rootProvider.CreateScope(); 166 | var innerScope = outerScope.ServiceProvider.CreateScope(); 167 | outerScope.Dispose(); 168 | 169 | // This part will blow up if the scopes are hierarchical. 170 | innerScope.ServiceProvider.GetRequiredService(); 171 | innerScope.Dispose(); 172 | } 173 | 174 | [Fact] 175 | public void ServiceInstancesRegisteredAreNotDisposedWhenTheProviderIsDisposed() 176 | { 177 | using var externalService = new DisposeTracker(); 178 | var services = new ServiceCollection().AddSingleton(externalService); 179 | var rootProvider = CreateServiceProvider(services); 180 | ((IDisposable)rootProvider).Dispose(); 181 | Assert.False(externalService.Disposed); 182 | } 183 | 184 | protected abstract IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection); 185 | 186 | [SuppressMessage("CA1812", "CA1812", Justification = "Instantiated through reflection.")] 187 | private class DisposeTrackerConsumer 188 | { 189 | public DisposeTrackerConsumer(IEnumerable trackers) 190 | { 191 | Trackers = trackers; 192 | } 193 | 194 | public IEnumerable Trackers { get; } 195 | } 196 | 197 | [SuppressMessage("CA1812", "CA1812", Justification = "Instantiated via dependency injection.")] 198 | private class DisposeTracker : IDisposable 199 | { 200 | public int DisposeCount { get; set; } 201 | 202 | public bool Disposed { get; set; } 203 | 204 | public void Dispose() 205 | { 206 | Disposed = true; 207 | DisposeCount++; 208 | } 209 | } 210 | 211 | [SuppressMessage("CA1812", "CA1812", Justification = "Instantiated via dependency injection.")] 212 | private class AsyncDisposeTracker : IDisposable, IAsyncDisposable 213 | { 214 | public bool SyncDisposed { get; set; } 215 | 216 | public bool AsyncDisposed { get; set; } 217 | 218 | public void Dispose() 219 | { 220 | SyncDisposed = true; 221 | } 222 | 223 | public async ValueTask DisposeAsync() 224 | { 225 | await Task.Delay(1).ConfigureAwait(false); 226 | 227 | AsyncDisposed = true; 228 | } 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /test/Autofac.Extensions.DependencyInjection.Test/AutofacRegistrationTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Autofac Project. All rights reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System.Diagnostics.CodeAnalysis; 5 | using Autofac.Core; 6 | using Autofac.Core.Lifetime; 7 | using Microsoft.Extensions.DependencyInjection; 8 | using Microsoft.Extensions.Options; 9 | 10 | namespace Autofac.Extensions.DependencyInjection.Test; 11 | 12 | public class AutofacRegistrationTests 13 | { 14 | [Fact] 15 | public void PopulateRegistersServiceProvider() 16 | { 17 | var builder = new ContainerBuilder(); 18 | builder.Populate(Enumerable.Empty()); 19 | var container = builder.Build(); 20 | 21 | container.AssertRegistered(); 22 | } 23 | 24 | [Fact] 25 | public void PopulateThrowsForNullBuilder() 26 | { 27 | Assert.Throws(() => AutofacRegistration.Populate(null, Enumerable.Empty())); 28 | } 29 | 30 | [Fact] 31 | public void PopulateThrowsForNullDescriptors() 32 | { 33 | Assert.Throws(() => new ContainerBuilder().Populate(null)); 34 | } 35 | 36 | [Fact] 37 | public void CorrectServiceProviderIsRegistered() 38 | { 39 | var builder = new ContainerBuilder(); 40 | builder.Populate(Enumerable.Empty()); 41 | var container = builder.Build(); 42 | 43 | container.AssertImplementation(); 44 | } 45 | 46 | [Fact] 47 | public void ServiceProviderInstancesAreNotTracked() 48 | { 49 | var builder = new ContainerBuilder(); 50 | builder.Populate(Enumerable.Empty()); 51 | var container = builder.Build(); 52 | 53 | container.AssertOwnership(InstanceOwnership.ExternallyOwned); 54 | } 55 | 56 | [Fact] 57 | public void PopulateRegistersServiceScopeFactory() 58 | { 59 | var builder = new ContainerBuilder(); 60 | builder.Populate(Enumerable.Empty()); 61 | var container = builder.Build(); 62 | 63 | container.AssertRegistered(); 64 | } 65 | 66 | [Fact] 67 | public void ServiceScopeFactoryIsRegistered() 68 | { 69 | var builder = new ContainerBuilder(); 70 | builder.Populate(Enumerable.Empty()); 71 | var container = builder.Build(); 72 | 73 | container.AssertImplementation(); 74 | } 75 | 76 | [Fact] 77 | public void CanRegisterTransientService() 78 | { 79 | var builder = new ContainerBuilder(); 80 | var descriptor = new ServiceDescriptor(typeof(IService), typeof(Service), ServiceLifetime.Transient); 81 | builder.Populate(new ServiceDescriptor[] { descriptor }); 82 | var container = builder.Build(); 83 | 84 | container.AssertLifetime(); 85 | container.AssertSharing(InstanceSharing.None); 86 | container.AssertOwnership(InstanceOwnership.OwnedByLifetimeScope); 87 | } 88 | 89 | [Fact] 90 | public void CanRegisterSingletonService() 91 | { 92 | var builder = new ContainerBuilder(); 93 | var descriptor = new ServiceDescriptor(typeof(IService), typeof(Service), ServiceLifetime.Singleton); 94 | builder.Populate(new ServiceDescriptor[] { descriptor }); 95 | var container = builder.Build(); 96 | 97 | container.AssertLifetime(); 98 | container.AssertSharing(InstanceSharing.Shared); 99 | container.AssertOwnership(InstanceOwnership.OwnedByLifetimeScope); 100 | } 101 | 102 | [Fact] 103 | public void CanRebaseSingletonServiceToNamedLifetimeScope() 104 | { 105 | var builder = new ContainerBuilder(); 106 | var descriptor = new ServiceDescriptor(typeof(IService), typeof(Service), ServiceLifetime.Singleton); 107 | builder.Populate(new ServiceDescriptor[] { descriptor }, "MY_SCOPE"); 108 | var container = builder.Build(); 109 | 110 | container.AssertLifetime(); 111 | container.AssertSharing(InstanceSharing.Shared); 112 | container.AssertOwnership(InstanceOwnership.OwnedByLifetimeScope); 113 | } 114 | 115 | [Fact] 116 | public void CanRegisterScopedService() 117 | { 118 | var builder = new ContainerBuilder(); 119 | var descriptor = new ServiceDescriptor(typeof(IService), typeof(Service), ServiceLifetime.Scoped); 120 | builder.Populate(new ServiceDescriptor[] { descriptor }); 121 | var container = builder.Build(); 122 | 123 | container.AssertLifetime(); 124 | container.AssertSharing(InstanceSharing.Shared); 125 | container.AssertOwnership(InstanceOwnership.OwnedByLifetimeScope); 126 | } 127 | 128 | [Fact] 129 | public void CanRegisterGenericService() 130 | { 131 | var builder = new ContainerBuilder(); 132 | var descriptor = new ServiceDescriptor(typeof(IList<>), typeof(List<>), ServiceLifetime.Scoped); 133 | builder.Populate(new ServiceDescriptor[] { descriptor }); 134 | var container = builder.Build(); 135 | 136 | container.AssertRegistered>(); 137 | } 138 | 139 | [Fact] 140 | public void CanRegisterFactoryService() 141 | { 142 | var builder = new ContainerBuilder(); 143 | var descriptor = new ServiceDescriptor(typeof(IService), sp => new Service(), ServiceLifetime.Transient); 144 | builder.Populate(new ServiceDescriptor[] { descriptor }); 145 | var container = builder.Build(); 146 | 147 | container.AssertRegistered>(); 148 | } 149 | 150 | [Fact] 151 | public void CanResolveOptionsFromChildScopeProvider() 152 | { 153 | // Issue #32: Registering options in a child scope fails to resolve IOptions. 154 | var container = new ContainerBuilder().Build(); 155 | var scope = container.BeginLifetimeScope(b => 156 | { 157 | var services = new ServiceCollection(); 158 | services 159 | .AddOptions() 160 | .Configure(opt => 161 | { 162 | opt.Value = 6; 163 | }); 164 | b.Populate(services); 165 | }); 166 | 167 | using var provider = new AutofacServiceProvider(scope); 168 | var options = provider.GetRequiredService>(); 169 | Assert.Equal(6, options.Value.Value); 170 | } 171 | 172 | [Fact] 173 | public void CanGenerateFactoryService() 174 | { 175 | var builder = new ContainerBuilder(); 176 | var descriptor = new ServiceDescriptor(typeof(IService), typeof(Service), ServiceLifetime.Transient); 177 | builder.Populate(new ServiceDescriptor[] { descriptor }); 178 | var container = builder.Build(); 179 | 180 | container.AssertRegistered>(); 181 | } 182 | 183 | [Fact] 184 | public void ServiceCollectionConfigurationIsRetainedInRootContainer() 185 | { 186 | var collection = new ServiceCollection(); 187 | collection.AddOptions(); 188 | collection.Configure(options => 189 | { 190 | options.Value = 5; 191 | }); 192 | 193 | var builder = new ContainerBuilder(); 194 | builder.Populate(collection); 195 | var container = builder.Build(); 196 | 197 | var resolved = container.Resolve>(); 198 | Assert.NotNull(resolved.Value); 199 | Assert.Equal(5, resolved.Value.Value); 200 | } 201 | 202 | [Fact] 203 | public void RegistrationsAddedAfterPopulateComeLastWhenResolvedWithIEnumerable() 204 | { 205 | const string s1 = "s1"; 206 | const string s2 = "s2"; 207 | const string s3 = "s3"; 208 | const string s4 = "s4"; 209 | 210 | var collection = new ServiceCollection(); 211 | collection.AddTransient(provider => s1); 212 | collection.AddTransient(provider => s2); 213 | var builder = new ContainerBuilder(); 214 | builder.Populate(collection); 215 | builder.Register(c => s3); 216 | builder.Register(c => s4); 217 | var container = builder.Build(); 218 | 219 | var resolved = container.Resolve>().ToArray(); 220 | 221 | Assert.Equal(resolved, new[] { s1, s2, s3, s4 }); 222 | } 223 | 224 | [Fact] 225 | public void RegistrationsAddedBeforePopulateComeFirstWhenResolvedWithIEnumerable() 226 | { 227 | const string s1 = "s1"; 228 | const string s2 = "s2"; 229 | const string s3 = "s3"; 230 | const string s4 = "s4"; 231 | 232 | var builder = new ContainerBuilder(); 233 | builder.Register(c => s1); 234 | builder.Register(c => s2); 235 | var collection = new ServiceCollection(); 236 | collection.AddTransient(provider => s3); 237 | collection.AddTransient(provider => s4); 238 | builder.Populate(collection); 239 | var container = builder.Build(); 240 | 241 | var resolved = container.Resolve>().ToArray(); 242 | 243 | Assert.Equal(resolved, new[] { s1, s2, s3, s4 }); 244 | } 245 | 246 | private class Service : IService 247 | { 248 | } 249 | 250 | private interface IService 251 | { 252 | } 253 | 254 | [SuppressMessage("CA1812", "CA1812", Justification = "Instantiated via dependency injection.")] 255 | private class TestOptions 256 | { 257 | public int Value { get; set; } 258 | } 259 | } 260 | -------------------------------------------------------------------------------- /Autofac.Extensions.DependencyInjection.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29201.188 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{5A54DF18-E3F3-4929-876D-00650A15763E}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{DEA4A8C6-DE56-4359-A87C-472FB34132E7}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6B324E70-6C86-4E09-B150-CAE9DD2BADCC}" 11 | ProjectSection(SolutionItems) = preProject 12 | .gitignore = .gitignore 13 | appveyor.yml = appveyor.yml 14 | global.json = global.json 15 | NuGet.Config = NuGet.Config 16 | codecov.yml = codecov.yml 17 | build.ps1 = build.ps1 18 | EndProjectSection 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Autofac.Extensions.DependencyInjection", "src\Autofac.Extensions.DependencyInjection\Autofac.Extensions.DependencyInjection.csproj", "{513C7F7A-A758-4D48-94F4-891A00F63DA1}" 21 | EndProject 22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Autofac.Extensions.DependencyInjection.Test", "test\Autofac.Extensions.DependencyInjection.Test\Autofac.Extensions.DependencyInjection.Test.csproj", "{911AA52A-4E68-41C5-AB24-D1618AFDF753}" 23 | EndProject 24 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "bench", "bench", "{C39BE99E-D778-485B-920A-42D4EB3CBA75}" 25 | EndProject 26 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Autofac.Extensions.DependencyInjection.Bench", "bench\Autofac.Extensions.DependencyInjection.Bench\Autofac.Extensions.DependencyInjection.Bench.csproj", "{639CD744-1E61-4EF8-9E5A-9920A6BC3891}" 27 | EndProject 28 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "projects", "projects", "{7D26E232-3D30-4151-BB0A-C5965419F268}" 29 | EndProject 30 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchProject.AutofacApiServer", "bench\projects\Bench.AutofacApiServer\BenchProject.AutofacApiServer.csproj", "{F835591F-9578-411E-908F-BB46FD4DB00C}" 31 | EndProject 32 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{4072A677-5ACA-4CA4-B5EB-9B9DE7D1DD08}" 33 | ProjectSection(SolutionItems) = preProject 34 | build\Analyzers.ruleset = build\Analyzers.ruleset 35 | build\Autofac.Build.psd1 = build\Autofac.Build.psd1 36 | build\Autofac.Build.psm1 = build\Autofac.Build.psm1 37 | build\CodeAnalysisDictionary.xml = build\CodeAnalysisDictionary.xml 38 | EndProjectSection 39 | EndProject 40 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Autofac.Extensions.DependencyInjection.Integration.Test", "test\Autofac.Extensions.DependencyInjection.Integration.Test\Autofac.Extensions.DependencyInjection.Integration.Test.csproj", "{D6217688-8E4A-4092-8D78-C4D299F27D7B}" 41 | EndProject 42 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Integration.Net6", "test\Integration.Net6\Integration.Net6.csproj", "{EBAA6D3C-F458-4375-99E7-33E22D8F677E}" 43 | EndProject 44 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Integration.Net7", "test\Integration.Net7\Integration.Net7.csproj", "{F6B0977A-FAC6-4F9B-813A-C9D03F330887}" 45 | EndProject 46 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Integration.Net8", "test\Integration.Net8\Integration.Net8.csproj", "{549E4C50-7B8F-4E18-92E1-60F036B3515F}" 47 | EndProject 48 | Global 49 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 50 | Debug|Any CPU = Debug|Any CPU 51 | Debug|ARM = Debug|ARM 52 | Debug|x64 = Debug|x64 53 | Debug|x86 = Debug|x86 54 | Release|Any CPU = Release|Any CPU 55 | Release|ARM = Release|ARM 56 | Release|x64 = Release|x64 57 | Release|x86 = Release|x86 58 | EndGlobalSection 59 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 60 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 61 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Debug|Any CPU.Build.0 = Debug|Any CPU 62 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Debug|ARM.ActiveCfg = Debug|Any CPU 63 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Debug|ARM.Build.0 = Debug|Any CPU 64 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Debug|x64.ActiveCfg = Debug|Any CPU 65 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Debug|x64.Build.0 = Debug|Any CPU 66 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Debug|x86.ActiveCfg = Debug|Any CPU 67 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Debug|x86.Build.0 = Debug|Any CPU 68 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Release|Any CPU.ActiveCfg = Release|Any CPU 69 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Release|Any CPU.Build.0 = Release|Any CPU 70 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Release|ARM.ActiveCfg = Release|Any CPU 71 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Release|ARM.Build.0 = Release|Any CPU 72 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Release|x64.ActiveCfg = Release|Any CPU 73 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Release|x64.Build.0 = Release|Any CPU 74 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Release|x86.ActiveCfg = Release|Any CPU 75 | {513C7F7A-A758-4D48-94F4-891A00F63DA1}.Release|x86.Build.0 = Release|Any CPU 76 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 77 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Debug|Any CPU.Build.0 = Debug|Any CPU 78 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Debug|ARM.ActiveCfg = Debug|Any CPU 79 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Debug|ARM.Build.0 = Debug|Any CPU 80 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Debug|x64.ActiveCfg = Debug|Any CPU 81 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Debug|x64.Build.0 = Debug|Any CPU 82 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Debug|x86.ActiveCfg = Debug|Any CPU 83 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Debug|x86.Build.0 = Debug|Any CPU 84 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Release|Any CPU.ActiveCfg = Release|Any CPU 85 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Release|Any CPU.Build.0 = Release|Any CPU 86 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Release|ARM.ActiveCfg = Release|Any CPU 87 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Release|ARM.Build.0 = Release|Any CPU 88 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Release|x64.ActiveCfg = Release|Any CPU 89 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Release|x64.Build.0 = Release|Any CPU 90 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Release|x86.ActiveCfg = Release|Any CPU 91 | {911AA52A-4E68-41C5-AB24-D1618AFDF753}.Release|x86.Build.0 = Release|Any CPU 92 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 93 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Debug|Any CPU.Build.0 = Debug|Any CPU 94 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Debug|ARM.ActiveCfg = Debug|Any CPU 95 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Debug|ARM.Build.0 = Debug|Any CPU 96 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Debug|x64.ActiveCfg = Debug|Any CPU 97 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Debug|x64.Build.0 = Debug|Any CPU 98 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Debug|x86.ActiveCfg = Debug|Any CPU 99 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Debug|x86.Build.0 = Debug|Any CPU 100 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Release|Any CPU.ActiveCfg = Release|Any CPU 101 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Release|Any CPU.Build.0 = Release|Any CPU 102 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Release|ARM.ActiveCfg = Release|Any CPU 103 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Release|ARM.Build.0 = Release|Any CPU 104 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Release|x64.ActiveCfg = Release|Any CPU 105 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Release|x64.Build.0 = Release|Any CPU 106 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Release|x86.ActiveCfg = Release|Any CPU 107 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891}.Release|x86.Build.0 = Release|Any CPU 108 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 109 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Debug|Any CPU.Build.0 = Debug|Any CPU 110 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Debug|ARM.ActiveCfg = Debug|Any CPU 111 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Debug|ARM.Build.0 = Debug|Any CPU 112 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Debug|x64.ActiveCfg = Debug|Any CPU 113 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Debug|x64.Build.0 = Debug|Any CPU 114 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Debug|x86.ActiveCfg = Debug|Any CPU 115 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Debug|x86.Build.0 = Debug|Any CPU 116 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Release|Any CPU.ActiveCfg = Release|Any CPU 117 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Release|Any CPU.Build.0 = Release|Any CPU 118 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Release|ARM.ActiveCfg = Release|Any CPU 119 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Release|ARM.Build.0 = Release|Any CPU 120 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Release|x64.ActiveCfg = Release|Any CPU 121 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Release|x64.Build.0 = Release|Any CPU 122 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Release|x86.ActiveCfg = Release|Any CPU 123 | {F835591F-9578-411E-908F-BB46FD4DB00C}.Release|x86.Build.0 = Release|Any CPU 124 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 125 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Debug|Any CPU.Build.0 = Debug|Any CPU 126 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Debug|ARM.ActiveCfg = Debug|Any CPU 127 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Debug|ARM.Build.0 = Debug|Any CPU 128 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Debug|x64.ActiveCfg = Debug|Any CPU 129 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Debug|x64.Build.0 = Debug|Any CPU 130 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Debug|x86.ActiveCfg = Debug|Any CPU 131 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Debug|x86.Build.0 = Debug|Any CPU 132 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Release|Any CPU.ActiveCfg = Release|Any CPU 133 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Release|Any CPU.Build.0 = Release|Any CPU 134 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Release|ARM.ActiveCfg = Release|Any CPU 135 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Release|ARM.Build.0 = Release|Any CPU 136 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Release|x64.ActiveCfg = Release|Any CPU 137 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Release|x64.Build.0 = Release|Any CPU 138 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Release|x86.ActiveCfg = Release|Any CPU 139 | {D6217688-8E4A-4092-8D78-C4D299F27D7B}.Release|x86.Build.0 = Release|Any CPU 140 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 141 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Debug|Any CPU.Build.0 = Debug|Any CPU 142 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Debug|ARM.ActiveCfg = Debug|Any CPU 143 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Debug|ARM.Build.0 = Debug|Any CPU 144 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Debug|x64.ActiveCfg = Debug|Any CPU 145 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Debug|x64.Build.0 = Debug|Any CPU 146 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Debug|x86.ActiveCfg = Debug|Any CPU 147 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Debug|x86.Build.0 = Debug|Any CPU 148 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Release|Any CPU.ActiveCfg = Release|Any CPU 149 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Release|Any CPU.Build.0 = Release|Any CPU 150 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Release|ARM.ActiveCfg = Release|Any CPU 151 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Release|ARM.Build.0 = Release|Any CPU 152 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Release|x64.ActiveCfg = Release|Any CPU 153 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Release|x64.Build.0 = Release|Any CPU 154 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Release|x86.ActiveCfg = Release|Any CPU 155 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E}.Release|x86.Build.0 = Release|Any CPU 156 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 157 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Debug|Any CPU.Build.0 = Debug|Any CPU 158 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Debug|ARM.ActiveCfg = Debug|Any CPU 159 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Debug|ARM.Build.0 = Debug|Any CPU 160 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Debug|x64.ActiveCfg = Debug|Any CPU 161 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Debug|x64.Build.0 = Debug|Any CPU 162 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Debug|x86.ActiveCfg = Debug|Any CPU 163 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Debug|x86.Build.0 = Debug|Any CPU 164 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Release|Any CPU.ActiveCfg = Release|Any CPU 165 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Release|Any CPU.Build.0 = Release|Any CPU 166 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Release|ARM.ActiveCfg = Release|Any CPU 167 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Release|ARM.Build.0 = Release|Any CPU 168 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Release|x64.ActiveCfg = Release|Any CPU 169 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Release|x64.Build.0 = Release|Any CPU 170 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Release|x86.ActiveCfg = Release|Any CPU 171 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887}.Release|x86.Build.0 = Release|Any CPU 172 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 173 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Debug|Any CPU.Build.0 = Debug|Any CPU 174 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Debug|ARM.ActiveCfg = Debug|Any CPU 175 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Debug|ARM.Build.0 = Debug|Any CPU 176 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Debug|x64.ActiveCfg = Debug|Any CPU 177 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Debug|x64.Build.0 = Debug|Any CPU 178 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Debug|x86.ActiveCfg = Debug|Any CPU 179 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Debug|x86.Build.0 = Debug|Any CPU 180 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Release|Any CPU.ActiveCfg = Release|Any CPU 181 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Release|Any CPU.Build.0 = Release|Any CPU 182 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Release|ARM.ActiveCfg = Release|Any CPU 183 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Release|ARM.Build.0 = Release|Any CPU 184 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Release|x64.ActiveCfg = Release|Any CPU 185 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Release|x64.Build.0 = Release|Any CPU 186 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Release|x86.ActiveCfg = Release|Any CPU 187 | {549E4C50-7B8F-4E18-92E1-60F036B3515F}.Release|x86.Build.0 = Release|Any CPU 188 | EndGlobalSection 189 | GlobalSection(SolutionProperties) = preSolution 190 | HideSolutionNode = FALSE 191 | EndGlobalSection 192 | GlobalSection(NestedProjects) = preSolution 193 | {513C7F7A-A758-4D48-94F4-891A00F63DA1} = {5A54DF18-E3F3-4929-876D-00650A15763E} 194 | {911AA52A-4E68-41C5-AB24-D1618AFDF753} = {DEA4A8C6-DE56-4359-A87C-472FB34132E7} 195 | {639CD744-1E61-4EF8-9E5A-9920A6BC3891} = {C39BE99E-D778-485B-920A-42D4EB3CBA75} 196 | {7D26E232-3D30-4151-BB0A-C5965419F268} = {C39BE99E-D778-485B-920A-42D4EB3CBA75} 197 | {F835591F-9578-411E-908F-BB46FD4DB00C} = {7D26E232-3D30-4151-BB0A-C5965419F268} 198 | {D6217688-8E4A-4092-8D78-C4D299F27D7B} = {DEA4A8C6-DE56-4359-A87C-472FB34132E7} 199 | {EBAA6D3C-F458-4375-99E7-33E22D8F677E} = {DEA4A8C6-DE56-4359-A87C-472FB34132E7} 200 | {F6B0977A-FAC6-4F9B-813A-C9D03F330887} = {DEA4A8C6-DE56-4359-A87C-472FB34132E7} 201 | {549E4C50-7B8F-4E18-92E1-60F036B3515F} = {DEA4A8C6-DE56-4359-A87C-472FB34132E7} 202 | EndGlobalSection 203 | GlobalSection(ExtensibilityGlobals) = postSolution 204 | SolutionGuid = {76E0A652-E5E2-4CA4-BAFD-AF6FDE0BD56A} 205 | EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.0\lib\NET35;packages\Unity.Interception.2.1.505.0\lib\NET35;packages\EnterpriseLibrary.Common.5.0.505.0\lib\NET35;packages\EnterpriseLibrary.ExceptionHandling.5.0.505.0\lib\NET35;packages\Unity.2.1.505.2\lib\NET35;packages\Unity.Interception.2.1.505.2\lib\NET35 206 | EndGlobalSection 207 | EndGlobal 208 | --------------------------------------------------------------------------------