-
Notifications
You must be signed in to change notification settings - Fork 23
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
Unsubscribe? #1
Comments
According to my test, it seems that yes. A component subscribing itself to the event aggregator (as shown in the documentation) should implement |
Yeah it needs to unsubscribe at |
@dodyg is there a way to automatically unsubscribe on each listening component? Or is it required to manually put that logic in each component? Is there an example of how you unsubscribe at |
This is the pattern I am using in each of the component that cares
protected override async Task OnInitializedAsync()
{
base.SubscribeEvents();
}
public Task HandleAsync(JobNominationPagingInfo pi)
{
return Task.CompletedTask;
}
public void Dispose()
{
base.UnsubscribeEvents();
} |
Thanks. I'm using a code behind file so I think that solution would look something like the following... public partial class FooPage : IHandle<FooMessage>, IDisposable
{
protected override void OnInitialized()
{
EventAggregator.Subscribe(this);
}
public void Dispose()
{
EventAggregator.Unsubscribe(this);
}
} |
Is unsubscribing still necessary if EventAggregator is added as a scoped service? |
There is no such thing as Scoped service in Blazor. It's either Singleton or Transient. |
Blazor WebAssembly apps don't currently have a concept of DI scopes. Scoped-registered services behave like Singleton services. Registering a dependency as Scoped in Blazor Server will result in dependencies that live for the duration of the user’s session. Instances of Scoped dependencies will be shared across pages and components for a single user, but not between different users and not across different tabs in the same browser. It sounds like a scoped service is what I want for my Blazor Server EventAggregator. I don't want a singleton because I don't want a single instance for all users, across all tabs/browsers. And I don't want transient because it isn't necessary to obtain a new instance of the EventAggregator for each component. Thoughts? |
Maybe the disconnect is you are using WebAssembly and I'm using Server... |
You can use EventAggregrator in scoped in Blazor Server but you still have to subscribe and unsubscribe. |
Ok. And I'm able to register it as a scoped service by doing the following:
but I'm struggling to determine how to set |
My issue was the order. I initially had:
which caused IEventAggregator to be registered as a singleton which for Blazor Server is not desirable. Switching to the following seems to be the workaround:
Thanks! |
Does the subscriber have to call
Unsubscribe
in order to prevent memory leaks (as theEventAggregator
is registered as a singleton)?The text was updated successfully, but these errors were encountered: