मेरे पास सी # में लिखा गया एक विंडोज सेवा है जो बैक एंड डेटाबेस में नेटवर्क उपकरणों के समूह के लिए प्रॉक्सी के रूप में कार्य करती है। परीक्षण के लिए और बैक एंड का परीक्षण करने के लिए सिमुलेशन परत जोड़ने के लिए मैं परीक्षण ऑपरेटर के लिए सिमुलेशन चलाने में सक्षम होने के लिए एक जीयूआई रखना चाहता हूं। डेमो के रूप में भेजने के लिए एक धारीदार संस्करण के लिए भी। जीयूआई और सेवा को एक ही समय में चलाने की ज़रूरत नहीं है। इस द्वंद्वयुद्ध ऑपरेशन को हासिल करने का सबसे अच्छा तरीका क्या है?सी # सेवा कैसे लिखें जिसे मैं Winforms प्रोग्राम के रूप में भी चला सकता हूं?
संपादित करें: यहाँ से Marc Gravell
मेरी समाधान this question, Am I Running as a Service और Install a .NET windows service without InstallUtil.exethis excellent code का उपयोग करने से सामान में कंघी है यह अगर जीयूआई चलाने के लिए या सेवा के रूप में चलाने के लिए परीक्षण करने के लिए निम्न पंक्ति का उपयोग करता है।
if (arg_gui || Environment.UserInteractive || Debugger.IsAttached)
यहां कोड है।
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;
}
}
}
मैं इसे सेवा के रूप में कैसे चला सकता हूं? मैं cmd से अपना प्रोग्राम चलाता हूं मैं लिखता हूं c: \ myservice.exe -install कुछ भी नहीं चल रहा है। अगर मैं केवल अपना आवेदन खोलने के लिए c: \ myservice.exe लिखता हूं। मैं क्या गलत कर रहा हूँ। या मैं इसका उपयोग कैसे कर सकता हूं? –
क्या आपने कभी नीचे दिए गए समाधान का एहसास किया है? जहां तक रैपर वर्ग और ऐसे बनाते हैं? –