简体   繁体   English

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

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

I have build an application to parse Xml file for integrating data in mssql database.我已经构建了一个应用程序来解析用于在 mssql 数据库中集成数据的 XML 文件。 I'm using Visual c# express.我正在使用 Visual c# express。 There's a way to make service with express edition or I a have to get Visual Studio to do it?有一种方法可以使用快速版提供服务,还是我必须让 Visual Studio 来做?

Absolutely you can.绝对可以。 You can even do it with csc .你甚至可以用csc来做。 The only thing in VS is the template . VS 中唯一的东西就是模板 But you can reference the System.ServiceProcess.dll yourself.但是您可以自己引用 System.ServiceProcess.dll。

Key points:关键点:

  • write a class that inherits from ServiceBase编写一个继承自ServiceBase的类
  • in your Main() , use ServiceBase.Run(yourService)在您的Main()中,使用ServiceBase.Run(yourService)
  • in the ServiceBase.OnStart override, spawn whatever new thread etc you need to do the work ( Main() needs to exit promptly or it counts as a failed start)ServiceBase.OnStart覆盖中,生成完成工作所需的任何新线程等( Main()需要立即退出,否则会被视为启动失败)

Sample Code示例代码

Very basic template code would be:非常基本的模板代码是:

Program.cs :程序.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 : 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 : 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);
  } 
}  

And a .NET service application is not installed the same way as normal service application (ie you can't use cron.exe /install or some other command line argument. Instead you must use the .NET SDK's InstallUtil : .NET 服务应用程序的安装方式与普通服务应用程序不同(即,您不能使用cron.exe /install或其他命令行参数。相反,您必须使用 .NET SDK 的InstallUtil

InstallUtil /LogToConsole=true cron.exe

Resources资源

You could try Visual Web Developer Express along with the coding4fun developer kit library for web services (via managed code wrappers):-您可以尝试使用 Visual Web Developer Express 以及用于 Web 服务的 coding4fun 开发人员工具包库(通过托管代码包装器):-

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

Express Edition can write and compile Windows Service projects, but it can't create them normally. Express版可以编写和编译Windows Service项目,但不能正常创建。 The only easy way is to create a new service project in Visual Studio, and then copy the project files to the machine with Express Edition.唯一简单的方法是在 Visual Studio 中创建一个新的服务项目,然后将项目文件复制到使用 Express Edition 的机器上。 After that you don't need Visual Studio any more.之后,您不再需要 Visual Studio。

There are 3 ways to accomplish this:有3种方法可以做到这一点:

  • Go to a machine with Visual Studio and create the project使用 Visual Studio 转到机器并创建项目
  • Google an online tutorial for creating services and download the project source code谷歌一个创建服务的在线教程和下载项目源代码
  • Download and install a 90 day trial version of Visual Studio to create the project下载并安装 Visual Studio 的 90 天试用版以创建项目

However you'll still run into some difficulties such as if you want to rename your project or not have to repeat this for every new service.但是,您仍然会遇到一些困难,例如,如果您想重命名您的项目,或者不必为每个新服务重复此操作。 The best long term solution is to get the boss to finally pay for a copy of Standard or Professional edition.最好的长期解决方案是让老板最终支付标准版或专业版的副本。

According to this MSDN article you don't have the project template for a service.根据这篇MSDN 文章,您没有服务的项目模板。 But I'm pretty sure that if you know what the template does for you, you can create and compile a service.但我很确定,如果您知道模板为您做了什么,您就可以创建和编译服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM