我试图将生成pdf报告的控制台应用程序转换为Windows服务.我的代码如下.我正朝着正确的方向前进吗?我安装了这项服务,启动/停止工作正常,但没有生成报告!仅控制台应用程序可以正常生成Output.pdf.我的目标是在服务启...
我试图将生成pdf报告的控制台应用程序转换为Windows服务.我的代码如下.我正朝着正确的方向前进吗?我安装了这项服务,启动/停止工作正常,但没有生成报告!仅控制台应用程序可以正常生成Output.pdf.我的目标是在服务启动时生成输出.
class Program : ServiceBase
{
public Program()
{
this.ServiceName = "My PdfGeneration";
}
static void Main(string[] args)
{
ServiceBase.Run(new Program());
}
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();
}
protected override void OnStop()
{
EventLog.WriteEntry("My PdfGeneration Stopped");
base.OnStop();
}
}
解决方法:
我建议你将OnStart事件中的代码移动到一个单独的线程,因为
您的服务需要及时启动,否则可能会超时
在启动时.
例如
using System.ServiceProcess;
using System.Threading;
namespace myService
{
class Service : ServiceBase
{
static void Main()
{
ServiceBase.Run(new Service());
}
public Service()
{
Thread thread = new Thread(Actions);
thread.Start();
}
public void Actions()
{
// Do Work
}
}
}
您可能还想检查执行用户(运行服务的用户上下文)
拥有您正在写入的文件夹的权限等.
您还需要将错误写入事件日志,而不是将其写入
控制台窗口就像在您的代码段中看到的那样(您的代码目前正在吞咽异常
这就是为什么你不能指出什么是错的)
阅读更多内容:
C# Basics: Creating a Windows Service
沃梦达教程
本文标题为:c# – 将控制台应用程序转换为Windows服务
基础教程推荐
猜你喜欢
- unity实现动态排行榜 2023-04-27
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- winform把Office转成PDF文件 2023-06-14
- C# List实现行转列的通用方案 2022-11-02
- C# 调用WebService的方法 2023-03-09
- C#类和结构详解 2023-05-30
- ZooKeeper的安装及部署教程 2023-01-22
- C#控制台实现飞行棋小游戏 2023-04-22
- C# windows语音识别与朗读实例 2023-04-27
- 一个读写csv文件的C#类 2022-11-06