C# 从挂火调用方法时遇到困难

C# 从挂火调用方法时遇到困难,c#,twilio,hangfire,C#,Twilio,Hangfire,我有一个控制器方法,负责安排约会。我期待着安排一个任务,通过短信提醒用户(twilio)5分钟前,他们的约会登录到服务 下面是我的方法调用: private SMSNotifier sms = new SMSNotifier(); BackgroundJob.Schedule( () => sms.SendPatientUpcomingApptNotificationTest(appointment), appointment.Start.AddMinutes(-5)); 这是我的班级

我有一个控制器方法,负责安排约会。我期待着安排一个任务,通过短信提醒用户(twilio)5分钟前,他们的约会登录到服务

下面是我的方法调用:

private SMSNotifier sms = new SMSNotifier();
 BackgroundJob.Schedule( () => sms.SendPatientUpcomingApptNotificationTest(appointment), appointment.Start.AddMinutes(-5));
这是我的班级:

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));

    }
}
我不断地发现这个错误:

Server Error in '/' Application.

Self referencing loop detected for property 'User' with type 'System.Data.Entity.DynamicProxies.AspNetUser_80E6332CC002F8FCF589159A68E74A0922BEE992586B9FE280D950E149CCC7EB'. Path 'Patient.ActiveSessions[0]'.
但我不明白为什么。我不在任何地方引用用户对象

我应该提到的是,我显然用谷歌搜索了那个错误:

  • 所有这些都没有取得任何进展。我仍然有同样的错误

    你们觉得怎么样

    我肯定认为这与我试图安排“SendPatientUpcomingApptNotificationTest”方法的方式有关。我可以做一个控制台。WriteLine,它可以很好地对工作进行排队

    即:


    工作正常

    在尝试序列化您的
    约会
    对象时,它似乎遇到了此错误

    安排后台作业时,Hangfire会将有关正在调用的方法及其参数的信息保存到其作业存储中。如果将复杂对象作为参数传递给该方法,它会尝试将整个对象图序列化为json

    这个。例如,将appointmentId传递给作业而不是整个约会:

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

    然后您将检索工作中的实际约会。

    谢谢Peter,这非常有帮助。我现在似乎可以毫无问题地排队工作了。我的另一个问题是,SMSNotifier类的构造函数是否正确执行?我仍然在HangFire控制台中收到空引用错误。但是,当我手动执行SendPatientUpcomingApptNotificationTest方法时,它工作得很好。奇怪吧?
    BackgroundJob.Schedule( () => sms.SendPatientUpcomingApptNotificationTest(appointmentId), ...);