Developer forum

Forum » Development »  Notification Subscriber [UserSaved] OnNotify method runs 2 times when a user is created via a form in frontend

Notification Subscriber [UserSaved] OnNotify method runs 2 times when a user is created via a form in frontend

Lars Mathiesen
Reply

I have a problem with my [UserSaved] notification subscriber is exceuted two times every time a user signs up on a page in the frontend. I can't seem to figure out why this notification subscriber is exceuted two times - I would only expect it to run 1 time, when a users is created.

The notification Subscriber has the same user information in both runs of the subscriber. I have tried to use fiddler to see if the frontend page post the user create form two times, but it dosent.

I've tried to restrict my OnNotify method only to run if im "in the frontend page", I tried to restrict the URL to only run on a specific url, but the subscribers OnNotify method is still exceuted two times.

Is there a way to make sure the OnNotify method in my code only is exceuted a single time? 

I have spoken with some of my colleagues aswell, but with no luck of finding the right solution, hope someone out that can enlighten me in this case, in advance thanks.

[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Modules.UserManagement.Notifications.UserSaved)]
    public class AfterSubmitUserToIsabellaClubObserver : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            if (args == null || !(args is Dynamicweb.Modules.UserManagement.UserNotificationArgs))
                return;

......................................


Replies

 
Morten Bengtson
Reply
This post has been marked as an answer

Sometimes a notification is fired multiple times during a single request.

You can make sure your code is only executed once during a request by adding a few extra lines of code...

public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
{
    if (HttpContext.Current.Items.Contains(notification)) return; // we've already been here. skip the rest.
    HttpContext.Current.Items.Add(notification, true);

    // Add your code here
}
Votes for this answer: 1
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi Lars,

You are correct, the notification is fired too many times. I have create a bug report to get this issue fixed (16650). Which version of Dynamicweb are you using?

In the meantime, you can work around this by adding a value into the HttpContext.Items collection indicating that the save method has already run for this HTTP request.

Like this:

// Check whether the notification has run already or not
if (System.Web.HttpContext.Current.Items.Contains("MyValueIndicatingThisNotificationHasRun"))
	return;

// TODO: Execute your notification specific code

// Set value indicating that the notification has already run
System.Web.HttpContext.Current.Items["MyValueIndicatingThisNotificationHasRun"] = true;

The Items collection is emptied after the HTTP request ends, so the collection will only contain your value if the notification has in fact run.

- Jeppe

Votes for this answer: 1
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Another night owl beat me to it I see. Luckily solutions are pretty similar :)

 
Lars Mathiesen
Reply

Thanks for your answers, it worked perfectly.

I discovered something else during all of my debugging, and that is if you modify some user details in the AccessUserTable for the current user in the OnNotify method during the first run, then the modified user details will be reset when the method is exceuted the secound time, to the default values which is provided from the form the user used to sumbit his details from.. And in my case I need to set some flags on the current  user who are signing up - so I changed your solutions in the oppisite way. So in my case i want to exceute my custom code the second time the OnNotify method is exceuted, so the flags will be saved in the AccessUser table after my notification subscriber is complete. I used the following method as shown below.

[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Modules.UserManagement.Notifications.UserSaved)]
    public class AfterSubmitUserToIsabellaClubObserver : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            if (args == null || !(args is Dynamicweb.Modules.UserManagement.UserNotificationArgs))
                return;

            //if this is the first time the OnNotify code is executed, want want to add a web item to the current request
            if (HttpContext.Current.Items.Contains(notification) == false)
            {
                HttpContext.Current.Items.Add(notification, true);
                return; //break out of the first run
            }

if (HttpContext.Current.Items.Contains(notification) == true)
            {

// custom code here........

            }

I would say that the method is resetting the user details modified in the first run of the OnNotify is a bug aswell, I don't know if u also where aware of that Jeppe Eriksson Agger ?

 

 

 

You must be logged in to post in the forum