Tengo un servicio windows que me genera estos errores ya verifique mis cadenas de conexion y estan correctas no se que pase?
No se ha inicializado la propiedad ConnectionString.
El proceso no puede obtener acceso al archivo 'C:\log_alert.txt' porque está siendo utilizado en otro proceso.
private SqlConnection Conn = new SqlConnection("Persist Security Info=False;User ID= xx;Password=******;Initial Catalog=ReportesDB;Server=xxx");
public string mensaje;
private List<EntityAlerta> objReporte;
private System.Timers.Timer timerprocess = null;
protected override void OnStart(string[] args)
{
Log1("Se inicia servicio " + DateTime.Now.ToString());
timerprocess = new System.Timers.Timer();
timerprocess.Interval = 800;
timerprocess.Elapsed += Timerprocess_Elapsed;
timerprocess.Enabled = true;
timerprocess.Start();
}
protected override void OnStop()
{
timerprocess.Enabled = false;
Log1("servicio detenido");
}
void Timerprocess_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timerprocess.Enabled = false;
try
{
//metodo();
Thread libRep = new Thread(EnviaAlerta);
libRep.Start();
timerprocess.Enabled = true;
}
catch (Exception ex)
{
timerprocess.Enabled = true;
Log1("Entro catch: " + ex.Message);
}
finally
{
timerprocess.Start();
}
}
private void EnviaAlerta()
{
try
{
Conn.Open();
SqlCommand command = new SqlCommand("SELECT Hora FROM Ejecucion WHERE Reporte = 30", Conn);
string str = (string)command.ExecuteScalar();
Conn.Close();
string str2 = DateTime.Today.ToShortDateString();
while (true)
{
try
{
TimeSpan span;
string str3 = str;
this.mensaje = "Enviando Alertas...";
char[] separator = new char[] { ':' };
string[] strArray = str3.Split(separator);
DateTime now = DateTime.Now;
DateTime time3 = new DateTime(now.Year, now.Month, now.Day, int.Parse(strArray[0]), int.Parse(strArray[1]), int.Parse(strArray[2]));
if (time3 > now)
{
span = (TimeSpan)(time3 - now);
}
else
{
time3 = time3.AddDays(1.0);
span = (TimeSpan)(time3 - now);
}
Task.Delay(span).Wait();
Log1("Inicia Env\x00edo de Correos" + DateTime.Now.ToString());
List<string> list = new List<string>();
EnviaCorreo correos = new EnviaCorreo();
DataTable table = new DataTable();
this.objReporte = new List<EntityAlerta>();
table = correos.mailAlertas();
foreach (DataRow row in table.Rows)
{
EntityAlerta item = new EntityAlerta
{
IdReporte = Convert.ToString(row["Id_Reporte"]),
CveReporte = Convert.ToString(row["Cve_Reporte"]),
Asunto = Convert.ToString(row["Asunto"]),
Descripcion = Convert.ToString(row["Descripcion"])
};
this.objReporte.Add(item);
}
foreach (EntityAlerta alerta3 in this.objReporte)
{
using (MailMessage message = new MailMessage())
{
EntityAlerta alerta = new EntityAlerta();
//message.To.Add(new MailAddress("abc@abc.com.mx"));
message.To.Add(new MailAddress("abc@abc.com.mx"));
message.From = new MailAddress("abc@abc.com.mx");
message.Subject = alerta3.Asunto.ToString().Trim();
message.Body = alerta3.Descripcion.ToString().Trim();
message.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 0x24b,
EnableSsl = true,
Credentials = new NetworkCredential("abc@abc.com.mx", "Temporal123")
};
try
{
client.Send(message);
message.Dispose();
Log1("Correos Enviados " + DateTime.Now.ToString());
}
catch (Exception exception)
{
Log1(string.Concat(new object[] { "ERROR ", exception.Message, " ", exception.InnerException, " ", exception.Data }));
}
}
}
}
catch (Exception exception2)
{
Log1(exception2.Message);
}
}
}
catch (Exception e)
{
Log1("Catch finally: " + e.Message);
}
finally
{
Conn.Close();
Conn.Dispose();
}
}
private void Log1(string mensaje)
{
using (StreamWriter writer = System.IO.File.AppendText(@"C:\log_alert.txt"))
{
writer.Write("r\nLog Entry : ");
writer.WriteLine(" :");
writer.WriteLine(" :{0}", mensaje);
writer.WriteLine("----------------------");
}
}
public DataTable mailAlertas()
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBConnStr"].ConnectionString);
conn.Open();
string query = "SELECT Id_Reporte,Cve_Reporte,Asunto,Descripcion FROM Alertas WHERE Activo = 1";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Close();
DataTable t1 = new DataTable();
using (SqlDataAdapter a = new SqlDataAdapter(cmd))
{
a.Fill(t1);
}
return t1;
}