title: Windows service gives error - System.Security.SecurityException
body: <p>thanks for reading my post i am programming a Windows Service, and when i try to start it i get this error on the EventViewer:</p>
  
  <pre><code>Application: SerivicioBIOHAcademico.exe
  Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.Security.SecurityException
  Stack:
     at System.Diagnostics.EventLog.FindSourceRegistration(System.String, System.String, Boolean, Boolean)
     at System.Diagnostics.EventLog.SourceExists(System.String, System.String, Boolean)
     at System.Diagnostics.EventLog.SourceExists(System.String)
     at SerivicioBIOHAcademico.BIOHAcad..ctor()
     at SerivicioBIOHAcademico.Program.Main()
  </code></pre>
  
  <p>Here is some code from my Service App (C#)</p>
  
  <pre><code>public partial class BIOHAcad : ServiceBase
      {
          Timer timer1 = new Timer();
          private readonly string WDcon = ConfigurationManager.ConnectionStrings["WindowsFormsApplication2.Properties.Settings.DBGriauleConnectionString"].ConnectionString;
          private readonly string UJGH = ConfigurationManager.ConnectionStrings["HorariosConnection"].ConnectionString;
  
          public BIOHAcad()
          {
              InitializeComponent();
              if (!System.Diagnostics.EventLog.SourceExists("Fuentes-BIO-H-Academico"))
              {
                  System.Diagnostics.EventLog.CreateEventSource(
                     "Fuentes-BIO-H-Academico", "Logs-BIO-H-Academico");
              }
              eventLog1.Source = "Fuentes-BIO-H-Academico";
              eventLog1.Log = "Logs-BIO-H-Academico";
          }
  
          protected override void OnStart(string[] args)
          {
              try
              {
                  eventLog1.WriteEntry("Iniciando Servicio BIO-H Academico");
                  timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
                  timer1.Interval = 1000;
                  timer1.Enabled = true;
                  timer1.Start();
              }
              catch (Exception e)
              {
                  eventLog1.WriteEntry(e.ToString());
              } 
          }
  
          private void timer1_Elapsed(object sender, EventArgs e)
          {
              try
              {
                  Buscar_Horarios(DateTime.Now);
              }
              catch (Exception ex)
              {
                  eventLog1.WriteEntry(ex.ToString());
              }
          }
  
          private void Buscar_Horarios(DateTime fecha)
          {
              bool conectarse = Conectarse_BD();
              if (conectarse == true)
              {
                  DateTime corte = fecha.AddMinutes(((-1) * Holgura_Academica()));
                  string dia = Funciones.ObtenerDiaSemana2(fecha);
                  string hora = Funciones.ObtenerHora(fecha);
                  string cortedia = Funciones.ObtenerHora(corte);
                  //Llamo la conexion SQL
                  SqlConnection Wdcon_usuario = new SqlConnection(UJGH);
                  SqlCommand usuario = new SqlCommand();
                  SqlDataReader usuarioDR = null;
  
                  //Instancio la conexion SQL
                  usuario.Connection = Wdcon_usuario;
  
                  //Registro el Query SQL
                  usuario.CommandText = "SELECT * FROM Vista_Horarios_Docentes WHERE (HRAFIN = @horafin) AND (HRADIA = @dia)";
                  usuario.Parameters.AddWithValue("@horafin", hora);
                  usuario.Parameters.AddWithValue("@dia", cortedia);
  
                  //Abro la conexion
                  Wdcon_usuario.Open();
  
                  //Ejecuto la consulta
                  usuarioDR = usuario.ExecuteReader();
  
                  //Empiezo el ciclo
                  while (usuarioDR.Read())
                  {
                      if (usuarioDR["HRARES"].ToString() != "")
                      {
                          if (Validar_Docente(Convert.ToInt64(usuarioDR["HRARES"].ToString())) == true)
                          {
                              DateTime inicio1 = (DateTime)usuarioDR["HRAINI"];
                              DateTime fecha2 = inicio1.AddMinutes((-1) * Holgura_Academica());
                              string inicio = Funciones.ObtenerHora(fecha2);
                              Int64 docente = Convert.ToInt64(usuarioDR["HRARES"].ToString());
                              if (SalioCorrectamente(docente, inicio, cortedia) == true)
                              {
                                  //Calculo las horas que dio clases
                                  CalcularHoras(docente, inicio, cortedia);
                              }
                              else
                              {
                                  //Denegar la persona
                                  Insertar_Denegado(docente, DateTime.Now, Convert.ToDateTime(inicio), Convert.ToDateTime(cortedia));
                              }
                          }
                      }
                  }
                  //Cierro la conexion
  
                  Wdcon_usuario.Close();
              }
          }
          .
          .
          .
        }
  </code></pre>
  
  <p>Hope you can help me to solve this, thanks in advance.</p>