Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Richard Terris 273 posts 715 karma points
    Sep 09, 2015 @ 15:59
    Richard Terris
    1

    MemberService Saved Event firing 3 times

    Hi guys,

    I've hooked into the Saving event on the MemberService (I have tried Saved event too) and under certain conditions I am setting a value on a custom property.

    The problem is (having debugged the code) that after the value is set, the Saving event is being fired another 2 times with the same member and the value of the custom property is being overwritten the 3rd time with null.

    I am calling the .Save() method and setting raiseEvents to false but I have also tried not having this save call at all.

    Does anyone know of any reason why the event would be fired 3 times? I did notice a while back there was a similar issue reported in the issue tracker, but that seems to have been resolved.

    As always I'm open to the possibility that I may be doing something wrong. Any help will be much appreciated.

    Richard

  • Wil Jones 11 posts 31 karma points
    Sep 09, 2015 @ 19:04
    Wil Jones
    0

    Can you post some example code?

  • Richard Terris 273 posts 715 karma points
    Sep 10, 2015 @ 08:11
    Richard Terris
    0

    Sure can:

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            MemberService.Saving += MemberService_Saved;
        }
    
        private void MemberService_Saved(IMemberService sender, SaveEventArgs<IMember> e)
        {
            foreach (IMember member in e.SavedEntities)
            {
                if (member.IsNewEntity()) //This is a newly created member
                {
                    //Reset token
                    Guid resetToken = Guid.NewGuid();
                    string setPasswordUrl = CreatePasswordUrl(resetToken.ToString(), member.Email);
                    if (!string.IsNullOrEmpty(setPasswordUrl))
                    {
                        EmailNewUser(member.Email, member.Name, setPasswordUrl);
                    }
    
                    member.SetValue("resetToken", resetToken.ToString());
                    ApplicationContext.Current.Services.MemberService.Save(member, false);
                }
            }
        }
    

    You can ignore the fact that the event is Saving and the method is MemberService_Saved; this is because I've been trying different events but I haven't been renaming the method.

  • Wil Jones 11 posts 31 karma points
    Sep 10, 2015 @ 09:00
    Wil Jones
    0

    It's not the foreach going round 3 times is it? Saving the member each time for the e.SavedEntities.

  • Richard Terris 273 posts 715 karma points
    Sep 10, 2015 @ 09:02
    Richard Terris
    0

    No, there is only one saved entity, and it's the event which is being triggered 3 times with the same object as e each time

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 10, 2015 @ 09:56
  • Richard Terris 273 posts 715 karma points
    Sep 10, 2015 @ 10:00
    Richard Terris
    0

    yep, looks like it, so definitely sounds like a bug.

    I'll update the issue tracker and I'll implement a workaround in the meantime.

    Thanks for the help Jeroen!

  • Leon Lindeberg 12 posts 103 karma points
    Sep 07, 2017 @ 12:07
    Leon Lindeberg
    0

    Got stuck here as well. Did you find a solution?

  • Marcio Goularte 388 posts 1360 karma points
    Sep 07, 2017 @ 14:36
    Marcio Goularte
    0

    The post is old and I do not know if they have already fixed this bug. When I need to update member property in the event and do not want to fire the event again, I do so:

     

    ApplicationContext.Current.Services.MemberService.Save (member, raiseEvents: false);
    

    Set the raiseEvents variable to false in the method. Always worked

Please Sign in or register to post replies

Write your reply to:

Draft