├── .gitattributes ├── .gitignore └── src └── ServiceProviderFactorySample ├── Extensions ├── AutofacExtensions.cs └── StructureMapExtensions.cs ├── Program.cs ├── Properties └── launchSettings.json ├── ServiceProviderFactorySample.csproj └── Startup.cs /.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 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | [Oo]bj/ 2 | [Bb]in/ 3 | TestResults/ 4 | .nuget/ 5 | _ReSharper.*/ 6 | packages/ 7 | artifacts/ 8 | PublishProfiles/ 9 | *.user 10 | *.suo 11 | *.cache 12 | *.docstates 13 | _ReSharper.* 14 | nuget.exe 15 | *net45.csproj 16 | *net451.csproj 17 | *k10.csproj 18 | *.psess 19 | *.vsp 20 | *.pidb 21 | *.userprefs 22 | *DS_Store 23 | *.ncrunchsolution 24 | *.*sdf 25 | *.ipch 26 | *.sln.ide 27 | project.lock.json 28 | /.vs/ 29 | .build/ 30 | .testPublish/ 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /src/ServiceProviderFactorySample/Extensions/AutofacExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Autofac; 3 | using Autofac.Extensions.DependencyInjection; 4 | using Microsoft.AspNetCore.Hosting; 5 | using Microsoft.Extensions.DependencyInjection; 6 | 7 | namespace ServiceProviderFactorySample 8 | { 9 | public static class AutofacExtensions 10 | { 11 | public static IServiceCollection AddAutofac(this IServiceCollection services) 12 | { 13 | return services.AddSingleton, AutofacServiceProviderFactory>(); 14 | } 15 | 16 | public static IWebHostBuilder UseAutofac(this IWebHostBuilder builder) 17 | { 18 | return builder.ConfigureServices(services => services.AddAutofac()); 19 | } 20 | 21 | private class AutofacServiceProviderFactory : IServiceProviderFactory 22 | { 23 | public ContainerBuilder CreateBuilder(IServiceCollection services) 24 | { 25 | var containerBuilder = new ContainerBuilder(); 26 | 27 | containerBuilder.Populate(services); 28 | 29 | return containerBuilder; 30 | } 31 | 32 | public IServiceProvider CreateServiceProvider(ContainerBuilder builder) 33 | { 34 | return new AutofacServiceProvider(builder.Build()); 35 | } 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/ServiceProviderFactorySample/Extensions/StructureMapExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.DependencyInjection; 4 | using StructureMap; 5 | 6 | namespace ServiceProviderFactorySample 7 | { 8 | public static class StructureMapExtensions 9 | { 10 | public static IServiceCollection AddStructureMap(this IServiceCollection services, Action configure = null) 11 | { 12 | return services.AddSingleton>(new StructureMapServiceProviderFactory(configure)); 13 | } 14 | 15 | public static IWebHostBuilder UseStructureMap(this IWebHostBuilder builder, Action configure = null) 16 | { 17 | return builder.ConfigureServices(services => services.AddStructureMap(configure)); 18 | } 19 | 20 | private class StructureMapServiceProviderFactory : IServiceProviderFactory 21 | { 22 | public StructureMapServiceProviderFactory(Action configure) 23 | { 24 | Configure = configure ?? (config => { }); 25 | } 26 | 27 | private Action Configure { get; } 28 | 29 | public IContainer CreateBuilder(IServiceCollection services) 30 | { 31 | var container = new Container(Configure); 32 | 33 | container.Populate(services); 34 | 35 | return container; 36 | } 37 | 38 | public IServiceProvider CreateServiceProvider(IContainer container) 39 | { 40 | return container.GetInstance(); 41 | } 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /src/ServiceProviderFactorySample/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | 5 | namespace ServiceProviderFactorySample 6 | { 7 | public class Program 8 | { 9 | public static void Main(string[] args) 10 | { 11 | BuildWebHost(args).Run(); 12 | } 13 | 14 | public static IWebHost BuildWebHost(string[] args) => 15 | WebHost.CreateDefaultBuilder(args) 16 | .UseStructureMap() 17 | .UseStartup() 18 | .Build(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/ServiceProviderFactorySample/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:4634/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "environmentVariables": { 15 | "ASPNETCORE_ENVIRONMENT": "Development" 16 | } 17 | }, 18 | "ServiceProviderFactorySample": { 19 | "commandName": "Project", 20 | "launchBrowser": true, 21 | "launchUrl": "http://localhost:5000", 22 | "environmentVariables": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/ServiceProviderFactorySample/ServiceProviderFactorySample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.0 5 | true 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/ServiceProviderFactorySample/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Http; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using StructureMap; 6 | using StructureMap.Pipeline; 7 | 8 | namespace ServiceProviderFactorySample 9 | { 10 | public class Startup 11 | { 12 | public void ConfigureServices(IServiceCollection services) 13 | { 14 | // Add services here that have extension methods on IServiceCollection 15 | } 16 | 17 | public void ConfigureContainer(Registry config) 18 | { 19 | // Get rid of the nested closure for the user 20 | config.ForSingletonOf().Use(); 21 | 22 | // Use StructureMap's own type scanning functionality 23 | config.Scan(_ => { 24 | _.TheCallingAssembly(); 25 | _.WithDefaultConventions(); 26 | }); 27 | } 28 | 29 | public void Configure(IApplicationBuilder app, Lazy foo) 30 | { 31 | app.Run(async (context) => 32 | { 33 | await context.Response.WriteAsync($"Hello World! {foo.Value.GetHashCode()}"); 34 | }); 35 | } 36 | 37 | public class Foo : IFoo 38 | { 39 | 40 | } 41 | 42 | public interface IFoo 43 | { 44 | 45 | } 46 | } 47 | } 48 | --------------------------------------------------------------------------------