Comment écrire c# service que je peux aussi fonctionner comme un winforms programme?

J'ai un service windows écrit en C# qui agit comme un proxy pour un tas de périphériques réseau à l'arrière de la base de données. Pour les tests, et aussi d'ajouter une simulation de la couche de tester le dos fin, j'aimerais avoir une interface graphique pour l'opérateur de test pour pouvoir exécuter la simulation. Aussi pour un rayé version d'envoyer une démo. Le GUI et le service n'ont pas à s'exécuter en même temps. Quelle est la meilleure façon d'atteindre ce duel opération?

Edit:
Voici ma solution peignage des trucs de cette question , Suis-je l'Exécute en tant que Service et Installer un .NET service windows sans InstallUtil.exe à l'aide de cet excellent code par Marc Gravel

Il utilise la ligne suivante pour tester si pour exécuter l'interface graphique ou d'exécuter en tant que service.

 if (arg_gui || Environment.UserInteractive || Debugger.IsAttached)

Voici le code.


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Diagnostics;
namespace Form_Service
{
static class Program
{
///
///The main entry point for the application.
///
[STAThread]
static int Main(string[] args)
{
bool arg_install =  false;
bool arg_uninstall = false;
bool arg_gui = false;
bool rethrow = false;
try
{
foreach (string arg in args)
{
switch (arg)
{
case "-i":
case "-install":
arg_install = true; break;
case "-u":
case "-uninstall":
arg_uninstall = true; break;
case "-g":
case "-gui":
arg_gui = true; break;
default:
Console.Error.WriteLine("Argument not expected: " + arg);
break;
}
}
if (arg_uninstall)
{
Install(true, args);
}
if (arg_install)
{
Install(false, args);
}
if (!(arg_install || arg_uninstall))
{
if (arg_gui || Environment.UserInteractive || Debugger.IsAttached)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
rethrow = true; //so that windows sees error... 
ServiceBase[] services = { new Service1() };
ServiceBase.Run(services);
rethrow = false;
}
}
return 0;
}
catch (Exception ex)
{
if (rethrow) throw;
Console.Error.WriteLine(ex.Message);
return -1;
}
}
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);
}
}
}
[RunInstaller(true)]
public sealed class MyServiceInstallerProcess : ServiceProcessInstaller
{
public MyServiceInstallerProcess()
{
this.Account = ServiceAccount.NetworkService;
}
}
[RunInstaller(true)]
public sealed class MyServiceInstaller : ServiceInstaller
{
public MyServiceInstaller()
{
this.Description = "My Service";
this.DisplayName = "My Service";
this.ServiceName = "My Service";
this.StartType = System.ServiceProcess.ServiceStartMode.Manual;
}
}
}
Comment puis-je exécuter en tant que service? J'exécute mon programme à partir de la cmd j'écris c:\myservice.exe -installer, rien ne se passe. Si j'écris seulement c:\myservice.exe ouverture de mon application. Ce que je fais de mal. Ou comment puis-je l'utiliser?
avez-vous jamais donner un sens à la solution ci-dessous? plus loin en créant les classes wrapper ou tel?

OriginalL'auteur Rex Logan | 2009-01-07