[c#] Windows service start failure: Cannot start service from the command line or debugger

hi i'm getting this error

Cannot start service from the command line or debugger. A winwows Service must first be installed(using installutil.exe) and then started with the ServerExplorer, Windows Services Afministrative tool or the NET START command.

and i dont understand why im geting this error. And here is my code:

{
    string Hash = "";
    string connectionstring = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    SqlConnection myConnection = new SqlConnection(connectionstring);
    SqlCommand myCommand = new SqlCommand("GetNullHash", myConnection);
    myCommand.CommandType = CommandType.StoredProcedure;
    myConnection.Open();
    SqlDataReader rdr = myCommand.ExecuteReader();

    while (rdr.Read())
    {
        string filename = @"\\" + rdr.GetString(3);
        filename = System.IO.Path.Combine(filename, rdr.GetString(2));
        filename = System.IO.Path.Combine(filename, rdr.GetString(1));
        Hash = rdr.GetString(0);
        Hash = computeHash(filename);

    }
    myConnection.Close();
    return Hash;
}

This question is related to c# windows-services

The answer is


Your code has nothing to do with the service installation, it is not the problem.

In order to test the service, you must install it as indicated.

For more information about installing your service : Installing and Uninstalling Services


To install Open CMD and type in {YourServiceName} -i once its installed type in NET START {YourserviceName} to start your service

to uninstall

To uninstall Open CMD and type in NET STOP {YourserviceName} once stopped type in {YourServiceName} -u and it should be uninstalled


To install your service manually

To install or uninstall windows service manually (which was created using .NET Framework) use utility InstallUtil.exe. This tool can be found in the following path (use appropriate framework version number).

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe

To install

installutil yourproject.exe

To uninstall

installutil /u yourproject.exe

See: How to: Install and Uninstall Services (Microsoft)

Install service programmatically

To install service programmatically using C# see the following class ServiceInstaller (c-sharpcorner).


Goto App.config

Find

<setting name="RunAsWindowsService" serializeAs="String">
    <value>True</value>
  </setting>

Set to False


I will suggest creating a setup project for the reasons while deploying this seems the best convinience , no headaches of copying files manually. Follow the Windows service setup creation tutorial and you know how to create it. And this instance is for vb.net but it is the same for any type.