0

I have a controller method which is responsible for scheduling appointments. I'm looking to schedule a task to remind the user via SMS (twilio) 5 minutes before their appointment to login to the service.

Here is my method call:

private SMSNotifier sms = new SMSNotifier();
 BackgroundJob.Schedule( () => sms.SendPatientUpcomingApptNotificationTest(appointment), appointment.Start.AddMinutes(-5));

and here is my class:

public SMSNotifier()
    {
        var accountSid = ConfigurationManager.AppSettings["TwilioSid"];
        // Your Auth Token from twilio.com/console
        var authToken = ConfigurationManager.AppSettings["TwilioToken"]; ;

        TwilioClient.Init(accountSid, authToken);
    }


    public void SendPatientUpcomingApptNotificationTest(Appointment appt)
    {

            var message = MessageResource.Create(
        to: new PhoneNumber("+xxxxxxxxxx"),
        from: new PhoneNumber("+xxxxxxxxxx"),
        body: string.Format("Hello {0} {1}! You have an upcoming web based video appointment with xxxxxxxxxx. Please login to the website to be seen. Your appointment time is: {2} Thank you - xxxxxxxx", "xxxxxxx", "xxxxxxxx", appt.Start));

    }
}

I keep getting this error:

Server Error in '/' Application.

Self referencing loop detected for property 'User' with type 'System.Data.Entity.DynamicProxies.AspNetUser_80E6332CC002F8FCF589159A68E74A0922BEE992586B9FE280D950E149CCC7EB'. Path 'Patient.ActiveSessions[0]'.

But I just don't understand why. I don't reference a User object anywhere.

I should mention I obviously googled that error:

  1. JSON.NET Error Self referencing loop detected for type

  2. Self referencing loop detected - Getting back data from WebApi to the browser

None of this provided any progress. I still have the same error.

What do you guys think?

I definitely think it has to do with the way I'm trying to schedule the 'SendPatientUpcomingApptNotificationTest' method. I can do a Console.WriteLine and it queues the job fine.

IE:

  BackgroundJob.Schedule( () => Console.WriteLine("TestSchedule"), appointment.Start.AddMinutes(-5));

Works perfectly

jon.r
  • 896
  • 8
  • 16

1 Answers1

1

It sounds like it's running into this error when trying to serialize your appointment object.

When you schedule a background job, Hangfire saves info about the method you are calling and its arguments to its job store. If you're passing a complex object as an argument to the method, it attempts to serialize the whole object graph to json.

The recommended approach is to keep your job arguments small and simple. For example, pass an appointmentId to the job instead of the whole appointment:

BackgroundJob.Schedule( () => sms.SendPatientUpcomingApptNotificationTest(appointmentId), ...);

Then you would retrieve the actual appointment within the job.

Peter
  • 12,541
  • 3
  • 34
  • 39
  • Thanks Peter, that's very helpful. I seem to be able to queue up job now without issue. My other question is, does the constructor for the SMSNotifier class get executed properly? I'm still getting null reference errors in the HangFire console. But when I manually execute the SendPatientUpcomingApptNotificationTest method, it works fine. Weird right? – jon.r Dec 04 '17 at 23:23