C#中的Windows服务问题

时间:2011-02-28 08:49:52

标签: c# windows windows-services

我设计了一个窗口服务,在其中我从OnStart()调用了RunProgram方法..但是当我安装它的pakage时它没有在服务控制台中显示....任何建议都是最受欢迎的....

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


        }



    }
}

4 个答案:

答案 0 :(得分:0)

你有任何记录文件吗?也许你的网络服务有一些错误。您也可以调试您的Web服务。

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


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


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

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

答案 1 :(得分:0)

你试过这个吗? http://msdn.microsoft.com/en-us/library/zt39148a.aspx 我记得在.NET之前的旧服务中你也应该注册安装它的服务 安装程序有一个特定的密钥,像“autoregister”

答案 2 :(得分:0)

构建服务后,必须从Visual Studio Command Propt运行以下命令:

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

完整信息here

答案 3 :(得分:0)

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

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

2.在设计视图中右键单击并选择添加安装程序。这将创建一个名为 ProjectInstaller.cs 的安装程序类。使用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.

希望这会对你有帮助........