简体   繁体   English

在asp.net中运行Web服务多长时间

[英]how long run web service in asp.net

I'm creating mail sender web service. 我正在创建邮件发件人Web服务。 Every 10 second 1 mail sent. 每10秒发送1封邮件。 My total mail count 1500. this operation ends after 4.16 hours later (1500*10/60/60). 我的邮件总数为1500。此操作在4.16小时后(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;
            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. 为什么要等待10秒。 Because my mail provider agreed that mail account every 1 hour possible to send 300 mail. 因为我的邮件提供商同意该邮件帐户每1小时可以发送300封邮件。

I want calling from aspx page. 我想从aspx页面打电话。

My question is: 我的问题是:
How to call this service from aspx page? 如何从aspx页面调用此服务?

This web service work or not? 此Web服务有效吗?

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. 没有理由将其实现为Web服务,尤其是当您有10秒钟的睡眠时间时,它可能会发生多次,这可能会导致超时。 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. 另外,由于在asp.net下运行时间较长,因此存在应用程序池回收失去所有尚未发送的邮件的风险。

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. 如果您使用Windows服务( 而非 Web服务)或由“ 计划任务”触发的控制台应用程序执行发送每封电子邮件的代码,这也将更加强大。

In summary: 综上所述:

  • Using a web service that sleeps and sends multiple emails could be very unreliable 使用休眠并发送多封电子邮件的Web服务可能非常不可靠
  • Use a Windows Service or a scheduled task 使用Windows服务或预定任务

Also, your code has the line msg.To.Add(email); 另外,您的代码行msg.To.Add(email); but the variable email isn't declared anywhere? 但是可变的email没有在任何地方声明吗?

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. 我认为您最好的选择是在BroadcastMail方法中产生一个单独的线程,并使该线程处理邮件-这样,您就可以及时返回BroadcastMail方法,同时仍触发广播。

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. 虽然不是ASP.Net程序员,但如果这种方法会遇到与流程回收等相关的问题,我准备将其击倒。

Service.cs Service.cs

using System; 使用系统; using System.Collections.Generic; 使用System.Collections.Generic;

另外,您遍历从未使用过的名为“ dv”的集合,这是为什么呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM