据我所知,Visual Studio Express 没有用于服务的项目模板。这并不意味着您不能使用 VSE 编写 Windows 服务,只是您没有模板可以帮助您入门。
要创建服务,您只需创建一个普通的控制台应用程序。创建负责实际服务实现的服务类。它看起来像这样
using System.ServiceProcess;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
}
然后Service启动代码就可以进入到你的服务的Main函数中了。
using System.ServiceProcess;
namespace WindowsService1
{
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
这应该为您提供服务的基本框架。您需要的另一件事是为该服务添加一个安装程序,以便它可以作为服务安装。以下内容应该可以帮助您入门,请注意我已经对此进行了测试。
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace WindowsService1
{
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller serviceProcessInstaller1;
private ServiceInstaller serviceInstaller1;
public ProjectInstaller()
{
this.serviceProcessInstaller1 = new ServiceProcessInstaller();
this.serviceInstaller1 = new ServiceInstaller();
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
this.serviceInstaller1.ServiceName = "Service1";
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
}
}
鉴于上述情况,您应该有足够的时间搜索或询问有关服务创建的更多详细信息。至于MSMQ监听器,可以参考以下MSDN文章入手
http://msdn.microsoft.com/en-us/library/ms978425.aspx