做Window Service的过程及注意事项

以前上学时学过Web Service,但是一直没有项目实践。这次接到的是一个Window Service的项目,可以说是第一次做。

下面是Spec:

Business/Functional Description

Lincoln Appraisal is in the business of matching certified home Appraisers with Banks and Lending institutions that supply mortgages.  The Mortgage process requires that a property be appraised before a Bank or Lending Institution can give out a loan.

Lincoln works with Banks and Lending institutions that need to schedule appraisals for mortgage applications.  Lincoln has a network of appraisers all around the country that are assigned to appraisal work based on XXXXXX for the most part and qualifications for specific types of appraisal work.

When Appraisers are assigned to a job there is a required amount of time and paper work that must be submitted from the Appraiser to the Lincoln Appraisal to complete the job.

The email notifications outlined below are designed as automatic reminders to the assigned appraisers based on the particular appraisal job assigned.  This will assist in Appraisers doing their work and submitting the necessary paperwork in a timely fashion.

Upload Directory - \\fileserv\ChinaFTP\Uploads\Lincoln\

File Name – Lincoln.rar   (Contains - Lincoln.BAK)

Technical Description

1.Create Windows Service Called – LincolnEmailNotification.

2.The Windows Service should have installer that will install the Windows Service to default Path – C:\Program Files\OpenBOX Technologies\

3.The Windows Service should have a Config file with the following configurable settings:

   a.RunTime – (This is the Time of day that the service will start up.  The time will be configurable.  Example – Setting this value to 9:00 AM will set the Service to Run at 9 AM)

   b.FromAddress – (This is the email address that will be used as the From Address.)             c.ConnectionString – (This will be a Standard connection string to the Database that will be used.)          

       d.Add the Following field to the Orders Table:  AlertDt Datetime Allows nulls

5.When the Windows Service Runs.  The following 8 Email notifications will be sent to the email address that is stored in Orders.EmailA

6.The Subject, Body and where condition for each Email notification are outlined below.

7.As each Order record is identified and an Alert email is sent the Order.AlertDt field for the specific Order record should be updated with the current date and time.    This value will be used in all of the Where clauses below to prohibit multiple emails being sent on the same day for the same Order record.

 

Email Notification #1

Subject

ASSIGNED STATUS – RETRIEVE THIS ORDER

Body

Please log in and confirm receipt of this order by 2PM EST TODAY as it was assigned yesterday, or the order will be reassigned. If you cannot log in to website or need further assistance please call 401-831-3500, option 2.

SQL Condition    

Where (Orders.Status = “Assigned”) AND (DateDiff(hh, AlertDt ,getdate()) > 12 OR (AlertDt = ‘’ OR AlertDt IS NULL))

 

Email Notification #2

Subject

RECEIVED STATUS – UPDATE w/ INSPECTION DATE

Body

Please update TODAY w/ inspection date.  Report due back by (_Due Date_)!  Thank you.

If you have provided an update, please disregard this notice.  If you need assistance, please call 877-696-5462, option 2.

SQL Condition

Where  Orders.Status = “Received” AND (DateDiff(hh, AlertDt ,getdate()) > 12 OR (AlertDt = ‘’ OR AlertDt IS NULL))

 

Email Notification #3

Subject

DUE TODAY

Body

Report due today!  Please upload by 2PM EST to avoid cancellation and non-payment.  Thank you.

If you have provided an update, please disregard this notice.  If you need assistance, please call 877-696-5462, option 2.

SQL Condition    

Where Orders.DueDate = GetDate() AND (DateDiff(hh, AlertDt ,getdate()) > 12 OR (AlertDt = ‘’ OR AlertDt IS NULL))

 

 

Email Notification #4

Subject

DUE TOMORROW

Body

Friendly reminder that this report is due back to the client by 2 PM EST (due date).  Please submit report as soon as possible.  Thank you!

If you have provided an update, please disregard this notice.  If you need assistance, please call 877-696-5462, option 2.

SQL Condition    

Where Orders.DateDue = Getdate() + 1  AND (DateDiff(hh, AlertDt ,getdate()) > 12 OR (AlertDt = ‘’ OR AlertDt IS NULL))

 

 

Email Notification #5

Subject

REPORT LATE

Body

REPORT LATE - you must upload IMMEDIATELY today, as we did not receive on (due date)!  If report is not submitted, order may be cancelled and appraiser will not be paid.    

If you have provided an update, please disregard this notice.  If you need assistance, please call 877-696-5462, option 2.

SQL Condition

Where Orders.Status = “Scheduled” and Orders.DateDue < GetDate AND (DateDiff(hh, AlertDt ,getdate()) > 12 OR (AlertDt = ‘’ OR AlertDt IS NULL))

 

Email Notification #6

Subject

SCHEDULED TODAY

Body

Inspecting today!  Please attempt to submit report within 24-48 hours.  Thank you!

If you have provided an update, please disregard this notice.  If you need assistance, please call 877-696-5462, option 2.

SQL Condition

Where Orders. Status = “Scheduled” and Orders.InSpDate = GetDate()  AND (DateDiff(hh, AlertDt ,getdate()) > 12 OR (AlertDt = ‘’ OR AlertDt IS NULL))

 

Email Notification #7

Subject

INSPECTED YESTERDAY

Body

Please make every effort to submit report today. Thank you!

If you have provided an update, please disregard this notice.  If you need assistance, please call 877-696-5462, option 2.

SQL Condition

Status = “Scheduled” and Orders.InSpDate = GetDate() – 1  AND (DateDiff(hh, AlertDt ,getdate()) > 12 OR (AlertDt =‘’ OR AlertDt IS NULL))

 

Email Notification #8

Subject

REVISIONS OUTSTANDING

Body

Revisions requested are outstanding.  Please submit revised report immediately to avoid cancellation and nonpayment.  Thank you.

If you have provided an update, please disregard this notice.  If you need assistance, please call 877-696-5462, option 2.

SQL Condition

Where Orders.Status IN (‘Revision’,’In Revision’)  AND (DateDiff(hh, AlertDt ,getdate()) > 12 OR (AlertDt = ‘’ OR AlertDt IS NULL))

 Project Plan

1.      Review Mini - Spec. – 6/25/2008

2.      Discuss understanding of request and formulate any question or concerns regarding the request. – 6/25/2008 - 6/26/2008

3.      Code - 6/27/2008 – 7/4/2008

4.      Test - 7/4/2008

5.      Delivery Providence Office - 7/4/2008

 

下面是我遇到的一些问题,或者说是值得注意的地方:

 1.大多数Service都是隔一段时间触发一次。而我这个业务逻辑,是一天定点只触发一次。这个用到了Timer控件。.Net有2个Timer:一个是System.Web.Form.Timer;一个是System.Timers.Timer。前者事件是_Tick,后者事件是_Elapsed。我用的是后者,好像大家都用这个,后者比前者计算精确。每分钟出发Elapsed事件,判断当前时间是否和配置时间一样,一样才执行相应的逻辑。

2.为了存储复杂对象,自定义了一个结构

     internal class IndividualOrders
        {
            public string email;
            public ArrayList orderIDs;
            public IndividualOrders(string address)
            {
                email = address;
                orderIDs = new ArrayList();
            }
            public void AddOrder(string address, string orderID)
            {
                if (email == address && !orderIDs.Contains(orderID))
                {
                    orderIDs.Add(orderID);
                }
            }
        }

 

这样用:

List<Common.IndividualOrders> orderList = new List<Common.IndividualOrders>();

3.再一个就是发邮件的问题

之前我们把发邮件放到一个线程里,这样建立多个线程,不影响主程序的执行速度,但是当我发送的邮件过多时,会出现邮件发不出去,或者只能发送几封。为了验证问题所在,我去掉了线程,程序运行慢,但是收到98封邮件,而放到线程里就不行。每个线程抛出异常,然后线程就退出了,所以发不出邮件。说明SMTP没有线程同步,send()的时候都是用一个端口发数据的,被卡住了。解决办法:a.放到队列里,排队发。b.join(),一个线程执行完了,再执行另一个。

当Output窗口显示“The thread 0xb78 has exited with code 0 (0x0).”说明该线程执行完毕,邮件发送成功。但是如果抛出异常,则表明线程没有执行完就被迫停止,即邮件没有发送成功。之所以发不了邮件,是因为多个线程公用一段代码,一个线程还没有执行完,就接着执行下个另一个线程,造成混乱,所以异常。

4.打包安装的问题

Setup弄好后,编译安装。但是卸载时出了很多问题。我没有停止服务就卸载了程序,或者修改代码后,再代码里的Setup工程中卸载,这些都会导致卸载不干净,导致无法重新安装。解决办法:打开注册表,遍及-Find,把所有和该项目已经该安装项目的文件夹,Key,value全部删掉,再把安装路径下的文件夹删掉,就OK了。

正确的操作是,先把服务停了,再在控制面板里把程序卸载了。这样可以删除的干净,即使代码有修改,也没关系。最好把安装的msi保存好,可以利用msi卸载。

下面这两个属性决定你的程序的安装路径。比如这两个属性值为test,那么默认安装在C:\Program Files\test\

 

还有,我的程序中有个App.config文件,我从中读取信息,并完成相应的操作。当我的程序安装后,在安装路径下有个.exe.config文件,修改这个文件的配置信息,保存后,重起服务就OK了,不用重新安装程序。

5.单步调试Service

安装好代码程序后,打开服务。然后

    untitled.bmp

   附加上自己的服务,就可以单步调试了。

   6.添加日志

  添加了System.Diagnostics.EventLog。

  7.在T-SQL获取当前日期

  CONVERT(varchar(10), GETDATE(), 120)

   

posted on 2008-07-28 16:39  美丽心情11  阅读(685)  评论(0编辑  收藏  举报