如何使用Visual c#Express制作服务应用程序?

如何使用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

我已经构建了一个应用程序来解析Xml文件,以便将数据集成到mssql数据库中。我正在使用Visual c#express。有一种方法可以使用express edition提供服务,或者我必须让Visual Studio来提供服务?

当然可以。您甚至可以使用
csc
完成此操作。VS中唯一的东西是模板。但是您可以自己引用System.ServiceProcess.dll

要点:

  • 编写一个继承自
    ServiceBase
  • 在您的
    Main()
    中,使用
    ServiceBase.Run(您的服务)
  • ServiceBase.OnStart
    override中,生成执行该工作所需的任何新线程等(
    Main()
    需要立即退出,否则将被视为启动失败)
示例代码 非常基本的模板代码是:

程序.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);
  } 
}  
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
资源
  • 马克·斯特劳迈尔
  • 戴夫·费特曼

您可以尝试使用Visual Web Developer Express以及Web服务的coding4fun开发工具包库(通过托管代码包装器):-


Express Edition可以编写和编译Windows服务项目,但无法正常创建它们。唯一简单的方法是在Visual Studio中创建一个新的服务项目,然后使用Express Edition将项目文件复制到计算机上。之后,您不再需要Visual Studio

有三种方法可以实现这一点:

  • 使用Visual Studio转到计算机并创建项目
  • 谷歌提供了一个创建服务的在线教程,并下载了项目源代码
  • 下载并安装Visual Studio的90天试用版以创建项目
但是,您仍然会遇到一些困难,例如您是否要重命名您的项目,或者不必为每项新服务重复此操作。最好的长期解决方案是让老板最终支付标准版或专业版的副本。

根据MSDN文章,您没有服务的项目模板。
但是我很确定,如果你知道模板为你做了什么,你可以创建和编译一个服务。

在Windows中,“服务”是指非常具体的东西。你真的是指像“services.msc”所示的服务吗?请看别忘了安装程序类。;)很好的例子,谢谢,我已经成功地尝试过了。我想添加一些信息:1)将项目创建为“Windows控制台应用程序”;2) InstallUtil应在Visual Studio开发者Shell中运行,以管理员身份启动;3) 使用InstallUtil的选项/u卸载服务。