Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: autofac/Autofac
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e76a8d39b158225ad5d046ae9487e252f9da03fd
Choose a base ref
..
head repository: autofac/Autofac
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 22d63973edaabb72506171f198b41124b805964d
Choose a head ref
Original file line number Diff line number Diff line change
@@ -386,7 +386,7 @@ public IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnPrepar

ResolvePipeline.Use(nameof(OnPreparing), PipelinePhase.ParameterSelection, (ctxt, next) =>
{
var args = new PreparingEventArgs(ctxt, ctxt.Registration, ctxt.Parameters);
var args = new PreparingEventArgs(ctxt, ctxt.Service, ctxt.Registration, ctxt.Parameters);

handler(args);

@@ -414,7 +414,7 @@ public IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActiva
{
next(ctxt);

var args = new ActivatingEventArgs<TLimit>(ctxt, ctxt.Registration, ctxt.Parameters, (TLimit)ctxt.Instance!);
var args = new ActivatingEventArgs<TLimit>(ctxt, ctxt.Service, ctxt.Registration, ctxt.Parameters, (TLimit)ctxt.Instance!);

handler(args);
ctxt.Instance = args.Instance;
@@ -452,7 +452,7 @@ public IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActiva
ctxt.RequestCompleting += (sender, evArgs) =>
{
var ctxt = evArgs.RequestContext;
var args = new ActivatedEventArgs<TLimit>(ctxt, ctxt.Registration, ctxt.Parameters, newInstance);
var args = new ActivatedEventArgs<TLimit>(ctxt, ctxt.Service, ctxt.Registration, ctxt.Parameters, newInstance);

handler(args);
};
15 changes: 14 additions & 1 deletion src/Autofac/Core/ActivatedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -40,19 +40,32 @@ public class ActivatedEventArgs<T> : EventArgs, IActivatedEventArgs<T>
/// <param name="component">The component.</param>
/// <param name="parameters">The parameters.</param>
/// <param name="instance">The instance.</param>
public ActivatedEventArgs(IComponentContext context, IComponentRegistration component, IEnumerable<Parameter> parameters, T instance)
/// <param name="service">The service being resolved.</param>
public ActivatedEventArgs(
IComponentContext context,
Service service,
IComponentRegistration component,
IEnumerable<Parameter> parameters,
T instance)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (component == null) throw new ArgumentNullException(nameof(component));
if (parameters == null) throw new ArgumentNullException(nameof(parameters));
if (instance == null) throw new ArgumentNullException(nameof(instance));
if (service == null) throw new ArgumentNullException(nameof(service));

Context = context;
Component = component;
Parameters = parameters;
Instance = instance;
Service = service;
}

/// <summary>
/// Gets the service being resolved.
/// </summary>
public Service Service { get; }

/// <summary>
/// Gets the context in which the activation occurred.
/// </summary>
9 changes: 8 additions & 1 deletion src/Autofac/Core/ActivatingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -41,22 +41,29 @@ public class ActivatingEventArgs<T> : EventArgs, IActivatingEventArgs<T>
/// Initializes a new instance of the <see cref="ActivatingEventArgs{T}"/> class.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="service">The service.</param>
/// <param name="component">The component.</param>
/// <param name="parameters">The parameters.</param>
/// <param name="instance">The instance.</param>
public ActivatingEventArgs(IComponentContext context, IComponentRegistration component, IEnumerable<Parameter> parameters, T instance)
public ActivatingEventArgs(IComponentContext context, Service service, IComponentRegistration component, IEnumerable<Parameter> parameters, T instance)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (component == null) throw new ArgumentNullException(nameof(component));
if (parameters == null) throw new ArgumentNullException(nameof(parameters));
if (instance == null) throw new ArgumentNullException(nameof(instance));

Service = service;
Context = context;
Component = component;
Parameters = parameters;
_instance = instance;
}

/// <summary>
/// Gets the service being resolved.
/// </summary>
public Service Service { get; }

/// <summary>
/// Gets the context in which the activation occurred.
/// </summary>
5 changes: 5 additions & 0 deletions src/Autofac/Core/IActivatedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -34,6 +34,11 @@ namespace Autofac.Core
[SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
public interface IActivatedEventArgs<out T>
{
/// <summary>
/// Gets the service being resolved.
/// </summary>
Service Service { get; }

/// <summary>
/// Gets the context in which the activation occurred.
/// </summary>
5 changes: 5 additions & 0 deletions src/Autofac/Core/IActivatingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -35,6 +35,11 @@ namespace Autofac.Core
[SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
public interface IActivatingEventArgs<out T>
{
/// <summary>
/// Gets the service being resolved.
/// </summary>
Service Service { get; }

/// <summary>
/// Gets the context in which the activation occurred.
/// </summary>
9 changes: 8 additions & 1 deletion src/Autofac/Core/PreparingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -39,20 +39,27 @@ public class PreparingEventArgs : EventArgs
/// <summary>
/// Initializes a new instance of the <see cref="PreparingEventArgs"/> class.
/// </summary>
/// <param name="service">The service being resolved.</param>
/// <param name="context">The context.</param>
/// <param name="component">The component.</param>
/// <param name="parameters">The parameters.</param>
public PreparingEventArgs(IComponentContext context, IComponentRegistration component, IEnumerable<Parameter> parameters)
public PreparingEventArgs(IComponentContext context, Service service, IComponentRegistration component, IEnumerable<Parameter> parameters)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (component == null) throw new ArgumentNullException(nameof(component));
if (parameters == null) throw new ArgumentNullException(nameof(parameters));

Context = context;
Service = service;
Component = component;
_parameters = parameters;
}

/// <summary>
/// Gets the service being resolved.
/// </summary>
public Service Service { get; }

/// <summary>
/// Gets the context in which the activation is occurring.
/// </summary>
24 changes: 24 additions & 0 deletions test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs
Original file line number Diff line number Diff line change
@@ -430,6 +430,30 @@ public void ReleaseStopsAutomaticDisposal()
Assert.False(dt.IsDisposed);
}

[Fact]
public void EventRaisedFromComponentRegistrationCanGetServiceBeingResolved()
{
var builder = new ContainerBuilder();
builder.RegisterType<MethodInjection>()
.OnPreparing((e) =>
{
var service = e.Service as IServiceWithType;
Assert.Equal(typeof(MethodInjection), service.ServiceType);
})
.OnActivating((e) =>
{
var service = e.Service as IServiceWithType;
Assert.Equal(typeof(MethodInjection), service.ServiceType);
})
.OnActivated((e) =>
{
var service = e.Service as IServiceWithType;
Assert.Equal(typeof(MethodInjection), service.ServiceType);
});

builder.Build().Resolve<MethodInjection>();
}

[Fact]
public void OnReleaseForSingletonStillFiresIfNotResolved()
{