how long run web service in asp.net
I'm creating mail sender web service. Every 10 second 1 mail sent. My total mail count 1500. this operation ends after 4.16 hours later (1500*10/60/60).
My web service method is here>>
[WebMethod]
public string BroadcastMail(DataView dv, string title, string body,MailAddress FromAddress, string[] fileattachments)
{
SmtpClient client = new SmtpClient("smtp.mysite.com");
client.EnableSsl = false;
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("*********", "********");
foreach (DataRowView item in dv)
{
MailMessage msg = new MailMessage();
msg.From = FromAddress;
开发者_Python百科 msg.Subject = title;
msg.IsBodyHtml = true;
msg.BodyEncoding = Encoding.UTF8;
msg.Body = body.AsciiToUnicode();
msg.To.Add(email);
client.Send(msg);
Thread.Sleep(10000);
}
}
You want ask from me. Why you wait 10 second. Because my mail provider agreed that mail account every 1 hour possible to send 300 mail.
I want calling from aspx page.
My question is:
How to call this service from aspx page?This web service work or not?
You have another idea?
There's no reason to implement this as a web service, especially as you have a 10 second sleep in it that could occur multiple times which would likely timeout. Also, as a long running process under asp.net, you run the risk of an application pool recycle loosing any mails that have yet to be sent.
A simpler/better way of handling this would be to store the emails into a database and process them from there, deleting the record from the table once the email has been sent. It would also be more robust if you used something like a Windows Service (not web service) or a console application triggered by a Scheduled Task to execute the code that sends each individual email.
In summary:
- Using a web service that sleeps and sends multiple emails could be very unreliable
- Use a Windows Service or a scheduled task
Also, your code has the line msg.To.Add(email);
but the variable email
isn't declared anywhere?
I think your best bet here is to spawn off a separate thread in the BroadcastMail method and have that thread deal with the mailing - that way you can have your BroadcastMail method return in a timely fashion whilst still having triggered off the broadcast.
Not being an ASP.Net programmer, though, I'm prepared to be shot down in flames if this approach will hit problems relating to recycling of processes etc.
Service.cs
using System; using System.Collections.Generic;
Also, your iterating through a collection named "dv" which you never use, why is this?
精彩评论