C# 服务器2008上的Windows filewatcher服务崩溃
我制作了一个filewatcher服务,它读取目录中的每个XML文件,并将内容打印在标签上(Visual Studio 2012,Report viewer 2010)。这项服务在Windows8上运行良好,但在Server2008R2上,每次复制目录中的文件时,我都会崩溃C# 服务器2008上的Windows filewatcher服务崩溃,c#,windows-services,report-viewer2010,C#,Windows Services,Report Viewer2010,我制作了一个filewatcher服务,它读取目录中的每个XML文件,并将内容打印在标签上(Visual Studio 2012,Report viewer 2010)。这项服务在Windows8上运行良好,但在Server2008R2上,每次复制目录中的文件时,我都会崩溃 public partial class MyService : ServiceBase { public MyService () { InitializeComponent();
public partial class MyService : ServiceBase
{
public MyService ()
{
InitializeComponent();
GetSourcePath();
}
private void GetSourcePath()
{
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Company\\MyService", false);
if (myKey == null)
{
fsw.Path = @"C:\Source\";
}
else
{
fsw.Path = (string)myKey.GetValue("SourcePath");
}
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
fsw.EnableRaisingEvents = true;
}
protected override void OnPause()
{
base.OnPause();
fsw.EnableRaisingEvents = false;
}
protected override void OnContinue()
{
base.OnContinue();
GetSourcePath();
fsw.EnableRaisingEvents = true;
}
private void fsw_Created(object sender, FileSystemEventArgs e)
{
System.Threading.Thread.Sleep(2000);
ReportViewer reportViewer = new ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.LocalReport.ReportPath = @"rptLabel.rdlc";
reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet", PrintLabel.GetPrintLabels(e.FullPath)));
reportViewer.RefreshReport();
AutoPrint.Export(reportViewer.LocalReport);
AutoPrint.Print();
}
protected override void OnStop()
{
fsw.EnableRaisingEvents = false;
}
}
这里是另一个类的代码
public class AutoPrint
{
private static int m_currentPageIndex;
private static IList<Stream> m_streams;
public static Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
public static void Export(LocalReport report)
{
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>10cm</PageWidth>
<PageHeight>4cm</PageHeight>
<MarginTop>0</MarginTop>
<MarginLeft>0</MarginLeft>
<MarginRight>0</MarginRight>
<MarginBottom>0</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
try
{
report.Render("Image", deviceInfo, CreateStream, out warnings);
}
catch (Exception exc)
{
System.Diagnostics.EventLog.WriteEntry("My Service", DateTime.Now.ToLongTimeString() + " Error rendering print : " + exc.Message);
foreach (Stream stream in m_streams)
{
stream.Position = 0;
}
}
}
public static void Print()
{
PrintDocument printDoc = new PrintDocument();
if(printDoc.PrinterSettings.IsDefaultPrinter) printDoc.PrinterSettings.PrinterName = "Printer Name";
if (m_streams == null || m_streams.Count == 0) System.Diagnostics.EventLog.WriteEntry("MyService", DateTime.Now.ToLongTimeString() + " Error: no stream to print.");
if (!printDoc.PrinterSettings.IsValid)
{
System.Diagnostics.EventLog.WriteEntry("MyService", DateTime.Now.ToLongTimeString() + " Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
}
}
public static void PrintPage(object sender, PrintPageEventArgs ev)
{
try
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
// Adjust rectangular area with printer margins.
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
// Draw a white background for the report
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Draw the report content
ev.Graphics.DrawImage(pageImage, adjustedRect);
// Prepare for the next page. Make sure we haven't hit the end.
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
catch (Exception exc)
{
System.Diagnostics.EventLog.WriteEntry("MyService", DateTime.Now.ToLongTimeString() + " Error rendering print page: " + exc.Message + "Inner exception :" + exc.InnerException );
}
}
}
公共类自动打印
{
私有静态int m_currentPageIndex;
私有静态IList m_流;
公共静态流CreateStream(字符串名称、字符串文件名扩展名、编码编码、字符串mimeType、bool willSeek)
{
Stream=新的MemoryStream();
m_streams.Add(stream);
回流;
}
公共静态无效导出(LocalReport)
{
字符串设备信息=
@"
电动势
10厘米
4厘米
0
0
0
0
";
警告[]警告;
m_streams=新列表();
尝试
{
report.Render(“Image”、deviceInfo、CreateStream、out警告);
}
捕获(异常exc)
{
System.Diagnostics.EventLog.WriteEntry(“我的服务”,DateTime.Now.ToLongTimeString()+“呈现打印时出错:”+exc.Message);
foreach(m_streams中的流)
{
流位置=0;
}
}
}
公共静态无效打印()
{
PrintDocument printDoc=新的PrintDocument();
if(printDoc.PrinterSettings.IsDefaultPrinter)printDoc.PrinterSettings.PrinterName=“打印机名称”;
if(m|u streams==null | m|streams.Count==0)System.Diagnostics.EventLog.WriteEntry(“MyService”,DateTime.Now.ToLongTimeString()+“错误:没有要打印的流”);
如果(!printDoc.PrinterSettings.IsValid)
{
System.Diagnostics.EventLog.WriteEntry(“MyService”,DateTime.Now.ToLongTimeString()+“错误:找不到默认打印机。”);
}
其他的
{
printDoc.PrintPage+=新的PrintPageEventHandler(PrintPage);
m_currentPageIndex=0;
printDoc.Print();
}
}
公共静态无效打印页(对象发送方,PrintPageEventArgs ev)
{
尝试
{
图元文件pageImage=新图元文件(m_streams[m_currentPageIndex]);
//使用打印机边距调整矩形区域。
矩形adjustedRect=新矩形(
ev.PageBounds.Left-(int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top-(int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
电动汽车(高),;
//为报告绘制白色背景
电动图形。填充矩形(画笔。白色,调整矩形);
//绘制报告内容
ev.Graphics.DrawImage(页面图像,调整后的图像);
//准备下一页。确保我们没有讲到最后。
m_currentPageIndex++;
ev.HasMorePages=(m_currentPageIndex
数据也有一个静态类printLabel,但本例中没有什么有趣的内容。它只加载数据。
下面是来自日志事件的第一条错误消息
应用程序:MyService.exe
框架版本:v4.0.30319
描述:由于未处理的异常,进程已终止。
异常信息:System.IO.FileNotFoundException
堆栈:
在MyService.MyService.fsw_处创建(System.Object、System.IO.FileSystemEventArgs)
在System.IO.FileSystemWatcher.OnCreated处(System.IO.filesystemventargs)
位于System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32,System.String)
在System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32、UInt32、System.Threading.NativeOverlapped*)
在System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32,UInt32,System.Threading.NativeOverlapped*)
这里是事件日志中的第二条消息
出错应用程序名称:MyService.exe,版本:1.0.0.0,时间戳:0x51349c1b
故障模块名称:KERNELBASE.dll,版本:6.1.7601.17932,时间戳:0x50327672
异常代码:0xe0434352
故障偏移量:0x0000c41f
出错进程id:0x2dc4
故障应用程序启动时间:0x01ce18d947393653
出现故障的应用程序路径:C:\Program Files(x86)\Company\MyService\MyService.exe
故障模块路径:C:\Windows\syswow64\KERNELBASE.dll
报告Id:a27c24e2-84cc-11e2-bb34-0019992623e2
我制作了与控制台相同的应用程序,它工作得非常完美。也许服务没有读取文件的权限?我知道有人有一个想法…您得到的是
FileNotFoundException
并且您使用的是相对路径。这意味着最有可能的问题是您有错误的工作目录。尝试使用绝对路径
有关更多信息,请参见我发现了问题。这是reportviewer 2010。安装ReportViewer Service Pack 1后,他似乎不再崩溃。碰撞发生在以下线路上:
ReportViewer reportViewer = new ReportViewer();
我也改变了这一行:
reportViewer.LocalReport.ReportEmbeddedResource = "MyService.rptLabel.rdlc";