Converting a Console Application to a service?

前端 未结 5 941
[愿得一人]
[愿得一人] 2021-02-06 05:53

I\'m looking for different ways with strengths/weaknesses for converting a console application we are using long term in to a windows service. We use something called java serv

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-06 06:44

    Some thoughts:

    Create windows service with VS 2005

    install .Net Service

    I wrote some couple of years ago a Perl based set of executables ( theads ) etc. , which seems to have similar requirements to yours ...

    Some stuff to keep in mind:

    • do have debuggin switch ( you should have one when something goes really wrong )
    • output to both console and files ( try log4net for example )
    • build your logging with future regex parsing in mind
    • if there are some inderdependant processes , learn how to kill them , stop and restart them
    • if there are some inderdependant processes try to communicate to them

    Here is a small Console example to output to db, file and console with log4net

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using log4net;
        using log4net.Config;
        using NUnit.Framework;
    
        namespace ExampleConsoleApplication
        {
            [TestFixture]
            class TestClass
            {
    
            //private static readonly ILog logger =
            //     LogManager.GetLogger ( typeof ( TestClass ) );
    
            private static readonly log4net.ILog logger = log4net.LogManager.GetLogger ( System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType );
    
                static void Main ( string[] args )
                {
    
                    Console.WriteLine ( " START " );
                    #region LoggerUsage
                    DOMConfigurator.Configure (); //tis configures the logger 
                    logger.Debug ( "Here is a debug log." );
                    logger.Info ( "... and an Info log." );
                    logger.Warn ( "... and a warning." );
                    logger.Error ( "... and an error." );
                    logger.Fatal ( "... and a fatal error." );
    
                    #endregion LoggerUsage
                    TestClass objTestClass = new TestClass();
                    objTestClass.TestMethodNameOK ();
                    objTestClass.TestMethodNameNOK ();
    
                    Console.WriteLine ( " END HIT A KEY TO EXIT " );
                    Console.ReadLine ();
                    } //eof method 
    
                [SetUp]
                protected void SetUp ()
                {
                    //Add Here the Initialization of the objects 
                }
                [Test ( Description = "Add here the description of this test method " )]
                protected void TestMethodNameOK ()
                { 
                    //build ok use case scenario here - e.g. no exception should be raced '
                    //Vegetable newCarrot = pool.GetItemByPropertyValue ( "WriongByPurpose", "Orange" );
                    //Assert.IsInstanceOfType ( typeof ( Vegetable ), newCarrot );
                    //Assert.AreSame ( newCarrot, carrot );
                    //logger.Info ( " I got the newCarrot which is " + newCarrot.Color );
    
                } //eof method 
    
                [Test ( Description = "Add here the description of this test method " )]
                protected void TestMethodNameNOK ()         //e.g. the one that should raze Exception
                {
                    //build ok use case scenario here - e.g. no exception should be raced '
                    //Vegetable newCarrot = pool.GetItemByPropertyValue ( "WriongByPurpose", "Orange" );
                    //Assert.IsInstanceOfType ( typeof ( Vegetable ), newCarrot );
                    //Assert.AreSame ( newCarrot, carrot );
                    //logger.Info ( " I got the newCarrot which is " + newCarrot.Color );
    
                } //eof method 
    
            } //eof class 
    
        } //eof namespace 
    
    
    
    
    
        #region TheAppConfig
    //    
    //
    //  
    //    
    // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // #endregion TheAppconfig //this is the xml added replace here your log4net and Nunit paths // // False // ..\..\..\Log4Net\log4net-1.2.10\bin\net\2.0\release\log4net.dll // //

提交回复
热议问题