下面是我的Windows服务代码。当我调试代码时,我得到错误/异常:

CSMessageUtility的类型初始化式。CSDetails抛出了一个异常。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using CSMessageUtility;

namespace CS_Data_Trasmmiting_Service
{
    public partial class svcCSWinServ : ServiceBase
    {
        //private string sLogFormat;
        //private string sErrorTime;
        private Thread new_thread;
        Logger logObject = new Logger();
        private bool isenable = true;

        public svcCSWinServ()
        {
            InitializeComponent();
            logObject.append("Initialize Service " + DateTime.Now.ToString(), 70);
            CheckForAlarms();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                new_thread = new Thread(new ThreadStart(CheckForAlarms));
                new_thread.Start();
            }
            catch
            {
            }

            logObject.append("Service Started successfully " + DateTime.Now.ToString(), 70);
        }

        protected override void OnStop()
        {
            try
            {
                isenable = false;
                new_thread.Abort();
            }
            catch
            {

            }
            logObject.append("Service Stopped successfully " + DateTime.Now.ToString(), 70);
        }


        void CheckForAlarms()
        {
            try
            {
                while (true)
                {
                    //if((DateTime.Now.ToString("HH:mm") == "18:00"))
                    //{

                        logObject.append("Start Sending Data " +DateTime.Now.ToString(), 70);
                        try
                        {
                            //SendAllInfo();
                            string str = CSMessageUtility.CSDetails.createDHSMessageFormat();
                            Thread.Sleep(2000);
                            string str1 = CSMessageUtility.CSDetails.createEALMessageFormat();
                            Thread.Sleep(2000);
                            string str2 = CSMessageUtility.CSDetails.createProductStatusMessageForamt();
                            Thread.Sleep(2000);
                            string str3 = CSMessageUtility.CSDetails.createEODMessageFormat();
                            Thread.Sleep(2000);
                            string str4 = CSDetails.createProductReceiptEntryatBOSMessageFormat();
                            Thread.Sleep(2000);
                            string str5 = CSMessageUtility.CSDetails.createProductSaleMessageFormat();
                            Thread.Sleep(2000);
                            string str6 = CSMessageUtility.CSDetails.createTotalizerExceptionMessageFormat();
                            Thread.Sleep(2000);
                            //CSMessageUtility.CSDetails.createDailyCOtransferMessageFormat();
                            //Thread.Sleep(2000);

                        }
                        catch (Exception ee)
                        {
                            logObject.append(ee.Message, 70);
                        }
                        logObject.append("Finished Sending Data " +DateTime.Now.ToString(), 70);
                        Thread.Sleep(3000);
                    //}
                    //Thread.Sleep(20000);
                }
            }
            catch (Exception ex)
            {
                logObject.append("Thread Exception: "+ ex.Message + " "+ DateTime.Now.ToString(), 70);

                try
                {
                    new_thread.Abort();
                }
                catch (Exception ex1)
                {
                    logObject.append("Thread Exception: " +ex1.Message + " " + DateTime.Now.ToString(), 70);
                }

                if (isenable == true)
                {
                    new_thread = new Thread(new ThreadStart(CheckForAlarms));
                    new_thread.Start();
                }
            }
        }
    }
}

当前回答

另一种可能导致这种情况的情况是,当你有一段代码调用:

string sParam = **ConfigurationManager.AppSettings["SOME_PARAM"].ToString();

请记住,必须使用OWSTIMER.EXE.CONFIG文件进行配置文件设置。我有一个App.config文件,我试图读取,我得到这个错误,因为在我的作业实例的实例化,我有一行在我的代码是引用配置。AppSettings & Configuration.ConnectionStrings。只要确保你走的是这条路:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

并将您的配置设置放在OWSTIMER.EXE.CONFIG文件中。

其他回答

如果一个类试图在web中获取一个键的值,就会导致这个问题。Config或app。Config,这里没有。

如。 该类有一个静态变量

private static string ClientID = System.Configuration.ConfigurationSettings.AppSettings["GoogleCalendarApplicationClientID"].ToString();

但是网络。config不包含GoogleCalendarApplicationClientID键

该错误将在任何静态函数调用或任何类实例创建时抛出

退出Visual Studio并重新打开它为我解决了这个问题。

当我在Util类中使用静态方法时,我遇到了同样的问题,就像您使用CSMessageUtility.CSDetails一样。

问题是在类的静态初始化过程中(使用静态构造函数),框架也初始化了类中的静态变量(字段)。我有一个静态变量,试图从app.config读取值,app.config缺少相应的设置,从而导致一个未处理的异常。这导致了

"对象引用未设置为对象的实例。"

作为内部异常。

在WPF项目中有一个类似的案例。我的问题是这样写的:

DataTable myTable = FillTable(strMySqlQuery);

FillTable()返回一个基于SQL查询字符串的数据表。如果我做了“复制例外到剪贴板”选项,我认为它是,并粘贴到记事本,我可以看到消息。对我来说,它是输入不是一个有效的Base-64字符串,因为它包含一个非Base-64字符。

My actual problem wasn't that the query string had something that shouldn't be there, like I was thinking, because string strMySqlQuery = "SELECT * FROM My_Table" was my string and thought it could be the * or _, but the actual problem was in FillTable(), where I had a call to another function, GetConnection() that returned an OracleConnection object, in order to open it and retrieve and return the DataTable. Inside GetConnection() I was getting the app.config parameters for my connection string, and I had one of them misnamed, so it was setting a null value for the service account's password and not making the DB connection. So it's not always where the error is exactly correct for all circumstances. Best to dive into the function where the error is and debug step-by-step and ensure all values are getting filled with what you expect.

如果由于任何原因断电或Visual Studio IDE崩溃,它会在你的bin/debug bin/release中导致这个问题…

只需删除内容并重新编译(从我的个人经验来看,当我的脚趾碰到复位按钮!)