簡體   English   中英

如何使用 Visual c# Express 制作服務應用程序?

[英]How can I make service apps with Visual c# Express?

我已經構建了一個應用程序來解析用於在 mssql 數據庫中集成數據的 XML 文件。 我正在使用 Visual c# express。 有一種方法可以使用快速版提供服務,還是我必須讓 Visual Studio 來做?

絕對可以。 你甚至可以用csc來做。 VS 中唯一的東西就是模板 但是您可以自己引用 System.ServiceProcess.dll。

關鍵點:

  • 編寫一個繼承自ServiceBase的類
  • 在您的Main()中,使用ServiceBase.Run(yourService)
  • ServiceBase.OnStart覆蓋中,生成完成工作所需的任何新線程等( 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());
        }
    }
}

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
        }
    }
}

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);
  } 
}  

.NET 服務應用程序的安裝方式與普通服務應用程序不同(即,您不能使用cron.exe /install或其他命令行參數。相反,您必須使用 .NET SDK 的InstallUtil

InstallUtil /LogToConsole=true cron.exe

資源

您可以嘗試使用 Visual Web Developer Express 以及用於 Web 服務的 coding4fun 開發人員工具包庫(通過托管代碼包裝器):-

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

Express版可以編寫和編譯Windows Service項目,但不能正常創建。 唯一簡單的方法是在 Visual Studio 中創建一個新的服務項目,然后將項目文件復制到使用 Express Edition 的機器上。 之后,您不再需要 Visual Studio。

有3種方法可以做到這一點:

  • 使用 Visual Studio 轉到機器並創建項目
  • 谷歌一個創建服務的在線教程和下載項目源代碼
  • 下載並安裝 Visual Studio 的 90 天試用版以創建項目

但是,您仍然會遇到一些困難,例如,如果您想重命名您的項目,或者不必為每個新服務重復此操作。 最好的長期解決方案是讓老板最終支付標准版或專業版的副本。

根據這篇MSDN 文章,您沒有服務的項目模板。 但我很確定,如果您知道模板為您做了什么,您就可以創建和編譯服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM