如何使用Visual c#Express制作服务应用程序?
我已经构建了一个应用程序来解析Xml文件,以便将数据集成到mssql数据库中。我正在使用Visual c#express。有一种方法可以使用express edition提供服务,或者我必须让Visual Studio来提供服务?当然可以。您甚至可以使用如何使用Visual c#Express制作服务应用程序?,c#,service,C#,Service,我已经构建了一个应用程序来解析Xml文件,以便将数据集成到mssql数据库中。我正在使用Visual c#express。有一种方法可以使用express edition提供服务,或者我必须让Visual Studio来提供服务?当然可以。您甚至可以使用csc完成此操作。VS中唯一的东西是模板。但是您可以自己引用System.ServiceProcess.dll 要点: 编写一个继承自ServiceBase 在您的Main()中,使用ServiceBase.Run(您的服务) 在Service
csc
完成此操作。VS中唯一的东西是模板。但是您可以自己引用System.ServiceProcess.dll
要点:
- 编写一个继承自
ServiceBase
- 在您的
中,使用Main()
ServiceBase.Run(您的服务)
- 在
override中,生成执行该工作所需的任何新线程等(ServiceBase.OnStart
需要立即退出,否则将被视为启动失败)Main()
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());
}
}
}
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);
}
}
CronInstaller.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());
}
}
}
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);
}
}
并且.NET服务应用程序的安装方式与普通服务应用程序不同(即,您不能使用cron.exe/install
或其他一些命令行参数。相反,您必须使用.NET SDK的InstallUtil
:
InstallUtil /LogToConsole=true cron.exe
资源
- 马克·斯特劳迈尔
- 戴夫·费特曼
Express Edition可以编写和编译Windows服务项目,但无法正常创建它们。唯一简单的方法是在Visual Studio中创建一个新的服务项目,然后使用Express Edition将项目文件复制到计算机上。之后,您不再需要Visual Studio 有三种方法可以实现这一点:
- 使用Visual Studio转到计算机并创建项目
- 谷歌提供了一个创建服务的在线教程,并下载了项目源代码
- 下载并安装Visual Studio的90天试用版以创建项目
但是我很确定,如果你知道模板为你做了什么,你可以创建和编译一个服务。在Windows中,“服务”是指非常具体的东西。你真的是指像“services.msc”所示的服务吗?请看别忘了安装程序类。;)很好的例子,谢谢,我已经成功地尝试过了。我想添加一些信息:1)将项目创建为“Windows控制台应用程序”;2) InstallUtil应在Visual Studio开发者Shell中运行,以管理员身份启动;3) 使用InstallUtil的选项/u卸载服务。