Skip to content

Commit 5a74ca8

Browse files
RaymondHuyalexmg
authored andcommitted
Fixes #1041 - PropertiesAutowired fails when service is decorated using new syntax (#1043)
* Raise activating events for new instance before it is decorated * Add test cases for issue#1041 * Remove unused property
1 parent 9eee0ea commit 5a74ca8

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Autofac/Core/Resolving/InstanceLookup.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ private object Activate(IEnumerable<Parameter> parameters, out object decoratorT
121121
{
122122
decoratorTarget = _newInstance = ComponentRegistration.Activator.ActivateInstance(this, resolveParameters);
123123

124+
ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);
125+
124126
_newInstance = InstanceDecorator.TryDecorateRegistration(
125127
_service,
126128
ComponentRegistration,
@@ -153,7 +155,8 @@ private object Activate(IEnumerable<Parameter> parameters, out object decoratorT
153155
}
154156
}
155157

156-
ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);
158+
if (_newInstance != decoratorTarget)
159+
ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);
157160

158161
return _newInstance;
159162
}

test/Autofac.Test/Features/Decorators/DecoratorTests.cs

+64
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@ private interface IService
1515
{
1616
}
1717

18+
private interface IAutoWiredService
19+
{
20+
bool NestedServiceIsNotNull();
21+
}
22+
23+
private class NestedService
24+
{
25+
}
26+
27+
private class AutoWiredService : IAutoWiredService
28+
{
29+
public NestedService NestedService { get; set; }
30+
31+
public bool NestedServiceIsNotNull()
32+
{
33+
return NestedService != null;
34+
}
35+
}
36+
37+
private class AutoWiredServiceDecorator : IAutoWiredService
38+
{
39+
private readonly IAutoWiredService _original;
40+
41+
public AutoWiredServiceDecorator(IAutoWiredService original)
42+
{
43+
_original = original;
44+
}
45+
46+
public bool NestedServiceIsNotNull()
47+
{
48+
return _original.NestedServiceIsNotNull();
49+
}
50+
}
51+
1852
public class Foo
1953
{
2054
}
@@ -149,6 +183,36 @@ public void RegistrationTargetsTheImplementationType()
149183
Assert.Equal(typeof(ImplementorA), registration.Target.Activator.LimitType);
150184
}
151185

186+
[Fact]
187+
public void DecorateReflectionActivatorWithPropertyInjection()
188+
{
189+
var builder = new ContainerBuilder();
190+
builder.RegisterType<NestedService>();
191+
builder.RegisterType<AutoWiredService>().As<IAutoWiredService>().PropertiesAutowired();
192+
builder.RegisterDecorator<AutoWiredServiceDecorator, IAutoWiredService>();
193+
194+
var container = builder.Build();
195+
196+
var service = container.Resolve<IAutoWiredService>();
197+
198+
Assert.True(service.NestedServiceIsNotNull());
199+
}
200+
201+
[Fact]
202+
public void DecorateProvidedInstanceActivatorWithPropertyInjection()
203+
{
204+
var builder = new ContainerBuilder();
205+
builder.RegisterType<NestedService>();
206+
builder.RegisterInstance<IAutoWiredService>(new AutoWiredService()).PropertiesAutowired();
207+
builder.RegisterDecorator<AutoWiredServiceDecorator, IAutoWiredService>();
208+
209+
var container = builder.Build();
210+
211+
var service = container.Resolve<IAutoWiredService>();
212+
213+
Assert.True(service.NestedServiceIsNotNull());
214+
}
215+
152216
private abstract class Decorator : IDecoratedService
153217
{
154218
protected Decorator(IDecoratedService decorated)

0 commit comments

Comments
 (0)