Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > ใครพอจะรู้เรื่อง การทำ Run Service บน windows บ้างค่ะ



 

ใครพอจะรู้เรื่อง การทำ Run Service บน windows บ้างค่ะ

 



Topic : 129458



โพสกระทู้ ( 61 )
บทความ ( 0 )



สถานะออฟไลน์




คือ มันขึ้น Error ที่ ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });

ตามรูปเลยค่ะ

ไม่รู้เป็นเพราะอะไร ใครพอจะรู้บ้างค่ะ

2017-11-11_1-27-02

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)







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-11-11 01:40:51 By : Jirata View : 836 Reply : 2
 

 

No. 1



โพสกระทู้ ( 381 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook

ลองรันแล้ว install manual เอาครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-11-11 16:56:21 By : mee079
 


 

No. 2



โพสกระทู้ ( 61 )
บทความ ( 0 )



สถานะออฟไลน์


install manual คือยังไรค่ะ ใช้ cmd รึป่าว หรือว่าใช้ Setup Projects เป็นตัวติดตั้ง แต่ อยากใช้ ติดตั้งโดย install ที่ cmd อ่ะ
เพราะ ต้องการใช้กับ Win XP 32 bit ด้วยค่ะ พอจะมีวิธีแนะนำไหม

2017-11-11_17-19-37
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-11-11 17:43:51 By : Jirata
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ใครพอจะรู้เรื่อง การทำ Run Service บน windows บ้างค่ะ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่