91

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;
}
paxcow
  • 1,670
  • 3
  • 17
  • 32

6 Answers6

131

Watch this video, I had the same question. He shows you how to debug the service as well.

Here are his instructions using the basic C# Windows Service template in Visual Studio 2010/2012.

You add this to the Service1.cs file:

public void onDebug()
{
    OnStart(null);
}

You change your Main() to call your service this way if you are in the DEBUG Active Solution Configuration.

static void Main()
{
    #if DEBUG
    //While debugging this section is used.
    Service1 myService = new Service1();
    myService.onDebug();
    System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

    #else
    //In Release this section is used. This is the "normal" way.
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
    { 
        new Service1() 
    };
    ServiceBase.Run(ServicesToRun);
    #endif
}

Keep in mind that while this is an awesome way to debug your service. It doesn't call OnStop() unless you explicitly call it similar to the way we called OnStart(null) in the onDebug() function.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
Cesar
  • 2,229
  • 1
  • 22
  • 20
  • Someone also offered me a "duh" tip that said: Develop your class like you would in a normal debuggable application then import it to your service when you feel the class is ready. – Cesar Jul 05 '13 at 05:33
  • What I do was put the functions that I need to debug in the OnStart method after Timer.Start();, so with that I can debug. – JD - DC TECH Apr 22 '15 at 23:33
39

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

Woodrow Barlow
  • 8,477
  • 3
  • 48
  • 86
Ria
  • 10,237
  • 3
  • 33
  • 60
  • This worked perfectly, I had a problematic server and need the service to run urgently, I used this step to configure the service on my local machine, please make sure to configure it with right user with required permissions. – kolexinfos Sep 02 '16 at 13:24
  • 7
    After running `installutil` it says it was successful, but I still can't start the service or see it in the service manager – Bassie May 01 '17 at 00:03
  • 2
    i swear, every MSDN link i've ever found in the wild is broken. ms docs are the poster child for why it's important to include the relevant info in your answer. (and you've done a great job at that! thank you!) – Woodrow Barlow Nov 12 '20 at 13:34
1

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

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
0

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.

bhuvin
  • 1,382
  • 1
  • 11
  • 28
0

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

JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
0

Goto App.config

Find

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

Set to False

TuanDPH
  • 461
  • 5
  • 14
  • Is there any online documentation for this setting? Where would I add it? I tried applicationSettings and it has no effect. – bornfromanegg Oct 19 '17 at 12:33