Nasıl yapmak .NET Windows Hizmeti doğru kurulumdan sonra başlar?
Servis dışında.Olarak = ServiceStartMode.Benim hizmet Otomatik kurulumdan sonra başlamıyor
Çözüm
Benim Projectİnstaller bu kod eklenmiş
protected override void OnAfterInstall(System.Collections.IDictionary savedState)
{
base.OnAfterInstall(savedState);
using (var serviceController = new ServiceController(this.serviceInstaller1.ServiceName, Environment.MachineName))
serviceController.Start();
}
ScottTx için teşekkürler ve Francis B.
CEVAP
C# here Windows hizmeti oluşturmak için adım adım bir yordam gönderdiniz. En azından bu noktaya gibisin, ve şimdi yüklendikten sonra hizmeti başlatmak için nasıl merak ediyorsanız. Otomatik Olarak ayarlama özelliği hizmeti otomatik olarak sistem yeniden başlatma neden olur, ama otomatik olarak kurulumdan sonra hizmeti başlatmak keşfettim gibi).
Aslında (belki de Marc Gravell?) nereden bulduğumu hatırlamıyorum ama yüklemek için izin veren bir online çözüm buldum ve gerçekten hizmetiniz kendisi çalıştırarak hizmeti başlatın. İşte adım adım:
Hizmet
Main()
işlevi yapı: bunun gibistatic void Main(string[] args) { if (args.Length == 0) { // Run your service normally. ServiceBase[] ServicesToRun = new ServiceBase[] {new YourService()}; ServiceBase.Run(ServicesToRun); } else if (args.Length == 1) { switch (args[0]) { case "-install": InstallService(); StartService(); break; case "-uninstall": StopService(); UninstallService(); break; default: throw new NotImplementedException(); } } }
Burada destekleyici kodu:
using System.Collections; using System.Configuration.Install; using System.ServiceProcess; private static bool IsInstalled() { using (ServiceController controller = new ServiceController("YourServiceName")) { try { ServiceControllerStatus status = controller.Status; } catch { return false; } return true; } } private static bool IsRunning() { using (ServiceController controller = new ServiceController("YourServiceName")) { if (!IsInstalled()) return false; return (controller.Status == ServiceControllerStatus.Running); } } private static AssemblyInstaller GetInstaller() { AssemblyInstaller installer = new AssemblyInstaller( typeof(YourServiceType).Assembly, null); installer.UseNewContext = true; return installer; }
Destek kodu ile devam ediyoruz...
private static void InstallService() { if (IsInstalled()) return; try { using (AssemblyInstaller installer = GetInstaller()) { IDictionary state = new Hashtable(); try { installer.Install(state); installer.Commit(state); } catch { try { installer.Rollback(state); } catch { } throw; } } } catch { throw; } } private static void UninstallService() { if ( !IsInstalled() ) return; try { using ( AssemblyInstaller installer = GetInstaller() ) { IDictionary state = new Hashtable(); try { installer.Uninstall( state ); } catch { throw; } } } catch { throw; } } private static void StartService() { if ( !IsInstalled() ) return; using (ServiceController controller = new ServiceController("YourServiceName")) { try { if ( controller.Status != ServiceControllerStatus.Running ) { controller.Start(); controller.WaitForStatus( ServiceControllerStatus.Running, TimeSpan.FromSeconds( 10 ) ); } } catch { throw; } } } private static void StopService() { if ( !IsInstalled() ) return; using ( ServiceController controller = new ServiceController("YourServiceName")) { try { if ( controller.Status != ServiceControllerStatus.Stopped ) { controller.Stop(); controller.WaitForStatus( ServiceControllerStatus.Stopped, TimeSpan.FromSeconds( 10 ) ); } } catch { throw; } } }
Bu noktada, yükledikten sonra hizmet hedef makine, koş hizmetiniz komut satırından (gibi sıradan bir uygulama) ile
-install
komut satırı bağımsız değişkeni için yükleme ve başlatma hizmeti.
Her şeyi hallettim sanırım, ama eğer bu işe yaramazsa bulursanız, bana cevap güncelleme iletebilirsiniz.
Nasıl eğer dosya artık mevcut değilse,...
Nasıl Javascript tam ekran windows (tü...
Nasıl java uygulaması, bir windows hiz...
Tüm sonra, doğru geri Parçaları örnek ...
Nasıl bir windows hizmeti yüklemek içi...