none
Converting Console app to Windows Service RRS feed

  • Question

  • Am trying to convert my console application which generates pdf reports- into a windows service.My code is as follows .. Am i on the rite direction? i did install this service and start/stop works fine but no report generated!

        protected override void OnStart(string[] args)
        {
            EventLog.WriteEntry("My PdfGeneration Started");
            //base.OnStart(args);
            //Customise parameters for render method
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;   //"application/pdf";
            string encoding = string.Empty;
            string filenameExtension = string.Empty;
            string deviceInfo = "<DeviceInfo>" + "<OutputFormat>PDF</OutputFormat>" + "<PageWidth>15in</PageWidth>" + "<PageHeight>11in</PageHeight>" + "<MarginTop>0.5in</MarginTop>" + "<MarginLeft>0.5in</MarginLeft>" + "<MarginRight>0.5in</MarginRight>" + "<MarginBottom>0.5in</MarginBottom>" + "</DeviceInfo>";
    
            //Create a SqlConnection to the AdventureWorks2008R2 database. 
            SqlConnection connection = new SqlConnection("data source=localhost;initial catalog=pod;integrated security=True");
    
            //Create a SqlDataAdapter for the Sales.Customer table.
            SqlDataAdapter adapter = new SqlDataAdapter();
    
            // A table mapping names the DataTable.
            adapter.TableMappings.Add("View", "Route_Manifest");
    
            // Open the connection.
            connection.Open();
            Console.WriteLine("\nThe SqlConnection is open.");
    
            // Create a SqlCommand to retrieve Suppliers data.
            SqlCommand command = new SqlCommand("SELECT TOP 10 [RouteID],[FullTruckID],[DriverID],[DriverName],[StopID],[CustomerID],[CustomerName],[InvoiceID],[last_modified],[Amount] FROM [pod].[dbo].[Route_Manifest]", connection);
            command.CommandType = CommandType.Text;
    
            // Set the SqlDataAdapter's SelectCommand.
            adapter.SelectCommand = command;
            command.ExecuteNonQuery();
    
            // Fill the DataSet.
            DataSet dataset = new DataSet("Route_Manifest");
            adapter.Fill(dataset);
    
            //Set up reportviewver and specify path
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;
            viewer.LocalReport.ReportPath = @"C:\Documents and Settings\xxxxx\My Documents\Visual Studio 2008\Projects\PdfReportGeneration\PdfReportGeneration\Report.rdlc";
    
            //specify the dataset syntax = (datasetofreport.rdlc,querydataset); 
            viewer.LocalReport.DataSources.Add(new ReportDataSource("podDataSet_Route_Manifest", dataset.Tables[0]));
    
    
            //Now render it to pdf
            try
            {
                byte[] bytes = viewer.LocalReport.Render("PDF", deviceInfo, out mimeType, out encoding, out filenameExtension, out streamIds, out warnings);
                //output to bin directory 
                using (System.IO.FileStream fs = new System.IO.FileStream("output.pdf", System.IO.FileMode.Create))
                {
                    //file saved to bin directory
                    fs.Write(bytes, 0, bytes.Length);
                }
                Console.WriteLine("\n YEP!! The report has been generated:-)");
    
                /*           //Save report to D:\ -- later
                             FileStream fsi = new FileStream(@"D:\output.pdf", System.IO.FileMode.Create);
                */
            }
            catch (Exception e)
            {
                Console.WriteLine("\n CHEY!!!this Exception encountered:", e);
            }
    
    
            // Close the connection.
            connection.Close();
            Console.WriteLine("\nThe SqlConnection is closed.");
            Console.ReadLine();
    
        }
       

    Also the console app alone works fine to generate Output.pdf .My aim is to Generate ouput on service start.Also On 

    Thursday, October 24, 2013 11:18 AM