Skip to content

Unable to use ISentryEventExceptionProcessor with Scoped DI #318

Closed

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;
    }
}

Activity

  1. changed the title [-]Unable to use `ISentryEventExceptionProcessor` with Scoped DI[/-] [+]Unable to use ISentryEventExceptionProcessor with Scoped DI[/+] on Dec 2, 2019
  2. bruno-garcia commented on Dec 6, 2019

    @bruno-garcia
    Member

    Thanks for raising this. I'll take a look soon.

  3. udlose commented on Mar 4, 2020

    @udlose
    Author

    any updates on this?

  4. bruno-garcia commented on Mar 5, 2020

    @bruno-garcia
    Member

    Sorry but I haven't had time to look at it yet. I suspect that the DI container doesn't use AsyncLocal for Scoped lifetime (creates a new container with the reference held by the HttpContext and somehow the callback we have is for an outer instance of the container (since we get it when the app starts).
    If that's the case, we could change it so that we register the EventProcessorProvider in the SentryMiddleware grabbing the container from the HttpContext. This way we'd be resolving Scoped properly.

    Would really appreciate some help with this since we're pretty busy here right now working on release health

  5. Yourstress commented on May 28, 2020

    @Yourstress

    Is there a workaround to make this work? I would like to supply the user details (in a different scoped service) and can only do it if I inject ISentryEventExceptionProcessor as a scoped service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions