SORU
14 EKİM 2011, Cuma


.NET Windows hizmeti olarak konsol uygulaması

Konsol uygulaması var ve Windows hizmeti olarak çalıştırmak istiyorum. VS2010 proje takın konsol ve Windows hizmet oluşturmak için izin veren bir proje şablonu vardır. İsterim ekleyin ayrılmış hizmeti proje ve mümkünse entegre hizmet kodu konsol uygulamasına devam konsol uygulaması olarak bir proje olabilir çalıştırmak gibi bir konsol uygulaması veya windows hizmeti çalıştırmak için örnek komut satırını kullanarak geçer.

Belki birisi hızla ve kolayca c dönüştürebilir hangi sınıf kütüphane veya kod parçacığını hatırlatıyoruz# servisi için konsol uygulaması mı?

CEVAP
14 EKİM 2011, Cuma


Ben genellikle aşağıdaki techinque bir hizmet olarak bir konsol uygulaması olarak ya da: aynı uygulamayı çalıştırmak için kullanın

public static class Program
{
    #region Nested classes to support running as service
    public const string ServiceName = "MyService";

    public class Service : ServiceBase
    {
        public Service()
        {
            ServiceName = Program.ServiceName;
        }

        protected override void OnStart(string[] args)
        {
            Program.Start(args);
        }

        protected override void OnStop()
        {
            Program.Stop();
        }
    }
    #endregion

    static void Main(string[] args)
    {
        if (!Environment.UserInteractive)
            // running as service
            using (var service = new Service())
                ServiceBase.Run(service);
        else
        {
            // running as console app
            Start(args);

            Console.WriteLine("Press any key to stop...");
            Console.ReadKey(true);

            Stop();
        }
    }

    private static void Start(string[] args)
    {
        // onstart code here
    }

    private static void Stop()
    {
        // onstop code here
    }
}

Environment.UserInteractive normal app konsolu için gerçek bir hizmet için yanlıştır. Techically, kullanıcı etkileşimli bir servis modunda çalıştırmak mümkündür, komut satırı anahtarı yerine gidip bakabilirsin.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • FD2097

    FD2097

    21 HAZİRAN 2009
  • LardTardProductions's channel

    LardTardProd

    10 NİSAN 2009
  • MarinaHD2001

    MarinaHD2001

    7 ŞUBAT 2009