Skip to main content
corrected code
Source Link
cubski
  • 3.3k
  • 1
  • 33
  • 33

Or using Async CTP.

public async boolTask<bool> Register(UserInfo info) { try { using (mydatabase db = new mydatabase()) { userinfotable uinfo = new userinfotable(); uinfo.Name = info.Name; uinfo.Age = info.Age; uinfo.Address = info.Address; db.userinfotables.AddObject(uinfo); db.SaveChanges(); //Wait for task to finish asynchronously await Utility.SendEmail(info); return true; } } catch { return false; } } private Task SendEmail(MailMessage mail) { SmtpClient client = new SmtpClient(); client.Host = "smtp.something.com"; client.Port = 123; client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); client.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); //The smtp SendTaskAsync is an extension method when using Async CTP return client.SendTaskAsync("from", "recipients", "subject", "body"); } 

There is also a bug in your original code. When an exception is thrown inside SendEmail function, it returns false but inside the register function it will still return true. Assuming that the bool signifies success or failure.

Or using Async CTP.

public async bool Register(UserInfo info) { try { using (mydatabase db = new mydatabase()) { userinfotable uinfo = new userinfotable(); uinfo.Name = info.Name; uinfo.Age = info.Age; uinfo.Address = info.Address; db.userinfotables.AddObject(uinfo); db.SaveChanges(); //Wait for task to finish asynchronously await Utility.SendEmail(info); return true; } } catch { return false; } } private Task SendEmail(MailMessage mail) { SmtpClient client = new SmtpClient(); client.Host = "smtp.something.com"; client.Port = 123; client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); client.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); //The smtp SendTaskAsync is an extension method when using Async CTP return client.SendTaskAsync("from", "recipients", "subject", "body"); } 

There is also a bug in your original code. When an exception is thrown inside SendEmail function, it returns false but inside the register function it will still return true. Assuming that the bool signifies success or failure.

Or using Async CTP.

public async Task<bool> Register(UserInfo info) { try { using (mydatabase db = new mydatabase()) { userinfotable uinfo = new userinfotable(); uinfo.Name = info.Name; uinfo.Age = info.Age; uinfo.Address = info.Address; db.userinfotables.AddObject(uinfo); db.SaveChanges(); //Wait for task to finish asynchronously await Utility.SendEmail(info); return true; } } catch { return false; } } private Task SendEmail(MailMessage mail) { SmtpClient client = new SmtpClient(); client.Host = "smtp.something.com"; client.Port = 123; client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); client.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); //The smtp SendTaskAsync is an extension method when using Async CTP return client.SendTaskAsync("from", "recipients", "subject", "body"); } 

There is also a bug in your original code. When an exception is thrown inside SendEmail function, it returns false but inside the register function it will still return true. Assuming that the bool signifies success or failure.

fixed code error
Source Link
cubski
  • 3.3k
  • 1
  • 33
  • 33

Or using Async CTP.

public async bool Register(UserInfo info) { try { using (mydatabase db = new mydatabase()) { userinfotable uinfo = new userinfotable(); uinfo.Name = info.Name; uinfo.Age = info.Age; uinfo.Address = info.Address; db.userinfotables.AddObject(uinfo); db.SaveChanges(); //Wait for task to finish asynchronously await Utility.SendEmail(info); return true; } } catch { return false; } } private async Task SendEmail(MailMessage mail) { SmtpClient client = new SmtpClient(); client.Host = "smtp.something.com"; client.Port = 123; client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); client.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); //The smtp SendTaskAsync is an extension method when using Async CTP awaitreturn client.SendTaskAsync("from", "recipients", "subject", "body"); } 

There is also a bug in your original code. When an exception is thrown inside SendEmail function, it returns false but inside the register function it will still return true. Assuming that the bool signifies success or failure.

Or using Async CTP.

public async bool Register(UserInfo info) { try { using (mydatabase db = new mydatabase()) { userinfotable uinfo = new userinfotable(); uinfo.Name = info.Name; uinfo.Age = info.Age; uinfo.Address = info.Address; db.userinfotables.AddObject(uinfo); db.SaveChanges(); //Wait for task to finish asynchronously await Utility.SendEmail(info); return true; } } catch { return false; } } private async Task SendEmail(MailMessage mail) { SmtpClient client = new SmtpClient(); client.Host = "smtp.something.com"; client.Port = 123; client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); client.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); //The smtp SendTaskAsync is an extension method when using Async CTP await client.SendTaskAsync("from", "recipients", "subject", "body"); } 

There is also a bug in your original code. When an exception is thrown inside SendEmail function, it returns false but inside the register function it will still return true. Assuming that the bool signifies success or failure.

Or using Async CTP.

public async bool Register(UserInfo info) { try { using (mydatabase db = new mydatabase()) { userinfotable uinfo = new userinfotable(); uinfo.Name = info.Name; uinfo.Age = info.Age; uinfo.Address = info.Address; db.userinfotables.AddObject(uinfo); db.SaveChanges(); //Wait for task to finish asynchronously await Utility.SendEmail(info); return true; } } catch { return false; } } private Task SendEmail(MailMessage mail) { SmtpClient client = new SmtpClient(); client.Host = "smtp.something.com"; client.Port = 123; client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); client.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); //The smtp SendTaskAsync is an extension method when using Async CTP return client.SendTaskAsync("from", "recipients", "subject", "body"); } 

There is also a bug in your original code. When an exception is thrown inside SendEmail function, it returns false but inside the register function it will still return true. Assuming that the bool signifies success or failure.

Source Link
cubski
  • 3.3k
  • 1
  • 33
  • 33

Or using Async CTP.

public async bool Register(UserInfo info) { try { using (mydatabase db = new mydatabase()) { userinfotable uinfo = new userinfotable(); uinfo.Name = info.Name; uinfo.Age = info.Age; uinfo.Address = info.Address; db.userinfotables.AddObject(uinfo); db.SaveChanges(); //Wait for task to finish asynchronously await Utility.SendEmail(info); return true; } } catch { return false; } } private async Task SendEmail(MailMessage mail) { SmtpClient client = new SmtpClient(); client.Host = "smtp.something.com"; client.Port = 123; client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); client.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); //The smtp SendTaskAsync is an extension method when using Async CTP await client.SendTaskAsync("from", "recipients", "subject", "body"); } 

There is also a bug in your original code. When an exception is thrown inside SendEmail function, it returns false but inside the register function it will still return true. Assuming that the bool signifies success or failure.