2017-03-31 44 views
0

我正在做一個小型的Windows服務,它從Web服務讀取並將數據存儲在本地數據庫中,但是,它並沒有完成執行while(當它是服務)時,我正在用vs 15調試它,它完美地工作,請按照下面的代碼。WindowsService不執行

PS:我用來調試的代碼在它開始之前評論它,它到達輸入while,但只有第一行,並沒有檢查其餘的。

Program.cs的

using System.ServiceProcess; 

namespace rdi_server.service 
{ 
static class Program 
{ 
    static void Main() 
    { 

     ServiceBase[] ServicesToRun; 
     ServicesToRun = new ServiceBase[] 
     { 
      new Service1() 
     }; 
     ServiceBase.Run(ServicesToRun); 
    } 
} 
} 

Service1.CS

using System; 
using System.Configuration; 
using System.ServiceProcess; 
using System.Threading; 

namespace rdi_server.service 
{ 
    public partial class Service1 : ServiceBase 
{ 

    private Thread _thread; 

    private Updater _updater = new Updater(); 

    private readonly int Interval = Convert.ToInt32(ConfigurationManager.AppSettings["timer"]); 
    private readonly string Connection = ConfigurationManager.AppSettings["connection"]; 

    private readonly Connector _usuarioConnector; 
    private readonly Connector _bandaConnector; 
    private readonly Connector _generoConnector; 
    private readonly Connector _musicaConnector; 

    public Service1() 
    { 
     _usuarioConnector = new Connector("UsuarioBase"); 
     _bandaConnector = new Connector("BandaBase"); 
     _generoConnector = new Connector("GeneroBase"); 
     _musicaConnector = new Connector("MusicaBase"); 

     InitializeComponent(); 
    } 

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

    public void StartDebug() 
    { 
     _thread = new Thread(OnTime); 
     _thread.Name = "My Worker Thread"; 
     _thread.Start(); 
    } 

    protected override void OnStop() 
    { 

    } 

    protected void OnTime() 
    { 
     while (true) 
     { 
      EventLog.WriteEntry("Dentro do WHILE foi executado!"); 
      _updater.Do(Connection, _usuarioConnector, _generoConnector, _bandaConnector, _musicaConnector); 
      EventLog.WriteEntry("Fim do while"); 
      Thread.Sleep(Interval); 
     } 
    } 
} 
} 

Updater.CS

using rdi_musica.core; 
using System.Collections.Generic; 
using System.Data.SqlClient; 

    namespace rdi_server.service 
    { 
    class Updater 
    { 
    public void Do(string Connection, 
         Connector usuario, 
         Connector genero, 
         Connector banda, 
         Connector musica) 
    { 

     var usuarios = Loader.LoadUsuarios(usuario); 
     var generos = Loader.LoadGeneros(genero); 
     var bandas = Loader.LoadBandas(banda); 
     var musicas = Loader.LoadMusicas(musica); 


     using (var connection = new SqlConnection(Connection)) 
     { 
      connection.Open(); 

      foreach (var _usuario in usuarios) 
      { 
       DomainDAO.InsertUsuario(connection, _usuario); 
      } 
      foreach (var _genero in generos) 
      { 
       DomainDAO.InsertGenero(connection, _genero); 
      } 
      foreach (var _banda in bandas) 
      { 
       DomainDAO.InsertBanda(connection, _banda); 
      } 
      foreach (var _musica in musicas) 
      { 
       DomainDAO.InsertMusica(connection, _musica); 
      } 
     } 
    } 
} 
} 
+2

請考慮寫一個[MCVE]包括代碼(在這裏,不是鏈接)所以這個問題可以在未來有相關性,如果鏈接死了。 – gravity

+1

你的代碼是否拋出某種異常? –

+0

回覆:第一個評論,我會更進一步:如果你希望在這裏獲得幫助,你必須提供一個好的[mcve]。這樣做不僅可以防止您的問題變得無法使用,還可以確保您提供了所需的信息來回答您的問題,不會更少。另請參閱[問],尤其是該頁面底部鏈接的文章。 –

回答

0

什麼是可能發生的事情是你有一個未處理的異常發生在某處,這是你的while循環。

您可以使用Windows事件日誌與try-catch代碼組合登錄你的錯誤,而不會破壞你的無限循環:

protected void OnTime() 
{ 
    while (true) 
    { 
     try 
     { 
      EventLog.WriteEntry("Dentro do WHILE foi executado!"); 
      _updater.Do(Connection, _usuarioConnector, _generoConnector, _bandaConnector, _musicaConnector); 
      EventLog.WriteEntry("Fim do while"); 
     } 
     catch (Exception ex) 
     { 
      this.EventLog.WriteEntry("ERROR: " + ex.GetType().ToString() + " : " + ex.Message + " : " + ex.StackTrace, EventLogEntryType.Error); 
     } 

     Thread.Sleep(Interval); 
    } 
} 
+0

不,我做了這件事,但沒有做任何事情,但是我棄權的是,當我調試它時,它完美地工作,連接到Web服務,檢索信息並寫入數據庫 –