ধাপ 1 -
একটি নতুন উইন্ডোজ পরিষেবা অ্যাপ্লিকেশন তৈরি করুন৷
৷

ধাপ 2 -
একটি Windows পরিষেবা চালানোর জন্য, আপনাকে ইনস্টলারটি ইনস্টল করতে হবে, যা পরিষেবা নিয়ন্ত্রণ ব্যবস্থাপকের সাথে এটি নিবন্ধন করে৷ Service1.cs[ডিজাইন] এবং AddInstaller-এ রাইট ক্লিক করুন।


ধাপ 3 -
ProjectInstaller.cs [ডিজাইন]-এ রাইট ক্লিক করুন এবং ভিউ কোড নির্বাচন করুন।

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;
namespace DemoWindowsService{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer{
public ProjectInstaller(){
InitializeComponent();
}
}
} F12 টিপুন এবং Initialize Component ক্লাস বাস্তবায়নে যান। পরিষেবার নাম এবং বিবরণ যোগ করুন যা ইনস্টলেশনের সময় উইন্ডোজ পরিষেবার নাম হবে৷
৷private void InitializeComponent(){
this.serviceProcessInstaller1 = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalService;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.Description = "My Demo Service";
this.serviceInstaller1.ServiceName = "DemoService";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
} ধাপ 4 -
এখন Service1.cs ক্লাসের টেক্সট ফাইলে লগ ডেটা লিখতে নিচের যুক্তি যোগ করি।
using System;
using System.IO;
using System.ServiceProcess;
using System.Timers;
namespace DemoWindowsService{
public partial class Service1 : ServiceBase{
Timer timer = new Timer();
public Service1(){
InitializeComponent();
}
protected override void OnStart(string[] args){
WriteToFile("Service started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 5000;
timer.Enabled = true;
}
protected override void OnStop(){
WriteToFile("Service stopped at " + DateTime.Now);
}
private void OnElapsedTime(object source, ElapsedEventArgs e){
WriteToFile("Service recall at " + DateTime.Now);
}
public void WriteToFile(string Message){
string path = @"D:\Demo";
if (!Directory.Exists(path)){
Directory.CreateDirectory(path);
}
string filepath = @"D:\Demo\Log.txt";
if (!File.Exists(filepath)){
using (StreamWriter sw = File.CreateText(filepath)){
sw.WriteLine(Message);
}
} else {
using (StreamWriter sw = File.AppendText(filepath)){
sw.WriteLine(Message);
}
}
}
}
} ধাপ 5 (ইনস্টলেশন) -
এখন আমরা কমান্ড প্রম্পট ব্যবহার করে আমাদের উইন্ডোজ সার্ভিস ইন্সটল করব। প্রশাসক হিসাবে কমান্ডপ্রম্পট খুলুন এবং নীচের কমান্ড প্রদান করুন।
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

ফোল্ডারটি খুলুন যেখানে আমাদের উইন্ডোজ সার্ভিস exe ফাইলটি উপস্থিত রয়েছে এবং নীচের কমান্ডটি চালান৷
৷InstallUtil.exe C:\Users\[UserName] source\repos\DemoWindowsService\DemoWindowsService\bin\Debug\ DemoWindowsService.exe


এখন উইন্ডোজ অ্যাপ্লিকেশন মেনু থেকে পরিষেবা খুলুন৷
৷


আমরা দেখতে পাচ্ছি যে আমাদের উইন্ডোজ পরিষেবা ইনস্টল করা হয়েছে এবং প্রত্যাশিতভাবে চলতে শুরু করেছে৷
নীচের আউটপুটটি দেখায় যে পরিষেবাটি চলছে এবং প্রত্যাশিতভাবে টেক্সট ফাইলে লগটি ওয়্যারিং করছে৷
