我的Windows服务安装不正确?

用户名

我有一个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;
        }
    }
用户名

我发现了问题。我的代码是正确的,仅此而已。我忘记将项目类型设置为Windows服务。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Windows MediaPack安装不正确

来自分类Dev

Virtualenvwrapper安装不正确

来自分类Dev

Vundle安装不正确

来自分类Dev

Xamarin安装不正确

来自分类Dev

Windows 64位程序安装不正确

来自分类Dev

Windows Media Player提示“安装不正确”,无法启动

来自分类Dev

.NET SDK的安装不正确

来自分类Dev

php-fpm安装不正确

来自分类Dev

openjdk-8安装不正确

来自分类Dev

我的IF语句不正确吗?

来自分类Dev

我的Webworkers似乎计算不正确

来自分类Dev

显示时我的输出不正确

来自分类Dev

我的反应表提交不正确

来自分类Dev

为什么我的图像不正确?

来自分类Dev

我的div颜色不正确

来自分类Dev

我的导航抽屉反应不正确

来自分类Dev

我的代码中的变量不正确

来自分类Dev

我的弹出窗口显示不正确

来自分类Dev

我的查询执行不正确

来自分类Dev

我的程序排序不正确

来自分类Dev

我使用文件不正确吗?

来自分类Dev

为什么我的数学不正确

来自分类Dev

Windows 10 V1511-安装外部加密驱动器错误“参数不正确”

来自分类Dev

我未使用任何广告服务时,由于“ IDFA不正确”,应用被拒绝

来自分类Dev

我的服务器TCP(node.js)上的订单执行不正确

来自分类Dev

为什么我的 Google Play 服务版本不正确?

来自分类Dev

Windows 显示日期格式不正确

来自分类Dev

Tomcat Windows服务(Apache Commons procsrv)“用户名或密码不正确”

来自分类Dev

Consul Windows 服务不再以错误“功能不正确”启动