While creating a project using Windows Service VS2013 project template i noticed that after add an EventLog component from the tools box the #region named "Component Designer generated code" is filled with the following code:
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
//
// Service1
//
this.ServiceName = "Service1";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
}
My question is: Should add the Designer generate a line like:
components.Add(this.eventLog1);
after the line
this.eventLog1 = new System.Diagnostics.EventLog();
? The fact that exist a method like:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
and that System.Diagnostics.EventLog implement the IDisposable interface make me think that such line should be included in Designer the generated code. I know that is not a big deal and that just in very rare cases (e.g. you play dirty with ServicesBase derived instances in the Main method) the lack of that line could impact the performance of the app, but still not have sense to me have a Disposal method implementation like that and not add a line like: components.Add(this.eventLog1);. Any thoughts?
This looks like a bug to me. The EventLog class forgets to implement the proper constructor, one that takes the container reference. Components are not required to do so, basic examples are BackgroundWorker and OpenFileDialog, they don't have anything to dispose.
But yes, EventLog does. Not so sure anybody would have noticed this before, event logging is usually done for the life of the program and the EventLog instance is usually stored in a static variable. It is quite easy to fix:
using System;
using System.ComponentModel;
using System.Diagnostics;
public class MyEventLog : EventLog {
public MyEventLog() { }
public MyEventLog(IContainer container) : this() { container.Add(this); }
}