代码之家  ›  专栏  ›  技术社区  ›  Bigballs

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

  •  18
  • Bigballs  · 技术社区  · 17 年前

    我已经构建了一个应用程序来解析用于在MSSQL数据库中集成数据的XML文件。我正在使用Visual C Express。有一种方法可以用Express Edition提供服务,还是我必须让Visual Studio来做?

    4 回复  |  直到 13 年前
        1
  •  61
  •   jdknight    13 年前

    当然可以。你甚至可以用 csc . 在vs中唯一的东西是 模板 . 但您可以自己引用System.ServiceProcess.dll。

    要点:

    • 编写从继承的类 ServiceBase
    • 在你 Main() 使用 ServiceBase.Run(yourService)
    • ServiceBase.OnStart 重写、生成执行该工作所需的任何新线程等( 主体() 需要立即退出或算作启动失败)

    样例代码

    非常基本的模板代码是:

    程序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;
    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
            }
        }
    }
    

    克罗纳安装程序.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);
      } 
    }  
    

    并且.NET服务应用程序的安装方式与普通服务应用程序不同(即,您不能使用 cron.exe /install 或者其他命令行参数。相反,您必须使用.NET SDK InstallUtil :

    InstallUtil /LogToConsole=true cron.exe
    

    资源

        2
  •  0
  •   cyberbobcat    17 年前

    您可以尝试使用Visual Web Developer Express和Coding4Fun Developer Kit Library for Web Services(通过托管代码包装器):。-

    http://www.microsoft.com/express/samples/c4fdevkit/default.aspx

        3
  •  0
  •   David    17 年前

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

    实现这一点有三种方法:

    • 转到带有Visual Studio的计算机并创建项目
    • 谷歌在线教程,用于创建服务和下载项目源代码
    • 下载并安装90天的Visual Studio试用版以创建项目

    但是,您仍然会遇到一些困难,例如,如果您想要重命名项目,或者不必为每个新服务重复此操作。最好的长期解决方案是让老板最终支付一份标准版或专业版的费用。

        4
  •  0
  •   Alex Shnayder    17 年前

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