|
|
|
ใครพอจะรู้เรื่อง การทำ Run Service บน windows บ้างค่ะ |
|
|
|
|
|
|
|
คือ มันขึ้น Error ที่ ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
ตามรูปเลยค่ะ
ไม่รู้เป็นเพราะอะไร ใครพอจะรู้บ้างค่ะ
Program.cs
using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Windows.Forms;
namespace SampleWindowsService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
string strPath = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
if (args != null && args.Length == 1 && args[0].Length > 1 && (args[0][0] == '-' || args[0][0] == '/'))
{
switch (args[0].Substring(1).ToLower())
{
default:
break;
case "install":
case "i":
//try
//{
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
File.AppendAllLines(strPath, new[] { "Cmd Install : " + DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss") });
using (ServiceController Ec = new ServiceController("SWService", Environment.MachineName))
{
if (Ec.Status != ServiceControllerStatus.Running)
Ec.Start();
}
//}
//catch (Exception)
//{
File.AppendAllLines(strPath, new[] { "Cmd Install Error : " + DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss") });
//}
break;
case "uninstall":
case "u":
try
{
using (ServiceController Ec = new ServiceController("SWService", Environment.MachineName))
{
if (Ec.Status == ServiceControllerStatus.StartPending || Ec.Status == ServiceControllerStatus.Running || Ec.Status == ServiceControllerStatus.Paused)
Ec.Stop();
}
ManagedInstallerClass.InstallHelper(new string[] { "-u", Assembly.GetExecutingAssembly().Location });
File.AppendAllLines(strPath, new[] { "Cmd Uninstall : " + DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss") });
foreach (Process process in Process.GetProcessesByName("SWService"))
{
process.Kill();
process.WaitForExit();
}
}
catch (Exception)
{
File.AppendAllLines(strPath, new[] { "Cmd Uninstall Error : " + DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss") });
}
break;
}
}
else if (Environment.UserInteractive)
{
MessageBox.Show("Please install the program with cmd.");
File.AppendAllLines(strPath, new[] { "Incorrect installation : " + DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss") });
}
else
{
ServiceBase.Run(new Service1());
}
}
}
}
ProjectInstaller.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Threading.Tasks;
namespace SampleWindowsService
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
try
{
using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName, Environment.MachineName))
{
if (sc.Status != ServiceControllerStatus.Running)
sc.Start();
}
}
catch (Exception ee)
{
EventLog.WriteEntry("Application", ee.ToString(), EventLogEntryType.Error);
}
}
private void serviceInstaller1_Committed(object sender, InstallEventArgs e)
{
try
{
using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
{
SetRecoveryOptions(sc.ServiceName);
}
}
catch (Exception e1)
{
EventLog.WriteEntry("Application", e1.ToString(), EventLogEntryType.Error);
return;
}
}
static void SetRecoveryOptions(string serviceName)
{
int exitCode;
using (var process = new Process())
{
var startInfo = process.StartInfo;
startInfo.FileName = "sc";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
// tell Windows that the service should restart if it fails
startInfo.Arguments = string.Format("failure \"{0}\" reset= 0 actions= restart/60000", serviceName);
process.Start();
process.WaitForExit();
exitCode = process.ExitCode;
}
if (exitCode != 0)
throw new InvalidOperationException();
}
private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
}
}
}
ขอบคุณค่ะ
Tag : .NET, C#, VS 2013 (.NET 4.x)
|
|
|
|
|
|
Date :
2017-11-11 01:40:51 |
By :
Jirata |
View :
903 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองรันแล้ว install manual เอาครับ
|
|
|
|
|
Date :
2017-11-11 16:56:21 |
By :
mee079 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
install manual คือยังไรค่ะ ใช้ cmd รึป่าว หรือว่าใช้ Setup Projects เป็นตัวติดตั้ง แต่ อยากใช้ ติดตั้งโดย install ที่ cmd อ่ะ
เพราะ ต้องการใช้กับ Win XP 32 bit ด้วยค่ะ พอจะมีวิธีแนะนำไหม
|
|
|
|
|
Date :
2017-11-11 17:43:51 |
By :
Jirata |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|