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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/569606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:36:38  来源:igfitidea点击:

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

c#service

提问by Bigballs

I have build an application to parse Xml file for integrating data in mssql database. I'm using Visual c# express. There's a way to make service with express edition or I a have to get Visual Studio to do it?

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

采纳答案by Marc Gravell

Absolutely you can. You can even do it with csc. The only thing in VS is the template. But you can reference the System.ServiceProcess.dll yourself.

绝对可以。你甚至可以用csc. VS 中唯一的东西是template。但是您可以自己引用 System.ServiceProcess.dll。

Key points:

关键点:

  • write a class that inherits from ServiceBase
  • in your Main(), use ServiceBase.Run(yourService)
  • in the ServiceBase.OnStartoverride, spawn whatever new thread etc you need to do the work (Main()needs to exit promptly or it counts as a failed start)
  • 写一个继承自的类 ServiceBase
  • 在你的Main(),使用ServiceBase.Run(yourService)
  • 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 (i.e. you can't use cron.exe /installor 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

资源

回答by cyberbobcat

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

回答by David

Express Edition can write and compile Windows Service projects, but it can't create them normally. 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. After that you don't need Visual Studio any more.

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

There are 3 ways to accomplish this:

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

  • Go to a machine with Visual Studio and create the project
  • 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 的机器并创建项目
  • google 一个创建服务的在线教程并下载项目源码
  • 下载并安装 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.

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

回答by Alex Shnayder

According to thisMSDN article you don't have the project template for a service. But I'm pretty sure that if you know what the template does for you, you can create and compile a service.

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