繁体   English   中英

C#中的Windows服务问题

[英]Windows Service Issue in C#

我设计了一个窗口服务,在其中我从OnStart()调用RunProgram Method。但是,当我安装它的包装时,它不会显示在服务控制台中。

protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            rd = new Thread(new ThreadStart(RunProgram));
            rd.Start();
        }

我的安装程序类如下。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Management;
using System.ServiceProcess;
using System.Linq;


namespace WindowsService1
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }
        public System.ServiceProcess.ServiceController serviceController = new ServiceController();
        private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
        {
            serviceController.ServiceName = "MyTestingService";
            ConnectionOptions coOptions = new ConnectionOptions();


            coOptions.Impersonation = ImpersonationLevel.Impersonate;

            ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);

            mgmtScope.Connect();

            ManagementObject wmiService;

            wmiService = new ManagementObject("Win32_Service.Name='" + this.serviceController.ServiceName + "'");

            ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");

            InParam["DesktopInteract"] = true;

            ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);

            this.serviceController.Start();
        }
    }
}

我的服务等级如下。

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.Web;
using System.Threading;
namespace WindowsService1
{
    public partial class MyTestingService : ServiceBase
    {
        public MyTestingService()
        {
            InitializeComponent();
        }
        System.Threading.Thread rd;
        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            rd = new Thread(new ThreadStart(RunProgram));
            rd.Start();
        }

        protected override void OnStop()
        {

        }
        public void RunProgram()
        {


           //My Code to do here


        }



    }
}

你有日志文件吗? 您的网络服务中可能存在一些错误。 您也可以调试Web服务。

static void Main()
{
#if (!DEBUG)


            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { new Service1Component() };
            ServiceBase.Run(ServicesToRun);


#else
            Service1Component s = new Service1Component();
            s.RunProgram();
#endif
}

ps s.RunProgram()是您可以将其用于调试的方法。

你尝试过这个吗? http://msdn.microsoft.com/zh-cn/library/zt39148a.aspx我记得在.NET之前的旧服务中,您还应该注册安装该服务的服务。安装程序具有特定的密钥,例如“ autoregister”

构建服务后,必须从Visual Studio命令提示符运行以下命令:

installutil [/u[ninstall]] [options] assembly [[options] assembly] ...

完整的信息在这里

请交叉检查您是否已完成以下步骤:

1.创建Windows服务项目后,转到服务类的设计视图(只需双击service1.cs类)。

2.在设计视图中,右键单击并选择添加安装程序 这将创建一个名为ProjectInstaller.cs的Installer类。 如果没有ProjectInstaller.cs或配置ProjectInstaller.cs时出现任何错误,可能会导致服务控制台中未显示该服务。

3.转到ProjectInstaller.cs的设计视图,您将在那里找到两个安装程序->

a.**ServiceInstaller1**

b.**ServiceProcessInstaller1**  

4.右键单击ServiceInstaller1并转到属性选项卡

a.Edit the ServiceName with the name you want to 
   see your service in the service console.

   b.Change the **StartType** to **Automatic**.

5.右键单击ServiceProcessInstaller1并转到属性选项卡

 a.Change the account to **LocalService**

6. Save and try it.

希望这个能对您有所帮助........

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM