Skip to content

Resolve Processors from ServiceProvider for Background Services in ASP.NET Core - #3476

Closed
jamescrosswell wants to merge 2 commits into
mainfrom
transient-event-processors
Closed

jamescrosswell wants to merge 2 commits into
mainfrom
transient-event-processors

Conversation

@jamescrosswell

@jamescrosswell jamescrosswell commented Jul 11, 2024 •

Copy link
Copy Markdown
Collaborator

Resolves GDX (view)

Currently WIP (sharing for comment).

It kind of works. I need to find a way to deduplicate these since the Except that is used currently only compares object references (which is too specific):

scope.AddEventProcessors(_eventProcessors.Except(scope.GetAllEventProcessors()));
scope.AddExceptionProcessors(_eventExceptionProcessors.Except(scope.GetAllExceptionProcessors()));
scope.AddTransactionProcessors(_transactionProcessors.Except(scope.GetAllTransactionProcessors()));

@github-actions

github-actions Bot commented Jul 11, 2024 •

Copy link
Copy Markdown
Contributor
Fails
🚫 Please consider adding a changelog entry for the next release.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

- Resolve Processors from ServiceProvider for Background Services in ASP.NET Core ([#3476](https://github.com/getsentry/sentry-dotnet/pull/3476))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description.

Generated by 🚫 dangerJS against 237564f

@jamescrosswell jamescrosswell changed the title Resolve Transient Processors for Background Services in ASP.NET Core Resolve Processors from ServiceProvider for Background Services in ASP.NET Core Jul 11, 2024
@jamescrosswell

Copy link
Copy Markdown
Collaborator Author

I need to find a way to deduplicate these since the Except that is used currently only compares object references

This turns out to be much harder than I thought.

For a start, some of these dependencies could be transient. Each transient dependency retrieved from the ServiceProvider will be, by definition, a separate instance (so the references won't match). Also, because we've effectively duplicated the activators that create instances in the LifetimeServiceResolver, we might get different references for Singletons being resolved from the ApplicationBuilderExtensions than those that get injected into the SentryMiddleware constructor.

If we were designing things from scratch, maybe we could tease the providers from here into separate Scoped and NonScoped providers:
https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry/SentryOptions.cs#L139

But as we have a public API that let's people add their own Event Processor Providers and they can add anything (scoped, transient or singleton) to that list, we have no way of knowing the lifetimes of the IEnumerable<ISentryEventProcessor> processors when we resolve these in order to filter out any non-scoped dependencies that would already have been added to the options (and therefore the Sentry Scope) via the ApplicationBuilderExtensions.

I thought of using the LifetimeServiceResolver to resolve a list of the implementation types of Scoped providers that could be used to filter the list... but it's entirely possible for someone to register the same concrete type twice... once as a transient and once as a scoped dependency.

All in all, this seems to be the wrong approach and I don't have any other bright ideas.

At this point, I'm starting to think we're better off recommending people register custom providers via the options rather than via the service provider directly.

@bitsandfoxes

Copy link
Copy Markdown
Contributor

Thanks for the detailed writeup!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants