我已经构建了一个解析Xml文件的应用程序,用于在mssql数据库中集成数据.我正在使用Visual c#express.有一种方法可以使用快速版本进行服务,或者我必须让Visual Studio来完成它吗?
Mar*_*ell 62
绝对可以.你甚至可以这样做csc.VS中唯一的东西就是模板.但您可以自己引用System.ServiceProcess.dll.
关键点:
ServiceBaseMain(),使用ServiceBase.Run(yourService)ServiceBase.OnStart覆盖中,产生你需要做的任何新线程等(Main()需要立即退出或者算作失败的开始)非常基本的模板代码是:
Program.cs:
using System;
using System.ServiceProcess;
namespace Cron
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
System.ServiceProcess.ServiceBase.Run(new CronService());
}
}
}
Run Code Online (Sandbox Code Playgroud)
CronService.cs:
using System;
using System.ServiceProcess;
namespace Cron
{
public class CronService : ServiceBase
{
public CronService()
{
this.ServiceName = "Cron";
this.CanStop = true;
this.CanPauseAndContinue = false;
this.AutoLog = true;
}
protected override void OnStart(string[] args)
{
// TODO: add startup stuff
}
protected override void OnStop()
{
// TODO: add shutdown stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
CronInstaller.cs:
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
[RunInstaller(true)]
public class CronInstaller : Installer
{
private ServiceProcessInstaller processInstaller;
private ServiceInstaller serviceInstaller;
public CronInstaller()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Cron"; //must match CronService.ServiceName
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
Run Code Online (Sandbox Code Playgroud)
并且.NET服务应用程序的安装方式与普通服务应用程序的安装方式不同(即您不能使用cron.exe /install或其他一些命令行参数.相反,您必须使用.NET SDK InstallUtil:
InstallUtil /LogToConsole=true cron.exe
Run Code Online (Sandbox Code Playgroud)