Commit 4d28864 1 parent 493b020 commit 4d28864 Copy full SHA for 4d28864
File tree 2 files changed +44
-8
lines changed
src/Autofac/Core/Resolving
2 files changed +44
-8
lines changed Original file line number Diff line number Diff line change @@ -96,14 +96,11 @@ private void StartStartableComponent(object instance)
96
96
&& ! ComponentRegistration . Metadata . ContainsKey ( MetadataKeys . AutoActivated )
97
97
&& ComponentRegistry . Properties . ContainsKey ( MetadataKeys . StartOnActivatePropertyKey ) )
98
98
{
99
- try
100
- {
101
- startable . Start ( ) ;
102
- }
103
- finally
104
- {
105
- ComponentRegistration . Metadata [ MetadataKeys . AutoActivated ] = true ;
106
- }
99
+ // Issue #916: Set the startable as "done starting" BEFORE calling Start
100
+ // so you don't get a StackOverflow if the component creates a child scope
101
+ // during Start. You don't want the startable trying to activate itself.
102
+ ComponentRegistration . Metadata [ MetadataKeys . AutoActivated ] = true ;
103
+ startable . Start ( ) ;
107
104
}
108
105
}
109
106
Original file line number Diff line number Diff line change @@ -51,6 +51,15 @@ public void WhenStartIsSpecified_StartableComponentsAreStarted()
51
51
Assert . True ( startable . StartCount > 0 ) ;
52
52
}
53
53
54
+ [ Fact ]
55
+ public void WhenStartableCreatesChildScope_NoExceptionIsThrown ( )
56
+ {
57
+ // Issue #916
58
+ var builder = new ContainerBuilder ( ) ;
59
+ builder . RegisterType < StartableCreatesLifetimeScope > ( ) . As < IStartable > ( ) . SingleInstance ( ) ;
60
+ var container = builder . Build ( ) ;
61
+ }
62
+
54
63
[ Theory ]
55
64
[ InlineData ( true ) ]
56
65
[ InlineData ( false ) ]
@@ -139,6 +148,36 @@ public void Start()
139
148
}
140
149
}
141
150
151
+ // Issue #916
152
+ private class StartableCreatesLifetimeScope : IStartable
153
+ {
154
+ private readonly ILifetimeScope _scope ;
155
+
156
+ public StartableCreatesLifetimeScope ( ILifetimeScope scope )
157
+ {
158
+ this . _scope = scope ;
159
+ }
160
+
161
+ public void Start ( )
162
+ {
163
+ using ( var nested = this . _scope . BeginLifetimeScope ( "tag" , b => { } ) )
164
+ {
165
+ }
166
+
167
+ using ( var nested = this . _scope . BeginLifetimeScope ( b => { } ) )
168
+ {
169
+ }
170
+
171
+ using ( var nested = this . _scope . BeginLifetimeScope ( "tag" ) )
172
+ {
173
+ }
174
+
175
+ using ( var nested = this . _scope . BeginLifetimeScope ( ) )
176
+ {
177
+ }
178
+ }
179
+ }
180
+
142
181
private class StartableDependency : IStartableDependency
143
182
{
144
183
private static int _count = 0 ;
You can’t perform that action at this time.
0 commit comments