0

我正在尝试一个简单的 Windows 服务,它将检查数据库中是否有任何记录,如果是,则运行报告并发送电子邮件,然后删除记录。我尝试安装此服务,但似乎没有安装。该应用程序显示在添加/删除程序中,但服务本身不存在。为什么这不会安装?

这是我的代码。第一部分是实际的服务。

namespace ReportSender
{
    public partial class EmailReportService : ServiceBase
    {
        private EmailReportApp _app = new EmailReportApp();

        public EmailReportService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (_app == null)
                _app = new EmailReportApp();

            _app.Start();
        }

        protected override void OnStop()
        {
            if (_app != null)
                _app.Stop();
        }


    }
}

第二部分是生成然后通过电子邮件发送报告的代码。我在手动版本中使用此代码,因此它应该可以工作。

namespace ReportSender
{
    class EmailReportApp
    {

        // Private fields
        private Thread _thread;
        private EventLog _log;



        private void Execute()
        {
            // Loop until the thread gets aborted

            try
            {
                while (true)
                {
                    // Check for a new record
                    DataClasses1DataContext dc = new DataClasses1DataContext();

                    foreach (var item in dc.reportsSent1s)
                    {
                        string matchedCaseNumber = item.CaseNumberKey;
                        (new MyReportRenderer()).RenderTest(matchedCaseNumber);

                        dc.reportsSent1s.DeleteOnSubmit(item);
                    }
                    dc.SubmitChanges();

                    // Sleep for one minute 
                    Thread.Sleep(60000);
                }
            }
            catch (ThreadAbortException ex)
            {
                _log.WriteEntry(ex.StackTrace.ToString());

                }
            }


        public void Start()
        {
            if (!EventLog.SourceExists("EventLoggerSource"))
                EventLog.CreateEventSource("EventLoggerSource", "Event Logger");
            _log = new EventLog("EventLoggerSource");
            _log.Source = "EventLoggerSource";

            _thread = new Thread(new ThreadStart(Execute));
            _thread.Start();
        }

        public void Stop()
        {
            if (_thread != null)
            {
                _thread.Abort();
                _thread.Join();
            }
        }

    }


    public class MyReportRenderer
    {

        private rs2005.ReportingService2005 rs;
        private rs2005Execution.ReportExecutionService rsExec;



        public void RenderTest(String matchedCaseNumber)
        {
            string HistoryID = null;
            string deviceInfo = null;
            string encoding = String.Empty;
            string mimeType = String.Empty;
            string extension = String.Empty;
            rs2005Execution.Warning[] warnings = null;
            string[] streamIDs = null;

            rs = new rs2005.ReportingService2005();
            rsExec = new rs2005Execution.ReportExecutionService();
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
            rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
            rs.Url = "http://***.**.***.**/ReportServer_DEVELOPMENT/ReportService2005.asmx";
            rsExec.Url = "http://***.**.***.**/ReportServer_DEVELOPMENT/ReportExecution2005.asmx";


            try
            {
                // Load the selected report.
                rsExec.LoadReport("/LawDept/LawDeptTIC", HistoryID);

                // Set the parameters for the report needed.

                rs2005Execution.ParameterValue[] parameters = new rs2005Execution.ParameterValue[1];
                parameters[0] = new rs2005Execution.ParameterValue();
                parameters[0].Name = "CaseNumberKey";
                parameters[0].Value = matchedCaseNumber;

                rsExec.SetExecutionParameters(parameters, "en-us");

                // get pdf of report 
                Byte[] results = rsExec.Render("PDF", deviceInfo,
                out extension, out encoding,
                out mimeType, out warnings, out streamIDs);

                //pass paramaters for email
                DataClasses1DataContext db = new DataClasses1DataContext();



                var matchedBRT = (from c in db.GetTable<vw_ProductClientInfo>()
                                  where c.CaseNumberKey == matchedCaseNumber
                                  select c.BRTNumber).SingleOrDefault();

                var matchedAdd = (from c in db.GetTable<vw_ProductClientInfo>()
                                  where c.CaseNumberKey == matchedCaseNumber
                                  select c.Premises).SingleOrDefault();


                //send email with attachment
                MailMessage message = new MailMessage("Reports@2341.com", "g@1234.com", "Report for BRT # " + matchedAdd, "Attached if the Tax Information Certificate for the aboved captioned BRT Number");
                MailAddress copy = new MailAddress("123s@gmail.com");
                message.CC.Add(copy);
                SmtpClient emailClient = new SmtpClient("***.**.***.**");
                message.Attachments.Add(new Attachment(new MemoryStream(results), String.Format("{0}" + matchedBRT + ".pdf", "BRT")));
                emailClient.Send(message);
            }

            catch (Exception)
            {


            }
        }
    }
}
4

0 回答 0