forked from autofac/Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
45 lines (41 loc) · 1.97 KB
/
Copy pathProgram.cs
File metadata and controls
45 lines (41 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace GenericHostBuilderExample
{
internal static class Program
{
public static async Task Main(string[] args)
{
// The `UseServiceProviderFactory(new AutofacServiceProviderFactory())` call here allows for
// ConfigureContainer to be supported with
// a strongly-typed ContainerBuilder. If you don't
// have the call to `UseServiceProviderFactory(new AutofacServiceProviderFactory())` here, you won't get
// ConfigureContainer support. This also automatically
// calls Populate to put services you register during
// ConfigureServices into Autofac.
await Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(ConfigureServices)
.ConfigureContainer<ContainerBuilder>(ConfigureContainer)
.RunConsoleAsync();
}
private static void ConfigureServices(IServiceCollection services)
// Use extensions from libraries to register services in the
// collection. These will be automatically added to the
// Autofac container.
=> services.AddHostedService<HostedService>();
private static void ConfigureContainer(ContainerBuilder containerBuilder)
{
// Add any Autofac modules or registrations.
// This is called AFTER ConfigureServices so things you
// register here OVERRIDE things registered in ConfigureServices.
//
// You must have the call to `UseServiceProviderFactory(new AutofacServiceProviderFactory())`
// when building the host or this won't be called.
containerBuilder.RegisterType<Logger>().As<ILogger>();
}
}
}