Thursday, September 21, 2017

Create windows service application (Step By Step) using C# .

In this tutorial we’ll create a windows service application. Suppose we’ve service which gives us notification in a text file when it starts and stop. Let’s create the service. 

And you can download full file form Slideshare By clicking in this  Link
............................................................................................................
1. Create a project with your visual studio.



2. Now select Windows Service application as shown below.




3. Now add an installer.



4. After selection “Add Installer”, a file named “ProjectInstaller.cs” will be added in your solutions. And there will be to things like label named “serviceInstaller1” and “serviceProcessInstaller1”. First select “serviceInstaller1” and go to properties. Then change the marked values as shown in image.





5. Now right click on “serviceProcessInstaller1” and go to properties then change the values as shown in image







6. Now Right click on your project and Create Public static class Named “Library.cs”. Like shown in image. And paste the following code inside the class.

using System.IO;
 using System.IO;

public static void WriteErrorLog(Exception ex)
{
  StreamWriter sw = null;
  try
  {
   sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
   sw.WriteLine(DateTime.Now.ToString() + ": " + ex.Source.ToString().Trim() + "; " + ex.Message.ToString().Trim());
   sw.Flush();
   sw.Close();
         
  }
  catch
    {
//nothting
    }
  }

public static void WriteErrorLog(string Message)
{
   StreamWriter sw = null;
try
{
  sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
  sw.WriteLine(DateTime.Now.ToString() + ": " + Message);
  sw.Flush();
  sw.Close();
      }
      catch
      {
//nothing
       }
     }



7. Now Right Click on your project > then select “Open Folder in File Explorer” > bin> Debug> Create a text file named “LogFile.txt”.



8.  Now Expand your Service (Ex. Here is Service1.cs) and double click on Service1.cs and write the code showed in image.



9. Now Save all changes and build your application. And Run Visual Studio Command Prompt as Administrator mode. Change the directory to application’s drive as showed the following image (Ex.:  here our directory is “G”) and hit enter key.



10. Now Right Click on your project > then select “Open Folder in File Explorer” > bin> Debug> and copy the directory from address bar. And then type cd <space> then paste the directory you copied and hit enter key. As like the following image.



11. Right Click on your project > then select “Open Folder in File Explorer” > bin> Debug>
Then copy your service name (Ex. Here our service name is “MyFirstServiceApp.exe”).


Now type “installutil.exe” <space> paste your service name you copied (in this case our service name is “MyFirstServiceApp.exe”) from Debug folder in your project as same as showed in following picture And hit enter key.





12. If everything are ok then you will get this type of result in your cmd and it means your service is ready to work.



13. Now navigate to windows services from start menu and run is as administrator.



14.  Now follow the find your services (i.e.: Here our service Name is “MyFirstService” ) and click on it then click on Start from left side of the service window.



15. Right Click on your project > then select “Open Folder in File Explorer” > bin> Debug> and open the text file Name “LogFile.txt”. You will see the following text has written in your text file. That means your service is working as your expectation. Close the text file.




16. Now once again go to service as described in 13 step. And stop your service as shown in picture bellow.



17. Open your text file named “LogFile.txt” as described in step 15. Now you will see another line has added in your text file which tells your service has stopped as same as following picture.




Download Full File From This Link

                  ====================== (~_~)(~_~)(~_~)======================

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. how about visual c# 2010 only not visual studio, i can't find windows service

    ReplyDelete
    Replies
    1. What do you mean by Visual C# and Visual Studio?
      C# is language and Visual Studio is an IDE which used develop application using c# language.

      Please, clear me about you problem which you faced. I'll try my label best.

      Delete