Windows service on local computer started and then stopped

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"

error

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:
event_viewer

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+"','-')";
 SqlCommand command;
 SqlDataReader dataReader;
 var DB = dbConnect();
 command = new SqlCommand(query, DB);
 dataReader = command.ExecuteReader();
 dataReader.Close();
 command.Dispose();
}

Add more try/catch blocks and logging to the app to get a better picture of what’s going on.
Hope it helps someone!!!

Installing Windows Services using command prompt

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.
bin_dubug_exe

Step 2: Open command prompt “as Administrator”
windows_services

Step 3: Navigate to folder “C:\Windows\Microsoft.NET\Framework\v4.0.30319>”
command line

Step 4: For installing you need to run below command:
installutil.exe "E:\Suresh\Projects\TestWindowsService\TestWindowsService\obj\Debug\TestWindowsService.exe"
installing_windows_services

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.
windows_services_in_Services
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”
uninstalling_windows_services

Hope it will help someone!!!

Starting windows services from C# MVC 4

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:

using System.ServiceProcess;

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");
service.Start();

It will start your services.

  Comments or questions are welcome  

The type or namespace name ‘ServiceController’ could not be found

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
Add Reference
2( go to Assembly => Framework and look for “System.ServiceProcess” and enable it.
enable_Service_Process
Click OK to enable it.

3) Now add belowline at the top:

using System.ServiceProcess;

4) Add the code below:

ServiceController svcController = new ServiceController("Test Windows Service");

if (svcController != null)
{
    try
    {
        //svcController.Stop();
        //svcController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
        svcController.Start();
    }
    catch
    {
    }
}
  Comments or questions are welcome  

Recording screen in windows 10

Windows 10 comes with Xbox app installed by default and it is having feature of video recording. It is not meant for that but we can used it somehow as it is free and easy to use.

Follow the below steps to capture the screen:
1) Press key combination “Window key” + “G”, it will open below popup or if it is directly open the recording window then skip to 3 step directly.
Windows record screen

2) Check the check box with “This is a game”.
3) it will open another window as below:
screen recording

4) click on “red” dot icon to start the screen recording.
screen recording

5) Click again red dot icon to stop recording.
6) As soon as you stop it will give system popup like below. Click on it and it plays the video.
screen recording

Hope it will help some!!!!

  Comments or questions are welcome