我的.NET Windows服务无法启动

时间:2016-01-03 21:48:05

标签: c# .net service

我编写了这个基本上是文件监视器的.NET Windows服务应用程序。该服务将监视传入的.csv文件,从中解析数据,并将数据添加到电子表格中。我在服务器上安装了该服务并尝试启动它。我得到了警告,"服务开始然后停止。如果某些服务未被其他服务或程序使用,则会自动停止。"在调试尝试中,我删除了所有代码,只是有一个裸服务,它启动/停止正常。所以我添加了文件观察器对象,它再次弹出警告。接下来,我将服务更改为使用本地管理帐户而不是" LocalService"帐户然后它工作。我添加了其余的代码,它工作了一段时间。我完成了开发并添加了EventLog对象,我又回到了警告状态。我删除了EventLog对象,但仍然收到了警告。我只是不知道造成这种情况的原因是什么。这是我的服务:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using OfficeOpenXml;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace COD_Automation
{
public partial class COD_AUTO : ServiceBase
{
    FileSystemWatcher eWatcher;
    String remoteSrc;
    public static String filePath;
    public static String fileName;
    public static Boolean fileCheck;
    public static String modifiedDT;
    public static String remodifiedDT;
    public static String SampNum;
    public static String SampDate;
    public static String AnalysisInitials;
    public static int SampResult;
    public static double Dilution;
    public static FileInfo efile;
    public int rowIndex = 8;
    public int filterID = 1;

    public COD_AUTO()
    {
        InitializeComponent();

        if (!System.Diagnostics.EventLog.SourceExists("COD_Automation"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "COD_Automation", "COD Automation Log");
        }

        serviceLog.Source = "COD_Automation";
        serviceLog.Log = "COD Automation Log";
    }

    protected override void OnStart(string[] args)
    {
        serviceLog.WriteEntry("COD Automation Service has started.");

        //Define the remote folder location to watch
        remoteSrc = "\\\\mkfiler01\\ops\\Envcom\\EXEC\\ENVCOM\\LAB\\COD\\Exports\\DataLog";

        //Create a new FileSystemWatcher and set its properties
        eWatcher = new FileSystemWatcher(remoteSrc, "*.csv");

        //Add event handler
        eWatcher.Created += new FileSystemEventHandler(eWatcher_Created);

        //Begin watching
        eWatcher.EnableRaisingEvents = true;
    }

    protected override void OnStop()
    {
        serviceLog.WriteEntry("COD Automation Service has stopped.");
        eWatcher.EnableRaisingEvents = false;
    }

    private void eWatcher_Created(object source, FileSystemEventArgs e)
    {
        filePath = e.FullPath;
        fileName = e.Name;
        ParseData(filePath);
        FileCheck(fileName);
        CreateExcelFile(fileCheck);
        AddSample(SampNum, SampDate, AnalysisInitials, SampResult, Dilution);
    }

    public void ParseData(String filePath)
    {
        //Create a dictionary collections with int keys (rowNums) and String values (each line of Strings)
        Dictionary<int, String> eachCSVLine = new Dictionary<int, string>();

        //String array that holds the contents of the specified row
        String[] lineContent;

        int rowNum = 1;

        foreach (string line in File.ReadLines(filePath))
        {
            eachCSVLine.Add(rowNum, line);
            rowNum++;
        }

        //Get the required line and split it by "," into an array
        String reqLine = eachCSVLine[5];
        lineContent = reqLine.Split(',');

        //Get the required values(index 2 for parsed Operator ID, index 4 for parsed Sample Number, index 11 for Sample Result)
        AnalysisInitials = lineContent.GetValue(2).ToString();
        SampNum = lineContent.GetValue(3).ToString();      //sample number            

        String result = lineContent.GetValue(11).ToString();
        String dilute = lineContent.GetValue(8).ToString();
        Dilution = Double.Parse(dilute);
        SampResult = Int32.Parse(result);    //sample result
    }

    public void AddSample(String SampleNum, String SampleDate, String AnalysisInitials, int SampleResult, double Diluted)
    {
        try
        {
            using (ExcelPackage excelPackage = new ExcelPackage(efile))
            {
                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
                var cell = worksheet.Cells;

                //check to see if this is the first sample added --if true, add the first sample --if false, increment rowindex & filterID then add the sample to next available row
                if (cell["A8"].Value == null)
                {
                    cell["B5"].Value = SampleDate;
                    cell["B6"].Value = AnalysisInitials;
                    cell[rowIndex, 1].Value = filterID;  //Filter ID
                    cell[rowIndex, 2].Value = SampleNum;   //Sample Number
                    cell[rowIndex, 3].Value = Dilution;   //Dilution
                    cell[rowIndex, 4].Value = SampleResult;   //Meter Reading
                }
                else
                {
                    while (!(cell["A8"].Value == null))
                    {
                        rowIndex++;
                        filterID++;
                        if (cell[rowIndex, 1].Value == null)  //ensures that the new row is blank so the loop can break to continue adding the sample
                        { break; }
                    }

                    //add the sample to the next empty row
                    cell[rowIndex, 1].Value = filterID;  //Filter ID
                    cell[rowIndex, 2].Value = SampleNum;   //Sample Number
                    cell[rowIndex, 3].Value = Dilution;   //Dilution
                    cell[rowIndex, 4].Value = SampleResult;   //Meter Reading
                }
                excelPackage.Save();
            }
        }
        catch (Exception e)
        {
            serviceLog.WriteEntry("Sample could not be added to the spreadsheet because of the following error: " + e.Message + ".");
        }
    }

    public Boolean FileCheck(String fileName)
    {
        //Get the date of the .csv file
        String[] fNames = fileName.Split('_');
        String fDate = fNames.ElementAt(3);
        DateTime dt = Convert.ToDateTime(fDate);

        //format the file date into the proper format and convert to a string
        modifiedDT = dt.ToString("MMddyy");

        //modify the "modifiedDT to get the sample date to insert into spreadsheet            
        String mdate = modifiedDT.Insert(2, "/");
        remodifiedDT = mdate.Insert(5, "/");
        SampDate = remodifiedDT;         //sample date           

        //assign an excel filename
        String exFile = @"\\mkfiler01\ops\Envcom\EXEC\ENVCOM\LAB\COD\Imports\" + modifiedDT + "COD-" + AnalysisInitials + ".xlsx";

        //check for file existence
        if (File.Exists(exFile))
        { fileCheck = true; }
        else
        { fileCheck = false; }

        return fileCheck;
    }

    public void CreateExcelFile(Boolean fileCheck)
    {
        if (fileCheck)
        {
            efile = new FileInfo(@"\\mkfiler01\ops\Envcom\EXEC\ENVCOM\LAB\COD\Imports\" + modifiedDT + "COD-" + AnalysisInitials + ".xlsx");

            using (ExcelPackage excelPackage = new ExcelPackage(efile))
            {
                //Read the existing file to see if the Analysis Initials match the AnalysisInitial variable value
                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
                String initials = worksheet.Cells["B6"].Value.ToString();

                //If initials = AnalysisIntials then assign the existing file the WB variable, else create a new file for the different AnalysisInitials
                if (initials.Equals(AnalysisInitials))
                {
                    excelPackage.Save();
                }
                else
                {
                    try
                    {
                        //Excel COD Template to use to create new Excel spreadsheet
                        FileInfo template = new FileInfo(@"\\mkfiler01\ops\Envcom\EXEC\ENVCOM\LAB\COD\COD TEMPLATE.xlsx");

                        //The new Excel spreadsheet filename
                        FileInfo newFile = new FileInfo(@"\\mkfiler01\ops\Envcom\EXEC\ENVCOM\LAB\COD\Imports\" + modifiedDT + "COD-" + AnalysisInitials + ".xlsx");

                        // Using the template to create the newfile
                        using (ExcelPackage excelPackage1 = new ExcelPackage(newFile, template))
                        {
                            // save the new Excel spreadsheet                                
                            excelPackage1.Save();
                        }
                    }
                    catch (Exception ex)
                    {
                        serviceLog.WriteEntry("Excel file could not be created because " + ex.Message);
                    }
                }
            }
        }
        else
        {
            try
            {
                efile = new FileInfo(@"\\mkfiler01\ops\Envcom\EXEC\ENVCOM\LAB\COD\Imports\" + modifiedDT + "COD-" + AnalysisInitials + ".xlsx");

                //Excel COD Template to use to create new Excel spreadsheet
                FileInfo template = new FileInfo(@"\\mkfiler01\ops\Envcom\EXEC\ENVCOM\LAB\COD\COD TEMPLATE.xlsx");

                //The new Excel spreadsheet filename
                FileInfo newFile = new FileInfo(@"\\mkfiler01\ops\Envcom\EXEC\ENVCOM\LAB\COD\Imports\" + modifiedDT + "COD-" + AnalysisInitials + ".xlsx");

                // Using the template to create the newfile
                using (ExcelPackage excelPackage = new ExcelPackage(newFile, template))
                {
                    // save the new Excel spreadsheet                        
                    excelPackage.Save();
                }
            }
            catch (Exception ex)
            {
                serviceLog.WriteEntry("Excel file could not be created because " + ex.Message);
            }
        }

    }        
}

}

2 个答案:

答案 0 :(得分:1)

我的EventViewer显示了有关EventLog对象的Source和Log属性的ArgumentException的详细信息。我更改了以下代码:

if (!System.Diagnostics.EventLog.SourceExists("COD_Automation"))
    {
        System.Diagnostics.EventLog.CreateEventSource(
            "COD_Automation", "COD Automation Log");
    }

    serviceLog.Source = "COD_Automation";
    serviceLog.Log = "COD Automation Log";

进入以下代码:

if (!EventLog.SourceExists("COD_Automation"))
        {
            EventLog.CreateEventSource("COD_Automation", "Application");
        }

        serviceLog.Source = "COD_Automation";
        serviceLog.Log = "Application";

这解决了我的问题。我最初尝试在COD_Automation日志中注册COD_Automation源,该日志不存在。所以我将Log属性设置为“Application”的正确日志。

答案 1 :(得分:0)

LocalService帐户无权访问\\mkfiler01等UNC分享。我的猜测是,您在尝试访问该文件共享时收到拒绝访问错误。

将服务设置为在有权访问该共享的域帐户下运行。