forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonApiHttpAutofacConfigurator.cs
More file actions
68 lines (58 loc) · 2.47 KB
/
Copy pathJsonApiHttpAutofacConfigurator.cs
File metadata and controls
68 lines (58 loc) · 2.47 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Web.Http;
using Autofac;
using Autofac.Integration.WebApi;
using JSONAPI.Configuration;
namespace JSONAPI.Autofac
{
public class JsonApiHttpAutofacConfigurator
{
private readonly ILifetimeScope _lifetimeScope;
private Action<ContainerBuilder> _appLifetimeScopeCreating;
private Action<ILifetimeScope> _appLifetimeScopeBegunAction;
public JsonApiHttpAutofacConfigurator()
{
}
public JsonApiHttpAutofacConfigurator(ILifetimeScope lifetimeScope)
{
_lifetimeScope = lifetimeScope;
}
public void OnApplicationLifetimeScopeCreating(Action<ContainerBuilder> appLifetimeScopeCreating)
{
_appLifetimeScopeCreating = appLifetimeScopeCreating;
}
public void OnApplicationLifetimeScopeBegun(Action<ILifetimeScope> appLifetimeScopeBegunAction)
{
_appLifetimeScopeBegunAction = appLifetimeScopeBegunAction;
}
public void Apply(HttpConfiguration httpConfiguration, IJsonApiConfiguration jsonApiConfiguration)
{
ILifetimeScope applicationLifetimeScope;
if (_lifetimeScope == null)
{
var builder = new ContainerBuilder();
ConfigureApplicationLifetimeScope(jsonApiConfiguration, builder);
applicationLifetimeScope = builder.Build();
}
else
{
applicationLifetimeScope = _lifetimeScope.BeginLifetimeScope(containerBuilder =>
{
ConfigureApplicationLifetimeScope(jsonApiConfiguration, containerBuilder);
});
}
if (_appLifetimeScopeBegunAction != null)
_appLifetimeScopeBegunAction(applicationLifetimeScope);
var jsonApiHttpConfiguration = applicationLifetimeScope.Resolve<JsonApiHttpConfiguration>();
jsonApiHttpConfiguration.Apply(httpConfiguration);
httpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(applicationLifetimeScope);
}
private void ConfigureApplicationLifetimeScope(IJsonApiConfiguration jsonApiConfiguration, ContainerBuilder containerBuilder)
{
var module = new JsonApiAutofacModule(jsonApiConfiguration);
containerBuilder.RegisterModule(module);
if (_appLifetimeScopeCreating != null)
_appLifetimeScopeCreating(containerBuilder);
}
}
}