স্টেপ ১ −
একটি নতুন উইন্ডোজ সার্ভিস অ্যাপ্লিকেশন তৈরি করুন।
স্টেপ ২ −
একটি উইন্ডোজ সার্ভিস চালানোর জন্য, আপনাকে ইনস্টলার ইনস্টল করতে হবে, যা সার্ভিস কন্ট্রোল ম্যানেজারের সাথে এটি রেজিস্টার করে। Service1.cs[Design]-এ রাইট-ক্লিক করুন এবং Add Installer সিলেক্ট করুন।
স্টেপ ৩ −
ProjectInstaller.cs [Design]-এ রাইট-ক্লিক করুন এবং View Code সিলেক্ট করুন।
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 চাপুন এবং InitializeComponent ক্লাসের ইমপ্লিমেন্টেশনে যান। সার্ভিসের নাম এবং বর্ণনা যোগ করুন যা ইনস্টলেশন সময় উইন্ডোজ সার্ভিসের নাম হবে।
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
this.serviceInstaller1.Description = "My Demo Service";
this.serviceInstaller1.ServiceName = "DemoService";
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1
});
}
স্টেপ ৪ −
এখন 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);
}
}
}
}
}
স্টেপ ৫ (ইনস্টলেশন) −
এখন আমরা কমান্ড প্রম্পট ব্যবহার করে আমাদের উইন্ডোজ সার্ভিস ইনস্টল করব। কমান্ড প্রম্পট অ্যাডমিনিস্ট্রেটর হিসেবে খুলুন এবং নিচের কমান্ড দিন।
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
আমাদের উইন্ডোজ সার্ভিসের exe ফাইল যেখানে আছে সেই ফোল্ডার খুলুন এবং নিচের কমান্ড চালান।
InstallUtil.exe C:\Users\[UserName]\source\repos\DemoWindowsService\DemoWindowsService\bin\Debug\DemoWindowsService.exe
এখন উইন্ডোজ অ্যাপ্লিকেশন মেনু থেকে Services খুলুন। আমরা দেখতে পাচ্ছি যে আমাদের উইন্ডোজ সার্ভিস ইনস্টল হয়ে প্রত্যাশা অনুযায়ী চলছে। নিচের আউটপুটে দেখানো হয়েছে যে সার্ভিস চলছে এবং প্রত্যাশা অনুযায়ী টেক্সট ফাইলে লগ লিখছে।