Hướng dẫn xây dựng windows services (How to create a windows services)
- Tạo dự án chọn loại Windows Services
- Đặt tên cho Windows Service
- Click chuột phải trên MyServiceDemo (Design) > Add Installer, VS sẽ add 2 thành phần vào MyServiceDemo như sau
- Thiết lập tài khoản chạy Service bằng cách click vào serviceProcessInstaller1
- Thiết lập loại tài khoản Account: LocalSystem
- Click chọn ServiceInstaller1 thiết lập kiểu khởi động và tên services
- StartType: Automatic
- Description: My Service Demo
- DisplayName: My Service Demo
- Viết code cho 2 sự kiện của service khi khởi động và tắt dịch vụ
protected override void OnStart(string[] args)
{
File.WriteAllText(“c:\\temp\\log.txt”,”Started time:” + DateTime.Today.ToLongTimeString());
}protected override void OnStop()
{File.WriteAllText(“c:\\temp\\log.txt”,”Started time:” + DateTime.Today.ToLongTimeString());
}
- Biên dịch Project
- Mở Visual Studio Command với quyền là Administrator
- Chuyển đến thư mục chứa file exe của windows services vừa biên dịch
- Gõ lệnh
- Installutil /i tencuaproject.exe
Advertisements