Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Object reference Lost in OnStop of Service
Message
De
28/01/2016 21:13:45
 
 
À
28/01/2016 16:54:40
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01630371
Message ID:
01630398
Vues:
94
>>Hi,
>>
>>I'm writing my first ever Windows service in C# and am getting this error when I stop the service (using the services applet).
>>
>>Failed to stop service. System.NullReferenceException: Object reference not set to an instance of an object.
>> at ResRepricerImportFolderMonitor.ResRepricerImportFolderMonitor.OnStop() in D:\Development\VS2010\ResPricer\ResRepricerImportFolderMonitor\ResRepricerImportFolderMonitor.cs:line 106
>> at System.ServiceProcess.ServiceBase.DeferredStop()
>>
>>The error occurs on this line:
>>
>>watcher.EnableRaisingEvents = false;
>>
>>in the OnStop method.
>>
>>Is my watcher being automatically shutdown or do I have it in the wrong scope?
>>
>>
namespace ResRepricerImportFolderMonitor
>>{
>>    public partial class ResRepricerImportFolderMonitor : ServiceBase
>>    {
>>        public enum ServiceState
>>        {
>>            SERVICE_STOPPED = 0x00000001,
>>            SERVICE_START_PENDING = 0x00000002,
>>            SERVICE_STOP_PENDING = 0x00000003,
>>            SERVICE_RUNNING = 0x00000004,
>>            SERVICE_CONTINUE_PENDING = 0x00000005,
>>            SERVICE_PAUSE_PENDING = 0x00000006,
>>            SERVICE_PAUSED = 0x00000007,
>>        }
>>
>>        [StructLayout(LayoutKind.Sequential)]
>>        public struct ServiceStatus
>>        {
>>            public long dwServiceType;
>>            public ServiceState dwCurrentState;
>>            public long dwControlsAccepted;
>>            public long dwWin32ExitCode;
>>            public long dwServiceSpecificExitCode;
>>            public long dwCheckPoint;
>>            public long dwWaitHint;
>>        };
>>
>>        [DllImport("advapi32.dll", SetLastError = true)]
>>        private static extern bool SetServiceStatus(IntPtr handle, ref ServiceStatus serviceStatus);
>>
>>        private FileSystemWatcher watcher = null;
>>
>>        public ResRepricerImportFolderMonitor()
>>        {
>>            InitializeComponent();
>>        }
>>
>>        protected override void OnStart(string[] args)
>>        {
>>            // Update the service state to Start Pending.
>>            ServiceStatus serviceStatus = new ServiceStatus();
>>            serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
>>            serviceStatus.dwWaitHint = 100000;
>>            SetServiceStatus(this.ServiceHandle, ref serviceStatus);
>>
>>            // Create a new FileSystemWatcher with the path
>>            //and text file filter
>>
>>            string CSVFolder = ConfigurationManager.AppSettings["CSVFolder"];
>>            string CSVFolderProcessed = ConfigurationManager.AppSettings["CSVFolderProcessed"];
>>
>>            FileSystemWatcher watcher = new FileSystemWatcher(CSVFolder, "*.csv");
>>
>>            //Watch for changes in LastAccess and LastWrite times, and
>>            //the renaming of files or directories.
>>            watcher.NotifyFilter = NotifyFilters.LastAccess
>>                                 | NotifyFilters.LastWrite
>>                                 | NotifyFilters.FileName
>>                                 | NotifyFilters.DirectoryName;
>>
>>            // Add event handlers.
>>            //watcher.Changed += new FileSystemEventHandler(OnChanged);
>>            watcher.Created += new FileSystemEventHandler(OnChanged);
>>            //watcher.Deleted += new FileSystemEventHandler(OnChanged);
>>            //watcher.Renamed += new RenamedEventHandler(OnRenamed);
>>
>>            // Begin watching.
>>            watcher.EnableRaisingEvents = true;
>>
>>            // Update the service state to Running.
>>            serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
>>            SetServiceStatus(this.ServiceHandle, ref serviceStatus);
>>
>>            LogEvent("started");
>>       }
>>
>>        protected override void OnStop()
>>        {
>>            watcher.EnableRaisingEvents = false; // error occurs here
>>            watcher.Dispose();
>>
>>            LogEvent("stopped");
>>        }
>
>
>Wrong scope. You have a variable type in front of the variable when you instantiate it. This causes it to create a local variable in the method instead of using the field in your class.

Thanks, how should I declare it?

< later >

I realised what you meant when I woke up a little fresher this morning, thanks.

I needed to change this line:

FileSystemWatcher watcher = new FileSystemWatcher(CSVFolder, "*.csv");

to this:

watcher = new FileSystemWatcher(CSVFolder, "*.csv");
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform