开发者

Need suggestion on background worker

开发者 https://www.devze.com 2023-03-08 00:45 出处:网络
I want to develope a delivery application(self hosted WCF service ) which allows scheduling of the emai开发者_开发知识库ls. User will assign a schedule to email and send it. The WCF service should be

I want to develope a delivery application(self hosted WCF service ) which allows scheduling of the emai开发者_开发知识库ls. User will assign a schedule to email and send it. The WCF service should be able to pick the email and send it on its scheduled time.

What approach shall I use here? I am thinking about following alternatives

  1. Use background worker thread to perform this task
  2. Any third party scheduling service (I yet to investigate on this)

Can anyone suggest me a possible solution for this apart from above mentioned two?

[Edit] : Can I use SQL Agents for this?

Thanks,

Ram


Have your WCF service queue the email requests either to a database table. Then write a Windows service that periodically scans the table, sends the email, and then update the table with the results.

If you're using a SQL Server, you can send emails directly from it and also schedule jobs to send the emails, saving you from having to deploy a Windows service.


I saw the "...apart from above mentioned two..." but I think there is nothing else to achieve this :) Either build an infinite loop inside of windows service like this:

private void DoTheThing()
    {
        try
        {
            while (true)
            {
                TheThing e = new TheThing();
                Thread t = new Thread(new ThreadStart(e.Run));
                t.Start();
                Thread.Sleep(1000);
            }
        }
        catch (ThreadAbortException) { }
        catch (Exception ex) { /* Whatever error handling you got */ }
    }

... where TheThing has a method Run which does all that you need every 1 second. This looks silly (while(true) - yeah, right) but has been working non-stop since .NET 1.0 on at least 30 servers :) Just make sure you call this DoTheThing method on Start of your windows service in a new thread.

Hope this helps :)


It depends on your non-functional requirements... :-)

I can imagine that you would want some reliability in this service. The life-time of the WCF service depends on its host. Eg. if this is IIS, the app pool will be recycled after a certain idle time (no requests coming in to IIS). This would plead for a different solution than a background worker. As you suggest, this could be a third party scheduler but there is also scheduler in Windows. (see http://technet.microsoft.com/en-us/library/dd363786(WS.10).aspx) With a small console program, you could have the Windows scheduler call your service, or alternatively, send the email itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消