2012-03-08 101 views
1

感谢阅读我的文章我编程Windows服务,当我尝试启动它,我得到这个错误在事件查看器:Windows服务提供了错误 - System.Security.SecurityException

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() 

下面是一些代码从我的服务应用程序(C#)

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(); 
      } 
     } 
     . 
     . 
     . 
     } 

希望你能帮助我解决这个问题,在此先感谢。

+5

你的异常说'System.Security.SecurityException'不是'System.IO.DirectoryNotFoundException'。尝试以更合适的用户身份运行它(尝试LocalSystem)。 – 2012-03-08 15:07:24

+0

哪行代码会抛出异常? – cadrell0 2012-03-08 15:20:01

+0

错误1053,更改为LocalSystem仍然没有。现在它给了我两个错误:EventType clr20r3,P1 seriviciobiohacademico.exe,P2 1.0.0.0,P3 4f58c7ba,P4系统,P5 4.0.0.0,P6 4ea7ab8b,P7 3bf8,P8 2e1,P9 system.argumentexception,P10 NIL。 And .. – ricardorios 2012-03-08 15:24:38

回答

2

我固定它,它似乎错误是因为我已经从其他服务名称我有同样的日志名,我改变了它,现在它似乎工作(let's说,它开始)

我用这个从另一篇博客解决了错误,并找到什么是错

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.Diagnostics; 

namespace SerivicioBIOHAcademico 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     static void Main() 
     { 
      try 
      { 
       ServiceBase[] ServicesToRun; 
       ServicesToRun = new ServiceBase[] 
       { 
        new BIOHAcad() 
       }; 
       ServiceBase.Run(ServicesToRun); 
      } 
      catch(Exception ex) 
      { 
       string SourceName = "WindowsService.ExceptionLog"; 
       if (!EventLog.SourceExists(SourceName)) 
       { 
        EventLog.CreateEventSource(SourceName, "Application"); 
       } 

       EventLog eventLog = new EventLog(); 
       eventLog.Source = SourceName; 
       string message = string.Format("Exception: {0} \n\nStack: {1}", ex.Message, ex.StackTrace); 
       eventLog.WriteEntry(message, EventLogEntryType.Error); 
      } 
     } 
    } 
} 

它给了我,我需要

1

我遇到了同样的问题,在这里和错误由ricardorios处理提供的错误帮我找出原因我的问题。我想我会做一个帖子来节省其他人的时间。如果您有一个自定义日志名称,前8个字符不是唯一的,则可以遇到此错误。这是我收到的错误:

例外:只有自定义日志名称的前八个字符是重要的,并且系统中已经有另一个日志使用给定名称的前八个字符。

相关问题