我的Windows服务安装不正确?

时间:2013-11-05 13:50:21

标签: c# .net windows-services

我有一个Windows服务,OnStart方法包含一个未启动的应用程序。我添加了

System.Diagnostics.Debugger.Launch();

在OnStart()内部并进入代码。

protected override void OnStart(string[] args)
    { 
        System.Diagnostics.Debugger.Launch();
        System.Timers.Timer aTimer = new System.Timers.Timer(500); //500 MilliSeconds
        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        // Set the Interval to 2 seconds (2000 milliseconds).
        //aTimer.Interval = 600000;  //10 minutes //2000; //2 seconds (2000 mili seconds)
        aTimer.AutoReset = false;
        aTimer.Enabled = true;
        // Keep the timer alive until the end of Main.  This allows OnStart to close in a timely fashion.
        GC.KeepAlive(aTimer);

    }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        IvrApplication.Start();// The code didn't reach here.
    }

根本没有执行IvrApplication.Start()。所以我认为我的安装不正确。

我的安装代码:

[MTAThread]
    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            if (IsInstalled() && !Environment.UserInteractive)
            {
                // Run your service normally.
                ServiceBase[] ServicesToRun = new ServiceBase[] { new IvrService() };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                if (Environment.UserInteractive)
                    Console.WriteLine("Debug the service only.");
                if (!IsInstalled())
                    Console.WriteLine("Service is not installed..");
                if (svcPath.ToLower() != "services.exe")
                {
                    // In an interactive session.
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new IvrInteractive());
                    return;
                }
            }
        }
        else if (args.Length == 1)
        {
            switch (args[0])
            {
                case "-install":
                    InstallService();
                    // Here to install the service.
                    break;
                case "-uninstall":
                    StopService();
                    UninstallService();
                    break;

对于InstallService,我们有:

    public static bool  InstallService(string svcPath, string svcName, string svcDispName)
    {
        #region Constants declaration.
        int SC_MANAGER_CREATE_SERVICE = 0x0002;
        int SERVICE_WIN32_OWN_PROCESS = 0x00000010;
        //int SERVICE_DEMAND_START = 0x00000003;
        int SERVICE_ERROR_NORMAL = 0x00000001;
        int STANDARD_RIGHTS_REQUIRED = 0xF0000;
        int SERVICE_QUERY_CONFIG = 0x0001;
        int SERVICE_CHANGE_CONFIG = 0x0002;
        int SERVICE_QUERY_STATUS = 0x0004;
        int SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
        int SERVICE_START = 0x0010;
        int SERVICE_STOP = 0x0020;
        int SERVICE_PAUSE_CONTINUE = 0x0040;
        int SERVICE_INTERROGATE = 0x0080;
        int SERVICE_USER_DEFINED_CONTROL = 0x0100;
        int SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED |
        SERVICE_QUERY_CONFIG |
        SERVICE_CHANGE_CONFIG |
        SERVICE_QUERY_STATUS |
        SERVICE_ENUMERATE_DEPENDENTS |
        SERVICE_START |
        SERVICE_STOP |
        SERVICE_PAUSE_CONTINUE |
        SERVICE_INTERROGATE |
        SERVICE_USER_DEFINED_CONTROL);
        int SERVICE_AUTO_START = 0x00000002;
        #endregion Constants declaration.
        try
        {
            IntPtr sc_handle = OpenSCManager(null, null, SC_MANAGER_CREATE_SERVICE);
            if (sc_handle.ToInt32() != 0)
            {
                IntPtr sv_handle = CreateService(sc_handle, svcName, svcDispName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, svcPath, null, 0, null, null, null);
                if (sv_handle.ToInt32() == 0)
                {
                    CloseServiceHandle(sc_handle);
                    return false;
                }
                else
                {
                    //now trying to start the service
                    int i = StartService(sv_handle, 0, null);
                    // If the value i is zero, then there was an error starting the service.
                    // note: error may arise if the service is already running or some other problem.
                    if (i == 0)
                    {
                        Console.WriteLine("Couldnt start service");
                        return false;
                    }
                    Console.WriteLine("Service started successfully");
                    CloseServiceHandle(sc_handle);
                    return true;
                }
            }
            else
            {
                Console.WriteLine("SCM not opened successfully");
                return false;
            }
        }
        catch (Exception e)
        {
            throw e;
        }
    }

2 个答案:

答案 0 :(得分:0)

您正在错误地使用GC.KeepAlive方法。

  

引用指定的对象,使其不符合垃圾条件   集合从当前例程的开始到其中的点   这种方法叫做

http://msdn.microsoft.com/en-us/library/system.gc.keepalive%28v=vs.110%29.aspx

只需将aTimer声明为Windows服务类的字段,不要使用GC.KeepAlive。

答案 1 :(得分:0)

我发现了这个问题。我的代码是对的,只是别的东西。我忘了将项目类型设置为windows服务。