ตอนที่ 2 : การ Install/Uninstall โปรแกรม Windows Service ที่ได้จาก Visual Studio (VB.Net,C#) |
การ Install/Uninstall โปรแกรม Windows Service ที่ได้จาก Visual Studio (VB.Net,C#) เนื่องจาก Windows Service ไม่สามารถรัน Application ได้จากการ Run ด้วยการคลิกหรือดับเบิ้ลคลิกเหมือนกับโปรแกรมอื่นๆ ที่วไป แต่มันจะทำงานภายใต้ระบบ Session ของ Windows ที่ถูกควบคุมด้วย Services Control Manager ถ้าจะให้ Windows Service นั้นๆ ทำได้อัตโนมัตตหลังจากที่ได้ทำการ Boot เครื่อง Computer หรือจะให้ทำการ Start แบบ Manual ซึงการทำงานทั้งหมดนี้จะถูกควบคุมด้วย Services Control Manager ของ Windows นั้นๆ และจำทำงานแบบ Session หรือ Background Process ที่ไม่แสดงผลออกทางหน้าจอให้ User ทราบ หรือพุดง่ายๆ เป็นโปรแกรมที่ทำงานอยู่เบื้องหลังที่ไม่มีใครรับรู้
และการที่เราจะนำโปรแกรม Windows Service ที่ได้เขียนมานั้นไปรันที่ Service ที่อยู่ภายใต้ Services Control Manager นั้น จะต้องทำการ Install/Uninstall เข้าไปในระบบ ซึ่งจะหลายๆ กับการ Register ตัว Service เขาระบบ Windows โดยรูแบบคำสั่งของการ Install และ Uninstall คือ
Install
InstallUtil.exe MyService.exe
Uninstall
InstallUtil.exe /u MyService.exe
โดยไฟล์ InstallUtil.exe จะอยู่ที่ C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe ทั้งนี้ให้ดูที่ Version ของ .Net Framework ด้วย
มาดูตัวอย่างการทำระบบ Install และ Uninstall
protected override void OnStart(string[] args)
{
string strPath = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
System.IO.File.AppendAllLines(strPath, new[] { "Starting time : " + DateTime.Now.ToString() });
}
protected override void OnStop()
{
string strPath = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
System.IO.File.AppendAllLines(strPath, new[] { "Stop time : " + DateTime.Now.ToString() });
}
Code ใน Service จะมีการสร้าง Log.txt และเขียนข้อความเมื่อมีการ Start และ Stop ตัว Service
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-02.jpg?v=1001)
ในการใช้งานจริงส่วนของ Debug ที่เราได้เพิ่มไว้ก่อนหน้านี้ อาจจะต้องตัดออก ให้เหลือเฉพาะที่ใช้งาน
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-03.jpg?v=1001)
ทดสอบคลิกที่ MyService.exe เพื่อรันโปรแกรม
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-04.jpg?v=1001)
ตามที่ได้แจ้งไว้คือโปรแกรมประเภท Service จะไม่สามารถรันด้วยการคลิกเปิด
![](/images/adv.jpg)
การทำระบบ Install บน Windows Service
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-05.jpg?v=1001)
คลิกที่ Service1.cs และเลือกมุมมองของการ Designer
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-06.jpg?v=1001)
คลิกขวาเลือก Add Installer
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-07.jpg?v=1001)
จะแสดงไฟล์ ProjectInstaller.cs ขึ้นมา และมี Control เกิดขึ้นมา 2 ตัวคือ serviceProcessInstaller กับ serviceInstaller
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-08.jpg?v=1001)
serviceProcessInstaller ให้กำหนด Account เป็น LocalService
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-09.jpg?v=1001)
serviceInstaller ให้กำหนด ServiceName ในที่นี้กำหนดเป็น ThaiCreate.Com Services
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-10.jpg?v=1001)
จากนั้นเลือก Rebuild เพื่อทำการ Build โปรแกรม
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-12.jpg?v=1001)
เปิดไฟล์ Browse ซึ่งโปรแกรมจะถูกจัดเก็บไว้ที่ bin\Debug
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-11.jpg?v=1001)
จะเห็นว่า Services ยังไมไ่ด้ถูกติดตั้ง เราจะต้องทำการรันเพื่อติดตั้งแบบ Manual ซะก่อน
การ Install ตัว Service
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-13.jpg?v=1001)
เปิด Command Prompt ให้ทำการ CD เข้าไปใน Path ที่จัดเก็บโปรแกรมที่ถูก Build ไว้
Command Prompt
CD C:\TestWinApp\MyService\MyService\bin\Debug
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-14.jpg?v=1001)
และดูว่าไฟล์ InstallUtil.exe อยู่ที่ไหนของ .Net Framework ในที่นี้อยู่ที่
Code
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-15.jpg?v=1001)
รันคำสั่ง Install
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe MyService.exe
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-16.jpg?v=1001)
แสดงข้อความเมื่อ Service ทำการติดตั้งเรียบร้อยแล้ว
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-17.jpg?v=1001)
โปรแกรมจะสร้างไฟล์ Install Log เก็บไว้
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-18.jpg?v=1001)
เมื่อมาดูที่ Services Control Manager จะเห็นรายการ Services ทีไ่ด้ถูกติดตั้ง ซึ่งสถานะปัจจุบัน ยังเป็น Stop และ Manual แบบ Start
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-19.jpg?v=1001)
ให้คลิกทำการ Run ตัว Services
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-20.jpg?v=1001)
เมื่อกลับมาดูที่ Path ของโปรแกรม จะเห็นว่า Log.txt ถูกสร้าง
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-21.jpg?v=1001)
พร้อมกับมีการเขียน Text เป็นเวลาที่ถูก Start
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-22.jpg?v=1001)
ทดสอบ Stop ตัว Services
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-23.jpg?v=1001)
ก็จะเห็นว่ามีการเขียน Text เป็นเวลาที่ถูก Stop
การ Uninstall ตัว Service
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-24.jpg?v=1001)
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /u MyService.exe
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-25.jpg?v=1001)
แสดง Service ถูก Uninstall เรียบร้อย
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-26.jpg?v=1001)
เมื่อกลับไปดูที่ Services Control Manager ก็จะเห็นว่ารายการ Services ถุกลบออกไปแล้ว
Note! ในการใช้งานจริงควรจะกำหนดสถานะของการ Build เป็นแบบ Release
![Install Uninstall Install Uninstall](https://www.thaicreate.com/upload/tutorial/windows-service-install-uninstall-27.jpg?v=1001)
โดยเวลาเรียกก็ให้เรียกจาก Release แทน
C:\TestWinApp\MyService\MyService\bin\Release
Note!! ในกรณีที่ Service ไม่ Start เราสามารถดู Log ได้ที่ Event Viewer
![Event Viewer Error Log Event Viewer Error Log](https://www.thaicreate.com/upload/stock/20170420172335.jpg?v=1001)
กรณีทีมี Error จะแสดง Log ที่ Event Viewer
|