Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow clearing of cached type infos via explicit method call - Mediator._notificationHandlers #1090

Open
Ralf1108 opened this issue Dec 13, 2024 · 1 comment
Labels

Comments

@Ralf1108
Copy link

We use MediatR to implement a plugin infrastructure for a table data program.
We defined messages in a plugin interface assembly which is used for each plugin. The plugin can also define their own messages.
Then we load the plugins via AssemblyLoadContext and register all request handlers vi DI and RegisterServicesFromAssembly().
Afterwards we can call the request handlers in the plugins via the IMediatR interface.

The issue is when we want unload the plugin during runtime.
We observed when we used "mediator.Publish" then there will be NotificationHandlerWrapper instances cached in a static field inside MediatR.
The wrappers are created here
and cached here.

The caching of these wrappers when they contain message types from the plugin assemblies prevents them from being unloaded properly.
We used the Visual Studio Memory to verify this:
image

We did not find a way to clear this cache except using reflection code:

// remove static references from Mediator when used "mediator.Publish"
var fieldInfo = typeof(MediatR.Mediator).GetField("_notificationHandlers", BindingFlags.Static | BindingFlags.NonPublic);
var value = (ConcurrentDictionary<Type, NotificationHandlerWrapper>)fieldInfo.GetValue(null);
value.Clear();

Is it possible to provide an official method to clear cached values inside MediatR?

Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant