Skip to content

Instantly share code, notes, and snippets.

@dls314
Created October 31, 2016 15:45
Show Gist options
  • Save dls314/d1a15aec406c8f514a8eba9c7c1960d2 to your computer and use it in GitHub Desktop.
Save dls314/d1a15aec406c8f514a8eba9c7c1960d2 to your computer and use it in GitHub Desktop.
Test.AutofacStartupIntegration
using System;
using System.IO;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Test.AutofacStartupIntegration
{
public static class AutofacExtensions
{
public static IServiceCollection AddAutofac(this IServiceCollection services)
{
return services.AddSingleton<IServiceProviderFactory<ContainerBuilder>, AutofacServiceProviderFactory>();
}
public static IServiceCollection AddAutofac(this IServiceCollection services, ILifetimeScope lifetimeScope)
{
return
services.AddSingleton<IServiceProviderFactory<ContainerBuilder>, ScopedAutofacServiceProviderFactory>(
serviceProvider => new ScopedAutofacServiceProviderFactory(lifetimeScope));
}
public static IWebHostBuilder UseAutofac(this IWebHostBuilder builder)
{
return builder.ConfigureServices(services => services.AddAutofac());
}
public static IWebHostBuilder UseAutofac(this IWebHostBuilder builder, ILifetimeScope lifetimeScope)
{
return builder.ConfigureServices(services => services.AddAutofac(lifetimeScope));
}
private class ScopedAutofacServiceProviderFactory : IServiceProviderFactory<ContainerBuilder>
{
private readonly ILifetimeScope _lifetimeScope;
public ScopedAutofacServiceProviderFactory(ILifetimeScope lifetimeScope)
{
_lifetimeScope = lifetimeScope;
}
public ContainerBuilder CreateBuilder(IServiceCollection services)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(services);
return containerBuilder;
}
public IServiceProvider CreateServiceProvider(ContainerBuilder builder)
{
builder.Update(_lifetimeScope.ComponentRegistry);
return new AutofacServiceProvider(_lifetimeScope);
}
}
private class AutofacServiceProviderFactory : IServiceProviderFactory<ContainerBuilder>
{
public ContainerBuilder CreateBuilder(IServiceCollection services)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(services);
return containerBuilder;
}
public IServiceProvider CreateServiceProvider(ContainerBuilder builder)
{
return new AutofacServiceProvider(builder.Build());
}
}
}
public interface IFoo
{
}
public class Foo : IFoo
{
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void ConfigureContainer(ContainerBuilder containerBuilder)
{
containerBuilder.RegisterType<Foo>().AsImplementedInterfaces().SingleInstance();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
Lazy<IFoo> foo)
{
loggerFactory.AddConsole();
app.Run(async (context) =>
{
await context.Response.WriteAsync($"Hello World! {foo.Value.GetHashCode()}");
});
}
}
public class Program
{
public static void Main(string[] args)
{
try
{
using (var container = new ContainerBuilder().Build())
{
var host = new WebHostBuilder()
.UseKestrel()
//.UseAutofac() // this works
.UseAutofac(container) // this also works
//.UseAutofac(container.BeginLifetimeScope()) // this does not work, and I don't know why
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run(); // non-working case throws here with Autofac.Core.DependencyResolutionException
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
}
{
"dependencies": {
"Autofac": "4.1.1",
"Autofac.Extensions.DependencyInjection": "4.0.0",
"Microsoft.AspNetCore.Diagnostics": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0-preview1-final",
"Microsoft.Extensions.DependencyInjection": "1.1.0-preview1-final",
"Microsoft.Extensions.Logging.Console": "1.1.0-preview1-final"
},
"tools": {
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
}
},
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment