Publishing a service

K

K Viltersten

I published the software i've designed but
it seems that since it's designed as a
service, i'm not able to use the usual
publishing tools in VS05.

When i try to install the software, it
says i need to run installutil.exe by hand
and i'd like the installer to do that for
me.

If that's not possible (for some strange
reason) - i'd like to get suggestions on
how to deploy a service well, i.e. how to
guarantee that all the needed files are
there. The method i use now is trial and
error, mostly error, which is painfully
time consuming.
 
M

Marc Gravell

You can deploy a service with an msi if you want. Personally, I find
the simplest mechanism (for my needs) is to make the service self-
installing through command-line switches (i.e. "-i" to install, "-u"
to uninstall). I also tend to add a "-c" console mode (which runs the
main code without bothering with the service layer) - this makes it
easy to debug etc; or with no switches present it just runs the
service like normal.

You can use the regular installer code in your main code (as long as
you run it as an admin), by adding code like below.

Marc

[RunInstaller(true)]
public sealed class MyServiceInstallerProcess :
ServiceProcessInstaller
{
public MyServiceInstallerProcess()
{
this.Account = ServiceAccount.NetworkService;
}
}
[RunInstaller(true)]
public sealed class MyServiceInstaller : ServiceInstaller
{
public MyServiceInstaller()
{
this.Description = "Service Description";
this.DisplayName = "Service Name";
this.ServiceName = "ServiceName";
this.StartType =
System.ServiceProcess.ServiceStartMode.Automatic;
}
}

static void Install(bool undo, string[] args)
{
try
{
Console.WriteLine(undo ? "uninstalling" :
"installing");
using (AssemblyInstaller inst = new
AssemblyInstaller(typeof(Program).Assembly, args))
{
IDictionary state = new Hashtable();
inst.UseNewContext = true;
try
{
if (undo)
{
inst.Uninstall(state);
}
else
{
inst.Install(state);
inst.Commit(state);
}
}
catch
{
try
{
inst.Rollback(state);
}
catch { }
throw;
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}
}
 
K

K Viltersten

You can deploy a service with an msi if you want. Personally, I find
the simplest mechanism (for my needs) is to make the service self-
installing through command-line switches (i.e. "-i" to install, "-u"
to uninstall). I also tend to add a "-c" console mode (which runs the
main code without bothering with the service layer) - this makes it
easy to debug etc; or with no switches present it just runs the
service like normal.

Great! I'll try that out right away.

Just one more question. When you say
you run the service like normal - do
you mean you run it as an application
or do you start it somehow?

When i went for installutil.exe
without adding inheritance of
Installer class, i got an angry error
message right up my screen.
 
M

Marc Gravell

Re installutil - I don't use it ;-p The service literally installs
itself as though it were an msi.
Just one more question. When you say
you run the service like normal - do
you mean you run it as an application
or do you start it somehow?

Maybe this'll make it clear... note StartUp and ShutDown are my start/
stop code - i.e. all my service does in OnStart is call StartUp,
etc...

Marc

static int Main(string[] args)
{
bool install = false, uninstall = false, console = false,
rethrow = false;
try
{
foreach (string arg in args)
{
switch (arg)
{
case "-i":
case "-install":
install = true; break;
case "-u":
case "-uninstall":
uninstall = true; break;
case "-c":
case "-console":
console = true; break;
default:
Console.Error.WriteLine("Argument not
expected: " + arg);
break;
}
}

if (uninstall)
{
Install(true, args);
}
if (install)
{
Install(false, args);
}
if (console)
{
Console.WriteLine("Starting...");
StartUp();
Console.WriteLine("System running; press any key
to stop");
Console.ReadKey(true);
ShutDown();
Console.WriteLine("System stopped");
}
else if (!(install || uninstall))
{
rethrow = true; // so that windows sees error...
ServiceBase[] services = { new WinService() };
ServiceBase.Run(services);
rethrow = false;
}
return 0;
}
catch (Exception ex)
{
if (rethrow) throw;
Console.Error.WriteLine(ex.Message);
return -1;
}
}
 
L

loki.lokesh3

Re installutil - I don't use it ;-p The service literally installs
itself as though it were an msi.
Just one more question. When you say
you run the service like normal - do
you mean you run it as an application
or do you start it somehow?

Maybe this'll make it clear... note StartUp and ShutDown are my start/
stop code - i.e. all my service does in OnStart is call StartUp,
etc...

Marc

static int Main(string[] args)
{
bool install = false, uninstall = false, console = false,
rethrow = false;
try
{
foreach (string arg in args)
{
switch (arg)
{
case "-i":
case "-install":
install = true; break;
case "-u":
case "-uninstall":
uninstall = true; break;
case "-c":
case "-console":
console = true; break;
default:
Console.Error.WriteLine("Argument not
expected: " + arg);
break;
}
}

if (uninstall)
{
Install(true, args);
}
if (install)
{
Install(false, args);
}
if (console)
{
Console.WriteLine("Starting...");
StartUp();
Console.WriteLine("System running; press any key
to stop");
Console.ReadKey(true);
ShutDown();
Console.WriteLine("System stopped");
}
else if (!(install || uninstall))
{
rethrow = true; // so that windows sees error...
ServiceBase[] services = { new WinService() };
ServiceBase.Run(services);
rethrow = false;
}
return 0;
}
catch (Exception ex)
{
if (rethrow) throw;
Console.Error.WriteLine(ex.Message);
return -1;
}
}

Hi Marc,
I followed your steps, but i don't know from where you are calling Install function, its declared like bool type,If you have Install function, what itcontains? can i get that code? and also where i need to pass args values.I want to achieve this task using innosetup? how to achieve this task? cani get any sample??
"Using INNO setup i want to install WCF service into window service and it should start manually, can i get any sample for inno setup and steps for achieving it??

Regards,
Lokesh.J
 
R

ryan.riley

I just found this through one of your SO answers. However, I'm missing something. Has this changed as of Windows Server 2008 R2? When I try to apply what you suggest, I get the "Windows Service Start Failure" that claims, "Cannot start service from the command line or debugger. ..."

Regards,
Ryan Riley

Re installutil - I don't use it ;-p The service literally installs
itself as though it were an msi.
Just one more question. When you say
you run the service like normal - do
you mean you run it as an application
or do you start it somehow?

Maybe this'll make it clear... note StartUp and ShutDown are my start/
stop code - i.e. all my service does in OnStart is call StartUp,
etc...

Marc

static int Main(string[] args)
{
bool install = false, uninstall = false, console = false,
rethrow = false;
try
{
foreach (string arg in args)
{
switch (arg)
{
case "-i":
case "-install":
install = true; break;
case "-u":
case "-uninstall":
uninstall = true; break;
case "-c":
case "-console":
console = true; break;
default:
Console.Error.WriteLine("Argument not
expected: " + arg);
break;
}
}

if (uninstall)
{
Install(true, args);
}
if (install)
{
Install(false, args);
}
if (console)
{
Console.WriteLine("Starting...");
StartUp();
Console.WriteLine("System running; press any key
to stop");
Console.ReadKey(true);
ShutDown();
Console.WriteLine("System stopped");
}
else if (!(install || uninstall))
{
rethrow = true; // so that windows sees error...
ServiceBase[] services = { new WinService() };
ServiceBase.Run(services);
rethrow = false;
}
return 0;
}
catch (Exception ex)
{
if (rethrow) throw;
Console.Error.WriteLine(ex.Message);
return -1;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top