none
Dienst mit c# erstellen, Dienst Startet und beendet sich automatisch. RRS feed

  • Frage

  • Guten Morgen Zusammen,

    ich habe einen Dienst mit C# erstellt, der Textdatei automatisch exportiert.

    Der Dienst exportiert schon die Textdatei, aber nach einige secondes beendet sich den Dienst automatisch.

    Ich habe dazu 2 frage

    1- Wie programmiert man in C# ein Dienst?

    2- wie kann ich den Dienst im Debbug mode ausfüren? Tools-->Attach to Process--> Ist der Proccess(Dienst) da, aber inaktiv(Grau). Kann man nicht verbinden. (Siehe Bild)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading;
    
    namespace nccg.Trubiquity.Service
    {
        
        public partial class nccgtrubiquityservice : ServiceBase
        {
    
            private System.Timers.Timer _timer;
    
            public nccgtrubiquityservice(string[] args)
            {
               
                try
                {
    
                   this.ServiceName = "nccgtrubiquityservice";
    
                    string eventSourceName = "NCC-Trubiquity-Service";
                    string logName = "NCC-Trubiquity-Service";
    
                    if (args.Count() > 0)
                    {
                        eventSourceName = args[0];
                    }
                    if (args.Count() > 1)
                    {
                        logName = args[1];
                    }
    
                    if (!System.Diagnostics.EventLog.SourceExists(eventSourceName))
                    {
                        System.Diagnostics.EventLog.CreateEventSource(eventSourceName, logName);
                        EventLog.WriteEntry("NCC-Trubiquity-Service", "Event source created", EventLogEntryType.Information);
    
                    }
    
                    EventLog.Source = eventSourceName;
                    EventLog.Log = logName;
    
                    //_timer = new System.Timers.Timer();
                    //_timer.Interval = 300000;        // 60000 = 1min
                    //_timer.Enabled = true;
                    //_timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
    
                    //var start = new TrubiquityService();
                    //start.StartExportImportService();
    
                }
                catch (Exception ex)
                {
    
                    EventLog.WriteEntry("NCC-Trubiquity-Service", "Fehler beim InitializeComponent: " + ex.ToString(), EventLogEntryType.Error);
                }
               
            }
    
    
    
    
            protected override void OnStart(string[] args)
            {
                try
                {
    
                    this._timer = new System.Timers.Timer(30000);
                    this._timer.AutoReset = true;
                    this._timer.Elapsed += new System.Timers.ElapsedEventHandler(this._timer_Elapsed);
                    this._timer.Start();
                   
    base.OnStart(args);
                EventLog.WriteEntry("NCC-Trubiquity-Service", "Der NCC-Trubiquity Service wurde gestartet.", EventLogEntryType.Information);
    
                }
                catch (Exception ex)
                {
                    EventLog.WriteEntry("NCC-Trubiquity-Service", "Fehler beim Starten des NCC-Trubiquity Services: " + ex.ToString(), EventLogEntryType.Information);
                }
              
            }
    
            protected override void OnStop()
            {
                try
                {
                    this._timer.Stop();
                    this._timer = null;
                    EventLog.WriteEntry("NCC-Trubiquity-Service", " Der NCC-Trubiquity Service wurde gestoppt.", EventLogEntryType.Information);
                }
                catch (Exception ex)
                {
                    EventLog.WriteEntry("NCC-Trubiquity-Service", "Fehler beim Stop des NCC-Trubiquity Services: " + ex.ToString(), EventLogEntryType.Information);        
                }
              
            }
        
    
            private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                var start = new TrubiquityService();
                if (start.StartExportImportService())
                {
                    EventLog.WriteEntry("NCC-Trubiquity-Service", "Neue Erstellung von TextDatei war erfolgreich.", EventLogEntryType.Information);
                }
            }
        }
    }
    

    Danke Im Voraus für die Vorschläge

    Mittwoch, 28. September 2016 10:06

Antworten

Alle Antworten