विंडोज सर्विस एप्लिकेशन बनाने और इसे कमांड प्रॉम्प्ट के माध्यम से इंस्टॉल करने की चरण-दर-चरण प्रक्रिया।
चरण 1: विंडोज सर्विस प्रोजेक्ट बनाएं
- Visual Studio खोलें
- नया प्रोजेक्ट बनाएं → "Windows Service" टेम्पलेट चुनें
- प्रोजेक्ट नाम दें (उदाहरण:
DemoWindowsService)
चरण 2: इंस्टॉलर जोड़ें
- Solution Explorer में
Service1.csपर राइट-क्लिक करें → View Designer - डिजाइनर सतह पर राइट-क्लिक करें → Add Installer
- यह
ProjectInstaller.csफाइल जोड़ता है जिसमेंServiceProcessInstaller1औरServiceInstaller1होते हैं
चरण 3: इंस्टॉलर प्रॉपर्टीज कॉन्फिगर करें
// ProjectInstaller.cs - InitializeComponent में (या डिजाइनर के माध्यम से प्रॉपर्टीज सेट करें)
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
// ServiceProcessInstaller - सर्विस किस अकाउंट के तहत चलती है
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
// अन्य विकल्प: LocalService, NetworkService, User (यूजरनेम/पासवर्ड की आवश्यकता)
// ServiceInstaller - सर्विस मेटाडेटा
this.serviceInstaller1.ServiceName = "DemoService"; // सर्विस नाम (यूनिक)
this.serviceInstaller1.DisplayName = "Demo Windows Service"; // सर्विसेज MMC में दिखने वाला नाम
this.serviceInstaller1.Description = "मेरा डेमो विंडोज सर्विस"; // विवरण
this.serviceInstaller1.StartType = ServiceStartMode.Automatic; // ऑटोमेटिक स्टार्ट
this.Installers.AddRange(new System.Configuration.Install.Installer[]
{
this.serviceProcessInstaller1,
this.serviceInstaller1
});
}
चरण 4: सर्विस लॉजिक इम्प्लीमेंट करें
using System;
using System.IO;
using System.ServiceProcess;
using System.Timers;
namespace DemoWindowsService
{
public partial class Service1 : ServiceBase
{
private Timer _timer;
private string _logPath = @"D:\Demo\Log.txt";
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteToFile("सर्विस शुरू हुई: " + DateTime.Now);
_timer = new Timer(5000); // 5 सेकंड
_timer.Elapsed += OnElapsedTime;
_timer.Enabled = true;
}
protected override void OnStop()
{
WriteToFile("सर्विस रोकी गई: " + DateTime.Now);
_timer?.Dispose();
}
private void OnElapsedTime(object sender, ElapsedEventArgs e)
{
WriteToFile("सर्विस रिकॉल: " + DateTime.Now);
}
private void WriteToFile(string message)
{
try
{
string dir = Path.GetDirectoryName(_logPath);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
File.AppendAllText(_logPath, message + Environment.NewLine);
}
catch (Exception ex)
{
// इवेंट लॉग में लिखें अगर फाइल लिखना विफल हो
EventLog.WriteEntry("DemoService", ex.Message, EventLogEntryType.Error);
}
}
}
}
चरण 5: कमांड प्रॉम्प्ट से इंस्टॉल करें
1. डेवलपर कमांड प्रॉम्प्ट एडमिनिस्ट्रेटर के रूप में खोलें
Windows Key → "Developer Command Prompt" → राइट-क्लिक → "Run as Administrator"
2. .NET Framework इंस्टॉल यूटिलिटी डायरेक्टरी में नेविगेट करें
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
नोट: .NET Core/.NET 5+ के लिए, dotnet tool install --global dotnet-svcutil या सीधे sc.exe का उपयोग करें।
3. InstallUtil का उपयोग करके सर्विस इंस्टॉल करें
InstallUtil.exe "C:\Full\Path\To\DemoWindowsService\bin\Debug\DemoWindowsService.exe"
4. सर्विस अनइंस्टॉल करने के लिए
InstallUtil.exe /u "C:\Full\Path\To\DemoWindowsService\bin\Debug\DemoWindowsService.exe"
आधुनिक विकल्प: .NET Core / .NET 5+ के लिए
sc.exe का उपयोग करके (विंडोज बिल्ट-इन)
REM सर्विस बनाएं
sc.exe create "DemoService" binPath= "C:\Full\Path\DemoWindowsService.exe" start= auto DisplayName= "Demo Windows Service"
REM सर्विस शुरू करें
sc.exe start "DemoService"
REM सर्विस रोकें
sc.exe stop "DemoService"
REM सर्विस हटाएं
sc.exe delete "DemoService"
.NET Core Worker Service के रूप में (अनुशंसित आधुनिक दृष्टिकोण)
// Program.cs (.NET 6+)
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddWindowsService(); // विंडोज सर्विस सपोर्ट
builder.Services.AddHostedService();
var host = builder.Build();
host.Run();
// Worker.cs
public class Worker : BackgroundService
{
private readonly ILogger _logger;
public Worker(ILogger logger) => _logger = logger;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker चल रहा है: {time}", DateTimeOffset.Now);
await Task.Delay(5000, stoppingToken);
}
}
}
सर्विस मैनेजमेंट कमांड्स
| एक्शन | कमांड |
|---|---|
| शुरू करें | |
| रोकें | |
| पॉज करें | |
| जारी रखें | |
| स्टेटस चेक करें | |
| स्टार्टअप टाइप बदलें |
समस्या निवारण
- Access Denied: एडमिनिस्ट्रेटर के रूप में कमांड प्रॉम्प्ट चलाएं
- Service not found: सही पथ और सर्विस नाम जांचें
- Log on failure: ServiceProcessInstaller में अकाउंट जांचें (LocalSystem, NetworkService, या वैध यूजर)
- Dependencies:
ServiceInstaller.ServicesDependedOnका उपयोग करके डिपेंडेंसीज सेट करें
नोट: आधुनिक .NET एप्लिकेशन्स के लिए, लिगेसी Windows Service टेम्पलेट के बजाय Microsoft.Extensions.Hosting.WindowsServices के साथ Worker Service टेम्पलेट का उपयोग करने पर विचार करें।