Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static IApplicationBuilder UseSentry(this IApplicationBuilder app)
{
o.UseStackTraceFactory(stackTraceFactory);
}

o.AddEventProcessorProvider(() => app.ApplicationServices.GetNonScoped<ISentryEventProcessor>());
o.AddExceptionProcessorProvider(() => app.ApplicationServices.GetNonScoped<ISentryEventExceptionProcessor>());
o.AddTransactionProcessorProvider(() => app.ApplicationServices.GetNonScoped<ISentryTransactionProcessor>());
}

var lifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();
Expand Down
36 changes: 36 additions & 0 deletions src/Sentry.AspNetCore/LifetimeServiceResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Extensions.DependencyInjection;

namespace Sentry.AspNetCore;

internal class LifetimeServiceResolver(IServiceCollection services)
{
public IEnumerable<T> GetServices<T>(IServiceProvider provider,
params ServiceLifetime[] lifetimes)
{
return Factories<T>(lifetimes)
.Select(factory => factory(provider));
}

private IEnumerable<Func<IServiceProvider, T>> Factories<T>(ServiceLifetime[] lifetimes)
{
foreach (var descriptor in services)
{
if (descriptor.ServiceType != typeof(T) || !lifetimes.Contains(descriptor.Lifetime))
{
continue;
}
if (descriptor.ImplementationInstance is not null)
{
yield return _ => (T)descriptor.ImplementationInstance;
}
if (descriptor.ImplementationFactory is not null)
{
yield return provider => (T)descriptor.ImplementationFactory(provider);
}
if (descriptor.ImplementationType is not null)
{
yield return provider => (T)ActivatorUtilities.GetServiceOrCreateInstance(provider, descriptor.ImplementationType);
}
}
}
}
2 changes: 2 additions & 0 deletions src/Sentry.AspNetCore/SentryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ private void SyncOptionsScope(Scope scope)

internal void PopulateScope(HttpContext context, Scope scope)
{
// Singleton and Transient processors might already have been registered in the ApplicationBuilderExtensions.
// Here we ensure any Scoped processors are also resolved
scope.AddEventProcessors(_eventProcessors.Except(scope.GetAllEventProcessors()));
scope.AddExceptionProcessors(_eventExceptionProcessors.Except(scope.GetAllExceptionProcessors()));
scope.AddTransactionProcessors(_transactionProcessors.Except(scope.GetAllTransactionProcessors()));
Expand Down
3 changes: 2 additions & 1 deletion src/Sentry.AspNetCore/SentryWebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public static IWebHostBuilder UseSentry(
});

_ = builder.ConfigureServices(c => _ =
c.AddTransient<IStartupFilter, SentryStartupFilter>()
c.AddSingleton(new LifetimeServiceResolver(c))
.AddTransient<IStartupFilter, SentryStartupFilter>()
.AddTransient<IStartupFilter, SentryTracingStartupFilter>()
.AddTransient<SentryMiddleware>()
);
Expand Down
17 changes: 17 additions & 0 deletions src/Sentry.AspNetCore/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Sentry.AspNetCore;

internal static class ServiceProviderExtensions
{
public static IEnumerable<T> GetServices<T>(this IServiceProvider serviceProvider, ServiceLifetime[] lifetimes) =>
serviceProvider
.GetRequiredService<LifetimeServiceResolver>()
.GetServices<T>(serviceProvider, lifetimes);

public static IEnumerable<T> GetNonScoped<T>(this IServiceProvider serviceProvider) =>
serviceProvider
.GetRequiredService<LifetimeServiceResolver>()
.GetServices<T>(serviceProvider, [ServiceLifetime.Singleton, ServiceLifetime.Transient]);
}
2 changes: 1 addition & 1 deletion src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ public void AddTransactionProcessor(ISentryTransactionProcessor processor)
{
if (TransactionProcessors == null)
{
TransactionProcessors = new() { processor };
TransactionProcessors = [processor];
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Sentry.AspNetCore.Tests;

public class LifetimeSpecificServiceResolversTests
{
private interface IMyInterface { }
private class ServiceA : IMyInterface { }
private class ServiceB : IMyInterface { }
private class ServiceC : IMyInterface { }
private class ServiceD : IMyInterface { }

private class ServiceE(Foo foo) : IMyInterface
{
public void Foo() => foo.Bar();
}

private class Foo
{
public void Bar() => Console.WriteLine("bar");
}

[Fact]
public void ShouldResolveSingletonDependencies()
{
// Arrange
var serviceCollection = new ServiceCollection();

// Register services with various lifetimes
serviceCollection.AddSingleton<IMyInterface>(new ServiceA());
serviceCollection.AddTransient<IMyInterface, ServiceB>();
serviceCollection.AddSingleton<IMyInterface>(_ => new ServiceC());

serviceCollection.AddSingleton(new LifetimeServiceResolver(serviceCollection));

var serviceProvider = serviceCollection.BuildServiceProvider();

// Act
var instances = serviceProvider
.GetServices<IMyInterface>([ServiceLifetime.Singleton])
.ToArray();

// Assert
using (new AssertionScope())
{
instances.Length.Should().Be(2);
instances.Should().Contain(x => x.GetType() == typeof(ServiceA));
instances.Should().Contain(x => x.GetType() == typeof(ServiceC));
}
}

[Fact]
public void ShouldResolveTransientDependencies()
{
// Arrange
var serviceCollection = new ServiceCollection();

// Register services with various lifetimes
serviceCollection.AddTransient<IMyInterface, ServiceA>();
serviceCollection.AddScoped<IMyInterface>(_ => new ServiceB());
serviceCollection.AddTransient<IMyInterface>(_ => new ServiceC());
serviceCollection.AddSingleton<IMyInterface>(new ServiceD());

serviceCollection.AddSingleton(new LifetimeServiceResolver(serviceCollection));

var serviceProvider = serviceCollection.BuildServiceProvider();

// Act
var instances = serviceProvider
.GetServices<IMyInterface>([ServiceLifetime.Transient])
.ToArray();

// Assert
using (new AssertionScope())
{
instances.Length.Should().Be(2);
instances.Should().Contain(x => x.GetType() == typeof(ServiceA));
instances.Should().Contain(x => x.GetType() == typeof(ServiceC));
}
}

[Fact]
public void ShouldResolveScopedDependencies()
{
// Arrange
var serviceCollection = new ServiceCollection();

// Register services with various lifetimes
serviceCollection.AddScoped<Foo>();
serviceCollection.AddScoped<IMyInterface, ServiceE>();
serviceCollection.AddScoped<IMyInterface>(_ => new ServiceB());
serviceCollection.AddTransient<IMyInterface>(_ => new ServiceC());
serviceCollection.AddSingleton<IMyInterface>(new ServiceD());

serviceCollection.AddSingleton(new LifetimeServiceResolver(serviceCollection));

var serviceProvider = serviceCollection.BuildServiceProvider();

// Act
var instances = serviceProvider
.GetServices<IMyInterface>([ServiceLifetime.Scoped])
.ToArray();

// Assert
using (new AssertionScope())
{
instances.Length.Should().Be(2);
instances.Should().Contain(x => x.GetType() == typeof(ServiceE));
instances.Should().Contain(x => x.GetType() == typeof(ServiceB));
}
}
}