@@ -9,25 +9,44 @@ namespace JSONAPI.Autofac
99 public class JsonApiHttpAutofacConfigurator
1010 {
1111 private readonly ILifetimeScope _lifetimeScope ;
12+ private Action < ContainerBuilder > _appLifetimeScopeCreating ;
1213 private Action < ILifetimeScope > _appLifetimeScopeBegunAction ;
1314
15+ public JsonApiHttpAutofacConfigurator ( )
16+ {
17+ }
18+
1419 public JsonApiHttpAutofacConfigurator ( ILifetimeScope lifetimeScope )
1520 {
1621 _lifetimeScope = lifetimeScope ;
1722 }
1823
24+ public void OnApplicationLifetimeScopeCreating ( Action < ContainerBuilder > appLifetimeScopeCreating )
25+ {
26+ _appLifetimeScopeCreating = appLifetimeScopeCreating ;
27+ }
28+
1929 public void OnApplicationLifetimeScopeBegun ( Action < ILifetimeScope > appLifetimeScopeBegunAction )
2030 {
2131 _appLifetimeScopeBegunAction = appLifetimeScopeBegunAction ;
2232 }
2333
2434 public void Apply ( HttpConfiguration httpConfiguration , IJsonApiConfiguration jsonApiConfiguration )
2535 {
26- var applicationLifetimeScope = _lifetimeScope . BeginLifetimeScope ( containerBuilder =>
36+ ILifetimeScope applicationLifetimeScope ;
37+ if ( _lifetimeScope == null )
2738 {
28- var module = new JsonApiAutofacModule ( jsonApiConfiguration ) ;
29- containerBuilder . RegisterModule ( module ) ;
30- } ) ;
39+ var builder = new ContainerBuilder ( ) ;
40+ ConfigureApplicationLifetimeScope ( jsonApiConfiguration , builder ) ;
41+ applicationLifetimeScope = builder . Build ( ) ;
42+ }
43+ else
44+ {
45+ applicationLifetimeScope = _lifetimeScope . BeginLifetimeScope ( containerBuilder =>
46+ {
47+ ConfigureApplicationLifetimeScope ( jsonApiConfiguration , containerBuilder ) ;
48+ } ) ;
49+ }
3150
3251 if ( _appLifetimeScopeBegunAction != null )
3352 _appLifetimeScopeBegunAction ( applicationLifetimeScope ) ;
@@ -36,5 +55,14 @@ public void Apply(HttpConfiguration httpConfiguration, IJsonApiConfiguration jso
3655 jsonApiHttpConfiguration . Apply ( httpConfiguration ) ;
3756 httpConfiguration . DependencyResolver = new AutofacWebApiDependencyResolver ( applicationLifetimeScope ) ;
3857 }
58+
59+ private void ConfigureApplicationLifetimeScope ( IJsonApiConfiguration jsonApiConfiguration , ContainerBuilder containerBuilder )
60+ {
61+ var module = new JsonApiAutofacModule ( jsonApiConfiguration ) ;
62+ containerBuilder . RegisterModule ( module ) ;
63+
64+ if ( _appLifetimeScopeCreating != null )
65+ _appLifetimeScopeCreating ( containerBuilder ) ;
66+ }
3967 }
4068}
0 commit comments