i am trying to send SMS message to my users from an asp.net webforms project like say everyday at 10AM.Now on a button click its working fine but it takes too much time when data is huge.What i want to achieve is to take this task to the background using
Hangfire.I have read the documentation of hangfire,confused on where to put the code to call the method.
Method :
protected void ReturnButton_OnClick(object sender, EventArgs e)
{
#region SMS Sending
var sms = Everest.Net.BusinessLayer.SMS.SmsSetup.GetSmsSetUp(OfficeId);
if (sms != null)
{
if (sms.EnableSmsData && sms.SmsCount > 0)
{
var customerSMS = BusinessLayer.SMS.SmsSetup.GetAllCustomerSMS(OfficeId);
string CustomerName = "";
foreach (Everest.Net.Common.SMS.SMSSetup s in customerSMS)
{
if (s.CustomerSMS == true && s.Fee_Charged == true)
{
var officeName = BusinessLayer.Office.Offices.GetOffice(OfficeId);
string finalMessage = "";
var cellNum = Everest.Net.BusinessLayer.SMS.SmsSetup.GetCustomerCellPhone(s.CustomerId, s.CorporateCustomer);
if (!string.IsNullOrEmpty(cellNum))
{
var smsLog = new Everest.Net.Common.SMS.SMSSetup();
try
{
if (s.Sms_AccountType == "Loan")
{
sms = Everest.Net.BusinessLayer.SMS.SmsSetup.GetSmsSetUp(OfficeId);
if (sms.EnableSmsData && sms.SmsCount > 0)
{
#region Loan Section
var smsLoan = Everest.Net.BusinessLayer.SMS.SmsSetup.GetLoanId(s.Sms_AccountNumber);
var loanId =
BusinessLayer.SMS.SmsSetup.GetLoanIdValue(s.Sms_AccountNumber);
var loanCustomerName =
BusinessLayer.SMS.SmsSetup.GetCustomerNameByLoanId(loanId);
var dateexceeded =
BusinessLayer.SMS.SmsSetup.IsDateExceeded(loanId);
if (smsLoan != null && dateexceeded == true)
{
foreach (Common.SMS.SMSSetup sm in smsLoan)
{
// var smsClosingBalanceLoan = Everest.Net.BusinessLayer.SMS.SmsSetup.GetLoanInstalmentMatured(sm.LoanId, BusinessLayer.Core.DateConversion.GetCurrentServerDate().AddDays(sms.DaysbeforeLoanalerts).ToString());
var smsClosingBalanceLoan =
BusinessLayer.SMS.SmsSetup.GetAmountForLoanAlert(
sm.LoanId,
BusinessLayer.Core.DateConversion
.GetCurrentServerDate()
.AddDays(sms.DaysbeforeLoanalerts).ToString());
if (smsClosingBalanceLoan != null)
{
if (smsClosingBalanceLoan.LoanAmountToPay > 0)
{
int smsSentAlertCount = sms.LoanAlertCount;
var logCount = BusinessLayer.SMS.SmsSetup.GetLoanSmsAlertSentCount(DateTime.Now.AddDays(-smsSentAlertCount).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"), sm.LoanAccountNumber);
if (logCount < smsSentAlertCount)
{
smsLog = new Everest.Net.Common.SMS.SMSSetup();
finalMessage = "Dear " + loanCustomerName + ",\nYour Loan accnt " + sm.LoanAccountNumber + " with Prin + Int Amnt: Rs." + smsClosingBalanceLoan.LoanAmountToPay + " need to be payed by : " + BusinessLayer.Core.DateConversion.GetCurrentServerDate().AddDays(sms.DaysbeforeLoanalerts).ToString("yyyy-MM-dd") + "\nThank You,\n" + officeName.OfficeName;
smsLog.LogServiceType = "Loan";
smsLog.LogSmsType = s.Sms_SmsType;
smsLog.LogSmsMessage = finalMessage;
smsLog.LogCustomerId = s.CustomerId.ToString();
smsLog.LogAccountNumber = s.Sms_AccountNumber;
smsLog.LogAccountType = s.Sms_AccountType;
smsLog.LogSmsSentDate = BusinessLayer.Core.DateConversion.GetCurrentServerDate();
smsLog.LogSmsFailedDate = "";
smsLog.LogSentStatus = true;
smsLog.LogUserId = UserId;
smsLog.LogSmsFailedMessage = "";
try
{
var result = Everest.Net.BusinessLayer.SMS.smsParameters.SendSMS(sms.FromNum, sms.Token, sms.Url, cellNum, finalMessage);
}
catch (Exception ex)
{
smsLog.LogSmsFailedDate = System.DateTime.Now.ToString("MM/dd/yyyy HHmmss");
smsLog.LogSentStatus = false;
smsLog.LogSmsFailedMessage = ex.Message;
Everest.Net.BusinessLayer.SMS.SmsSetup.InsertSMSLog(smsLog);
}
sms = Everest.Net.BusinessLayer.SMS.SmsSetup.GetSmsSetUp(OfficeId);
sms.SmsCount = sms.SmsCount - 1;
Everest.Net.BusinessLayer.SMS.SmsSetup.UpdateSmsSetup(sms);
Everest.Net.BusinessLayer.SMS.SmsSetup.InsertSMSLog(smsLog);
}
}
}
}
}
#endregion
}
}
}
catch (Exception ex)
{
}
}
}
}
}
}
#endregion
}
Below is the code in link, you could put the above code in a function, then replace Console.WriteLine("Hello, world") with the function name in the above code.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Thank you for your response sir..i want to know where should i put this code..in which aspx page..i am in a webforms project..and i want to run this in scheduled time without any button click.
You could put the above code inside a function instead of click event, then set it in the startup.cs file, for more details, please check the following sample tutorial:
How To: Background jobs in Web Application using Hangfire:
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
33 Points
153 Posts
Doing a background task with hangfire
Jan 25, 2018 12:48 PM|tarun02|LINK
i am trying to send SMS message to my users from an asp.net webforms project like say everyday at 10AM.Now on a button click its working fine but it takes too much time when data is huge.What i want to achieve is to take this task to the background using Hangfire.I have read the documentation of hangfire,confused on where to put the code to call the method.
Method :
Any Help Appreciated.Thanks
Contributor
6680 Points
2715 Posts
Re: Doing a background task with hangfire
Jan 26, 2018 06:41 AM|Eric Du|LINK
Hi tarun02,
According to your description, i check the document of the hangfire about how to call function, please check the below link:
Calling methods with delay:
http://docs.hangfire.io/en/latest/background-methods/calling-methods-with-delay.html
Below is the code in link, you could put the above code in a function, then replace Console.WriteLine("Hello, world") with the function name in the above code.
Best Regards,
Eric Du
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
33 Points
153 Posts
Re: Doing a background task with hangfire
Jan 26, 2018 04:38 PM|tarun02|LINK
Thank you for your response sir..i want to know where should i put this code..in which aspx page..i am in a webforms project..and i want to run this in scheduled time without any button click.
Contributor
6680 Points
2715 Posts
Re: Doing a background task with hangfire
Jan 30, 2018 12:09 PM|Eric Du|LINK
Hi tarun02,
You could put the above code inside a function instead of click event, then set it in the startup.cs file, for more details, please check the following sample tutorial:
How To: Background jobs in Web Application using Hangfire:
http://www.nalashaa.com/to-background-jobs-web-application-hangfire/
Best Regards,
Eric Du
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
33 Points
153 Posts
Re: Doing a background task with hangfire
Jan 31, 2018 01:51 PM|tarun02|LINK