我正在运行服务吗

我目前正在为可以在控制台中运行的服务编写一些引导程序代码。从本质上讲,它归结为调用OnStart()方法,而不是使用ServiceBase来启动和停止服务(因为如果未将其作为服务安装,则它不会运行该应用程序,并且会使调试成为噩梦)。


现在,我正在使用Debugger.IsAttached确定是否应该使用ServiceBase.Run或[service] .OnStart,但是我知道这不是最好的主意,因为有时最终用户希望在控制台中运行该服务(以查看输出等)。


关于如何确定Windows服务控制器是否在控制台中启动“我”或用户是否在控制台中启动“我”的任何想法?显然,Environment.IsUserInteractive不是答案。我考虑过使用命令行参数,但这似乎“肮脏”。


我总是可以看到围绕ServiceBase.Run的try-catch语句,但这似乎很脏。编辑:尝试捕获不起作用。


我有一个解决方案:将其放置在这里,供所有其他感兴趣的堆叠器使用:


    public void Run()

    {

        if (Debugger.IsAttached || Environment.GetCommandLineArgs().Contains<string>("-console"))

        {

            RunAllServices();

        }

        else

        {

            try

            {

                string temp = Console.Title;

                ServiceBase.Run((ServiceBase[])ComponentsToRun);

            }

            catch

            {

                RunAllServices();

            }

        }

    } // void Run


    private void RunAllServices()

    {

        foreach (ConsoleService component in ComponentsToRun)

        {

            component.Start();

        }

        WaitForCTRLC();

        foreach (ConsoleService component in ComponentsToRun)

        {

            component.Stop();

        }

    }

编辑:在StackOverflow上还有另一个问题,那里的人与Environment.CurrentDirectory是“ C:\ Windows \ System32”的问题看起来可能是答案。我今天要测试。


慕田峪9158850
浏览 515回答 3
3回答

宝慕林4294392

像Ash一样,我将所有实际的处理代码编写在单独的类库程序集中,然后由Windows Service可执行文件以及控制台应用程序引用。但是,有时候了解类库是否在服务可执行文件或控制台应用程序的上下文中运行很有用。我这样做的方法是反思托管应用程序的基类。(对VB表示抱歉,但是我认为可以很容易地对C#进行以下修饰):Public Class ExecutionContext&nbsp; &nbsp; ''' <summary>&nbsp; &nbsp; ''' Gets a value indicating whether the application is a windows service.&nbsp; &nbsp; ''' </summary>&nbsp; &nbsp; ''' <value>&nbsp; &nbsp; ''' <c>true</c> if this instance is service; otherwise, <c>false</c>.&nbsp; &nbsp; ''' </value>&nbsp; &nbsp; Public Shared ReadOnly Property IsService() As Boolean&nbsp; &nbsp; &nbsp; &nbsp; Get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' Determining whether or not the host application is a service is&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' an expensive operation (it uses reflection), so we cache the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' result of the first call to this method so that we don't have to&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' recalculate it every call.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' If we have not already determined whether or not the application&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' is running as a service...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If IsNothing(_isService) Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' Get details of the host assembly.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim entryAssembly As Reflection.Assembly = Reflection.Assembly.GetEntryAssembly&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' Get the method that was called to enter the host assembly.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim entryPoint As System.Reflection.MethodInfo = entryAssembly.EntryPoint&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' If the base type of the host assembly inherits from the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' "ServiceBase" class, it must be a windows service. We store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' the result ready for the next caller of this method.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _isService = (entryPoint.ReflectedType.BaseType.FullName = "System.ServiceProcess.ServiceBase")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' Return the cached result.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return CBool(_isService)&nbsp; &nbsp; &nbsp; &nbsp; End Get&nbsp; &nbsp; End Property&nbsp; &nbsp; Private Shared _isService As Nullable(Of Boolean) = Nothing#End RegionEnd Class

江户川乱折腾

什么对我有用:进行实际服务工作的类在单独的线程中运行。此线程从OnStart()方法内部启动,并从OnStop()停止。服务和控制台模式之间的决定取决于 Environment.UserInteractive样例代码:class MyService : ServiceBase{&nbsp; &nbsp; private static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Environment.UserInteractive)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startWorkerThread();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine ("======&nbsp; Press ENTER to stop threads&nbsp; ======");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stopWorkerThread() ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine ("======&nbsp; Press ENTER to quit&nbsp; ======");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Run (this) ;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; protected override void OnStart(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; startWorkerThread();&nbsp; &nbsp; }&nbsp; &nbsp; protected override void OnStop()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; stopWorkerThread() ;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP