Skip to content

Unable to use ISentryEventExceptionProcessor with Scoped DI #318

@udlose

Description

@udlose

Framework used: ASP.NET Core

Runtime used: .NET Core Version: 2.2.7

NuGet packages used: Sentry.AspNetCore Version: 1.2.0

I get a Startup error with the following exception when I register ISentryEventExceptionProcessor as a Scoped instance. This works correctly if I register it as Transient or Singleton.

Exception:

 An error occurred while starting the application.
 InvalidOperationException: Cannot resolve 'System.Collections.Generic.IEnumerable`1[Sentry.Extensibility.ISentryEventExceptionProcessor]' from root provider because it requires scoped service 'Sentry.Extensibility.ISentryEventExceptionProcessor'.
 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
 
 InvalidOperationException: Cannot resolve 'System.Collections.Generic.IEnumerable`1[Sentry.Extensibility.ISentryEventExceptionProcessor]' from root provider because it requires scoped service 'Sentry.Extensibility.ISentryEventExceptionProcessor'.
 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
 Microsoft.Extensions.DependencyInjection.ServiceProvider.Microsoft.Extensions.DependencyInjection.ServiceLookup.IServiceProviderEngineCallback.OnResolve(Type serviceType, IServiceScope scope)
 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType)
 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService<T>(IServiceProvider provider)
 Microsoft.AspNetCore.Builder.ApplicationBuilderExtensions.UseSentry(IApplicationBuilder app) in ApplicationBuilderExtensions.cs
 Sentry.AspNetCore.SentryStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder e) in SentryStartupFilter.cs
 Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter+<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
 Microsoft.AspNetCore.HostFilteringStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
 Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

In my Program.cs:

WebHost.CreateDefaultBuilder(args)
    .ConfigureLogging((hostingContext, logging) =>
    {
        // Requires `using Microsoft.Extensions.Logging;`
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        logging.AddConsole();
        logging.AddDebug();
        logging.AddEventSourceLogger();
    })
    .UseStartup<Startup>()
    .UseSentry();

In my Startup for DI registration:

// Sentry (logging/exception handling)
services.AddTransient<ISentryEventProcessor, CustomEventProcessor>();

//This does not work
services.AddScoped<ISentryEventExceptionProcessor, CustomEventExceptionProcessor>();
//This works
//services.AddSingleton<ISentryEventExceptionProcessor, CustomEventExceptionProcessor>();
//This works
//services.AddTransient<ISentryEventExceptionProcessor, CustomEventExceptionProcessor>();

CustomEventExceptionProcessor:

public class CustomEventExceptionProcessor : ISentryEventExceptionProcessor
{
    public void Process(Exception exception, SentryEvent sentryEvent)
    {
        //throw new NotImplementedException();
    }
}

CustomEventProcessor:

public class CustomEventProcessor : ISentryEventProcessor
{
    public SentryEvent Process(SentryEvent @event)
    {
        //throw new NotImplementedException();
        return @event;
    }
}

Metadata

Metadata

Assignees

No one assigned
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions