Skip to content

Instantly share code, notes, and snippets.

@dls314
Created October 31, 2016 16:48
Show Gist options
  • Save dls314/727f802382604b128a8ba75ef6db85b6 to your computer and use it in GitHub Desktop.
Save dls314/727f802382604b128a8ba75ef6db85b6 to your computer and use it in GitHub Desktop.
Test.OldAutofacStartupIntegration
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.OldAutofacStartupIntegration
{
public interface IFoo
{
}
public class Foo : IFoo
{
}
public class Startup
{
public static ILifetimeScope LifetimeScope { get; set; }
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<Foo>().AsImplementedInterfaces().SingleInstance();
containerBuilder.Populate(services);
containerBuilder.Update(LifetimeScope.ComponentRegistry);
return LifetimeScope.Resolve<IServiceProvider>();
}
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())
{
Startup.LifetimeScope = container; // this works
//Startup.LifetimeScope = container.BeginLifetimeScope(containerBuilder => { }); // this does not work, and I don't know why
var host = new WebHostBuilder()
.UseKestrel()
.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