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

Update all examples of subscribing to static events to include corresponding 'unsubscribing' to that event during termination #2690

Closed
marcemarc opened this issue Sep 4, 2020 · 2 comments · Fixed by #2692

Comments

@marcemarc
Copy link
Contributor

eg this sort of example:

public class SubscribeToSavedEventComponent : IComponent
{
private readonly ILogger _logger;

    public SubscribeToSavedEventComponent(ILogger logger)
    {
        this._logger = logger;
    }
   
    public void Initialize()
    {
        //specify my event handler
        MemberService.Saved += MemberService_Saved;
    }

    private void MemberService_Saved(IMemberService sender, SaveEventArgs<IMember> e)
    {
        foreach (var member in e.SavedEntities)
        {
            
          //write to the logs every time a member is saved
          this._logger.Info<MyPublishEventComponent>("member {member} has been saved and event fired!", member.Name);
        }
    }
    public void Terminate()
    {
        // Nothing to terminate
    }

should be

public class SubscribeToSavedEventComponent : IComponent
{
private readonly ILogger _logger;

    public SubscribeToSavedEventComponent(ILogger logger)
    {
        this._logger = logger;
    }
   
    public void Initialize()
    {
        //specify my event handler
        MemberService.Saved += MemberService_Saved;
    }

    private void MemberService_Saved(IMemberService sender, SaveEventArgs<IMember> e)
    {
        foreach (var member in e.SavedEntities)
        {
            
          //write to the logs every time a member is saved
          this._logger.Info<MyPublishEventComponent>("member {member} has been saved and event fired!", member.Name);
        }
    }
    public void Terminate()
    {
         //unsubscribe to event during shutdown
        MemberService.Saved -= MemberService_Saved;
    }

See this PR for context: umbraco/Umbraco-CMS#8815

@marcemarc
Copy link
Contributor Author

@nul800sebastiaan
Copy link
Member

Fixed in #2692

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants