Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/09/2011, 13:06
DKain
 
Fecha de Ingreso: junio-2008
Mensajes: 61
Antigüedad: 16 años, 10 meses
Puntos: 0
Problema viejo pero en VS 2010 'No se puede iniciar un servicio desde...'

Por cuestiones del destino me han pedido instalar VS 2010 a mi maquina eliminando la instancia de VS 2008 pero ahora al migrar un Windows Service no puedo levantarlo ya que me sale la vieja Excepcion 'No se puede iniciar un servicio desde la linea de comandos o un depurador' al hacer Debug en el 'ServiceBase.Run(new ServicioMonitor());'

Asi que opte por realizar un pequeño servicio desde el inicio para ver que pasa y se replica lo mismo, este solo debe de mandar datos y es el que pongo mas abajo.

Les paso el codigo el cual practicamente no ha cambiado, eso si, esta compilado en el Framework 2.0 pero eso no debe de influir ya que antes lo hacia en otras versiones de VS y solo tiene algunas variantes, que creen que pueda ser?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Threading;
using System.Data.SqlClient;

namespace Cliente.WinServ
{
public partial class ServicioMonitor : ServiceBase
{
private System.Timers.Timer monitor;
private EventLog logMonitor;

static void Main(string[] args)
{
ServiceBase.Run(new ServicioMonitor());
}

public ServicioMonitor()
{
this.ServiceName = "Servicio CL";
this.CanStop = true;
this.CanShutdown = true;
this.CanPauseAndContinue = true;
}

protected override void OnStart(string[] args)
{
InicializarLog();
InicializarMonitor();
}

protected override void OnStop()
{
logMonitor.WriteEntry("Monitor Detenido");
monitor.Stop();
monitor.Dispose();
}

protected override void OnContinue()
{
if (monitor.Enabled == true)
{
logMonitor.WriteEntry("Monitor Pausa");
monitor.Enabled = false;
}
else if (monitor.Enabled == false)
{
logMonitor.WriteEntry("Monitor Continua");
monitor.Enabled = true;
}
}

protected override void OnShutdown()
{
logMonitor.WriteEntry("Monitor Detenido");
logMonitor.Dispose();
monitor.Dispose();
this.Dispose();
}

private void monitor_Elapsed(object sender, EventArgs e)
{
monitor.Enabled = false;
EjecutarQuery();
logMonitor.WriteEntry("Monitor Continua: " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
monitor.Enabled = true;
}

public void InicializarLog()
{
this.AutoLog = true;
logMonitor = new EventLog();
if (!EventLog.SourceExists("LogClienteService"))
{ EventLog.CreateEventSource("LogClienteService", "LogService"); }
logMonitor.Source = "LogClienteService";
logMonitor.Log = "LogService";
}
private void InicializarMonitor()
{
monitor = new System.Timers.Timer();
monitor.Interval = 5000;
monitor.Elapsed += new ElapsedEventHandler(monitor_Elapsed);
monitor.Enabled = true;
monitor.Start();
logMonitor.WriteEntry("Inicializa Monitor");
}

private void EjecutarQuery()
{
SqlConnection conn = new SqlConnection("Data Source=BaseSQLS Catalog=Testing;Integrated Security=True");
SqlCommand command = new SqlCommand("INSERT INTO TablaFecha(fecha)VALUES('" + DateTime.Now.ToString() + "')");
command.CommandType = CommandType.Text;

conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}
}