c# - Workflow required to perform lengthy ASP.NET task -
i have asp.net webforms application mimics desk system. application works fine, recently, asked me make can text message in system whenever new desk ticket opened.
i using twilio , working fine. problem is, there 15 people in system should getting text message , when ticket submitted, application takes 15-20 seconds repost submit. in future, there more 15 people, double even.
what wondering if there way send these messages in background, page come submit right away. here relevant code:
this main method wrote sending text message. in utility class:
public static string sendsms(string phonenumber, string message) { var request = (httpwebrequest)webrequest.create("https://api.twilio.com/2010-04-01/accounts/" + constants.twilioid + "/messages.json"); string postdata = "from=" + constants.twiliofromnumber + "&to=+1" + phonenumber + "&body=" + httputility.htmlencode(message); byte[] data = encoding.ascii.getbytes(postdata); string authorization = string.format("{0}:{1}", constants.twilioid, constants.twilioauthtoken); string encodedauthorization = convert.tobase64string(encoding.ascii.getbytes(authorization)); string credentials = string.format("{0} {1}", "basic", encodedauthorization); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; request.contentlength = data.length; request.headers[httprequestheader.authorization] = credentials; using (var stream = request.getrequeststream()) { stream.write(data, 0, data.length); } string responsestring; using (var response = (httpwebresponse) request.getresponse()) { using (var reader = new streamreader(response.getresponsestream())) { responsestring = reader.readtoend(); } } return responsestring; }
and here how i'm calling it:
public void btnsubmit_click(object sender, eventargs e) { // // more code here, irrelevant // var employees = new employees(); employees.getall(); foreach (employee employee in employees) { string number = employee.cellphoneareacode + employee.cellphoneprefix + employee.cellphonesuffix; if (!string.isnullorempty(number) && number.length == 10) { utility.sendsms(number, "a new desk ticket in system!"); } } }
the other idea can come create wcf service, seemed on kill. suggestions welcome!
any asynchronous approach should trick. example, using task
or (if you're on .net 4.5+) async
method. (remember handle asynchronous errors supplying callback .continuewith()
examine task errors , respond accordingly.)
meaningfully responding errors in case might complex, though. sounds sort of operation want keep re-trying in event of failure (with logging in case of constant failures), , want continue loop if 1 message fails. little more manual might in order.
for recommend persisting messages simple database table application , continuing ui want. have separate application, such windows service, periodically polls database table , sends messages in simple loop on records.
a approach keep simple status flag on message records. queued, sent, error (with error message), etc. windows service can update records sends messages in loop. given message errors, update record , continue loop. re-try error-ed messages appropriate.
Comments
Post a Comment