 |
|
 |
When i install my service using the installUtil.exe i dont get any error and service runs successfully. but when i implement the above code in my service project i get following error message.
Error 1053: The service did not respond to the start or control request in a timely fashion.
Please help
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
hi all,
i am reviewing this code for 3 to 4 days continously but didnt get an idea that
1. what do you mean by service executable. Should i put my below code in windows service project
using System.Reflection; using System.Configuration.Install;
namespace gotnet.biz.Utilities.WindowsServices { public static class SelfInstaller { private static readonly string _exePath = Assembly.GetExecutingAssembly().Location; public static bool InstallMe() { try { ManagedInstallerClass.InstallHelper( new string[] { _exePath } ); } catch { return false; } return true; }
public static bool UninstallMe() { try { ManagedInstallerClass.InstallHelper( new string[] { "/u", _exePath } ); } catch { return false; } return true; } } }
2. The main() code is given later in this article, again should i put this in my windows service project if i put that in service project then should i delete the prevoius code
ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WindowsService1.Service1() }; ServiceBase.Run(ServicesToRun);
plz help me i very upset
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
I had my own solution back in 2005 for this: Self installing .NET service using the Win32 API[^]
It uses the native API and the latest versions (available through CodePlex as part of a greater project: http://www.codeplex.com/aspnetSuite[^]) support UAC and 32/64 bit support on Windows, Vista, and beyond. It will auto-detect if it's not installed and attempt to install it. You can also provide command line options to install, uninstall, etc. It also provides install/uninstall/start/stop/etc. utils for any service on the system (each w/ UAC support, if needed) and will automatically degrade to a typical console-based application if running in user interactive mode. It also provides more control than the .net framework-provided one in terms of what messages you can respond to (e.g. system shutdown, power states, etc.), your error handling capabilities, etc.
You can write an entire service w/ all of that functionality w/ ~2 methods.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
After trying your suggestions inside a windows service I'm programming, there is no console output. Surely it has something to do with the type of the project (window service instead of console application), but I don't know what to change in the project properties to get something. Of course, if I start with a console appl I get no service  
pd.- by console output I mean everything that gets out from system.console.writeline(), for example.
Thanks in advance for any suggestion...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I combined the enhancements by Aleksei Nickolayev and Ashley van Gerven, and now have my services accepting command line switches and offering to install or uninstall if launched interactively. This is the best! No more batch files!
Thanks for a great article, and thanks to the other users for the great enhancement ideas!
How could anyone not vote 5? 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I have a problem I can install a Windows service by InstallUtil but I can't do it when I try to do it with ManagerInstallerClass. It's strange, I reflected the Code (is the same that InstallUtil) and it doesn't work. Context data? This is My Code:
static void Main(string[] args) { string path = @"";
MainK(new string[] { path }); }
public static int MainK(string[] args) { Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture.GetConsoleFallbackUICulture(); if (((Console.OutputEncoding.CodePage != 0xfde9) && (Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.OEMCodePage)) && (Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.ANSICodePage)) { Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); } Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true); try { ManagedInstallerClass.InstallHelper(args); } catch (Exception exception) { Console.WriteLine(exception.Message); return -1; } return 0; }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi,
Excellent article! I have found non-command ways to do all things service related, apart from uninstalling...
I have created a helper service (Service1Updater) for my main service (Service1). Service1Updater does two things -Checks a network folder to see if there's any updates for Service1 and installs them -Checks to see if Service1 is running, if not then it tries to start it
The reason I'm using a service to do the updating: -I don't want Service1 to fail when updating, I don't want to manually fix things -I don't want any user interaction during updates -I have a large cluster of computers to roll out the updates, I can't do it manually would take ages -I want Service1 to stay alive if something unforeseen happens, so I use a helper process
The problem: I use Service1Updater to copy new files -> stop Service1 -> uninstall Service1 -> delete old Service1 files -> copy new files -> install Service1 -> set Service1 properties -> start Service1.
Between "uninstall Service1 -> delete old Service1 files" I always get Access denied or UnauthorizedAccessException as the InstallHelper seams to still have a handle open on the old Service1. If I use a Process command then I have no problem deleteing the old files:
Process myProcess = new Process(); string path = @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727"; myProcess.StartInfo.FileName = path + "\\InstallUtil.exe"; myProcess.StartInfo.Arguments = @"/u C:\Service1\Service1.exe"; myProcess.StartInfo.CreateNoWindow = true; myProcess.Start();
myProcess.WaitForExit(60000); if (!myProcess.HasExited) myProcess.Kill(); myProcess.Close();
Although this works it would be nice to have an InstallHelper solution especially as InstallUtil maybe in a different location. Does anyone have any ideas? Would I have to host the InstallHelper in a different thread/process and wait for it to exit?
Cheers
Ross
not suitable for idiots
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
Hi, would you mind providing an example of how you would use the command line switches to set properties of the underlying ServiceProcessInstaller object.
Thanx...
|
| Sign In·View Thread·PermaLink | 1.75/5 (3 votes) |
|
|
|
 |
|
 |
First of all, THANK YOU for your article. It is really useful.
I have one little improvement. In case of your code user must know what switches service executable accepts. I suppose it's a good idea to show all acceptable switches to user when the service was started from console, not from SCM. The method that I use to determine how service was started is quite simple — checking the value of Environment.UserInteractive.
Code:
static void Main(string[] args) { if (Environment.UserInteractive) { Console.Write(Environment.NewLine + System.Reflection.Assembly.GetExecutingAssembly().FullName + ".");
if (args != null && args.Length >= 1) { if (args[0].ToLower() == "/i") { Console.WriteLine(); installMyService(); return; } if (args[0].ToLower() == "/u") { uninstallMyService(); return; } }
Console.WriteLine(Environment.NewLine + Environment.NewLine + "Usage: MyService.exe [/i | /u]" + Environment.NewLine + Environment.NewLine + "Where:"); Console.WriteLine(" /i - install service;"); Console.WriteLine(" /u - uninstall service."); } else { ServiceBase.Run(new MyService()); } }
static void installMyService() { try { ManagedInstallerClass.InstallHelper(new string[] { "/LogFile=", Assembly.GetExecutingAssembly().Location }); } catch { } }
static void uninstallMyService() { try { ManagedInstallerClass.InstallHelper(new string[] { "/u", "/LogFile=", Assembly.GetExecutingAssembly().Location }); } catch { } }
|
| Sign In·View Thread·PermaLink | 4.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Firstly, very nice simple solution - thanks for sharing that tip. I decided to try it out and came up with the idea of installing the service if it's not already installed (i.e. just double click it in Explorer, and confirm whether or not to install it). Worked well for me, using this code:
if (args.Length == 0) { ServiceController sc = new ServiceController("AshFMS2"); try { string s = sc.Status.ToString(); } catch { if (System.Windows.Forms.MessageBox.Show("Install this service? ", "Confirm", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { SelfInstaller(SelfInstallOptions.Install); return; } } } else { switch (args[0]) { case "-i" : case "/i" : SelfInstaller(SelfInstallOptions.Install); return; case "-u" : case "/u" : SelfInstaller(SelfInstallOptions.Uninstall); return; } }
BTW SelfInstaller is my method containing code based on yours.
------------------------------ My Latest CP article: SmartPager - a Flickr-style pager control with go-to-page popup layer.
modified on Saturday, December 22, 2007 12:01:11 AM
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Very good, Ashley. Nice extension of the concept. It would be interesting to make your code detect the version of the installed service and upgrade it on demand.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I think if you stop the service you can replace the .exe no problem.
cheers Ashley
"For fifty bucks I'd put my face in their soup and blow." - George Costanza CP article: SmartPager - a Flickr-style pager control with go-to-page popup layer.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
..When I try to start the command line installed service from the Computer Management tool, I get a Services message box (!), quoting "Could not start the MyCLI_Service on Local Computer. Error 3: The system cannot find the path specified." Huh? ..The CLI Install appears to have gotten the correct path, so what gives? ..The Uninstall appears to work. (I have done it several times. ..The original project was setup as a Windows Service project and the solution included a setup/deployment project which got the service into the Services list of the Computer Management tool. This original version of the service could be started, paused (required a property setting change), and stopped.
Look sharp, be sharp, see sharp!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I suppose I would start by looking around in the registry at HKLM\System\CurrentControlSet\Services to see how your service got registered. Is the ImagePath value OK for your service?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
The ImagePath appears okay. However, I am using a fake drive/path, that is, I have substituted a C: path with J: drive. I will try placing all the files in a real path on the C: drive. Will update results. Thanks for feedback. Ricky
Look sharp, be sharp, see sharp!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
It works!!! No substituted drive pathing allowed. *.msi files have similar problems. Now I can breathe and thank you for a great article. Ricky
Look sharp, be sharp, see sharp!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
The EXE takes command line parameters. If the first parameter is "INSTALL" then it does:
System.Diagnostics.Process.Start ( "INSTALLUTIL" , "\"" + System.Windows.Forms.Application.ExecutablePath + "\"" ) ;
With similar code for UNINSTALL. START and STOP can also be done from the command line.
(I chose this method when I wrote my first Windows service three years ago and haven't revisited the code since, I guess I'll have to now.)
-- modified at 9:33 Thursday 29th November, 2007
SWEET! Thanks!
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |