I am getting below error while running windows service: The "my service name" service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other service or programs"
It is difficult to track down this type of generic errors. You can do the following options to figure out the issue and fix it. Option 1: With Event viewer you can see the errors and on which line number we are getting it. Do the following steps to open the Event Viewer:
1) Type Event Viewer on start and click it.
2) In popup look for “Windows Logs” under “Event Viewer (Local)” on left panel and click it.
3) Now you will get list of windows logs, select “Application” from it.
Here you can search for error, see the screen shot below:
Option 2: You can have try catch blog with logging errors to table.
I have created a small function to log error as below:
public static void getLog(string msg)
string query = "insert into LOGTABLE (DATE,LEVEL,MESSAGE,EXCEPTION) values(current_timestamp,'INFO','"+msg+"','-')";
var DB = dbConnect();
command = new SqlCommand(query, DB);
dataReader = command.ExecuteReader();
Add more try/catch blocks and logging to the app to get a better picture of what’s going on.
Hope it helps someone!!!
In this post, I am sharing how you can install or uninstall windows services in your local windows system [Windows 10]. Assuming you already built one windows services using C# and want to install it using the command prompt. Follow the below steps to install on your local windows 10 environment:
Step 1: Before you install a windows services you need to generate EXE file. And for generating you need to go to the Visual studio and clean and build your application, which will automatically generate exe file in bin folder.
Step 2: Open command prompt “as Administrator”
Step 3: Navigate to folder “C:\Windows\Microsoft.NET\Framework\v4.0.30319>”
Step 4: For installing you need to run below command: installutil.exe "E:\Suresh\Projects\TestWindowsService\TestWindowsService\obj\Debug\TestWindowsService.exe"
Step 5: Open Run window (Window + R), type in “services.msc”, and press enter. A “Services” popup will open. Step 6: In “Services” popup search for your service name and start it by right click on it.
Thats All !!!
Now If you want to uninstall it, type the following command:
installutil.exe -u “E:\Suresh\Projects\TestWindowsService\TestWindowsService\obj\Debug\TestWindowsService.exe”
Sometime you need to start/stop windows services from your C# MVC application. Here i am sharing how you can do that:
1) To access windows service you need to add below namespace in your controller:
If you are getting error as “namespace name ‘ServiceController’ could not be found” while adding above namesapce you need to add using “Add Reference”. For more details see my blog HERE
2) Add below code to your controller:
ServiceController service = new ServiceController("Test Windows Service");
May times you need to add namespaces to your application, here I sharing how you can add “System.ServiceProcess” required to incorporate windows services to your application.
Follow the below steps add references:
1) Right click on solution explore and click on add reff
2( go to Assembly => Framework and look for “System.ServiceProcess” and enable it.
Click OK to enable it.
3) Now add belowline at the top:
4) Add the code below:
ServiceController svcController = new ServiceController("Test Windows Service");
if (svcController != null)