122

I'm getting this error when I try to start a windows service I've created in C#:

alt text

My Code so far:

private ServiceHost host = null;

public RightAccessHost()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    host = new ServiceHost(typeof(RightAccessWcf));
    host.Open();
}

protected override void OnStop()
{
    if (host != null)
        host.Close();
    host = null;
}

Update #1

I solved the issue above by granting permissions to the account NETWORK SERVICE but now I have an another problem:

alt text

Update #2

Service cannot be started. System.InvalidOperationException: Service 'RightAccessManagementWcf.RightAccessWcf' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element. at System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreNonMexEndpoints(ServiceDescription description) at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) at System.ServiceModel.ServiceHostBase.InitializeRuntime() at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at RightAccessHosting.RightAccessHost.OnStart(String[] args) in C:\Users....

M463
  • 2,003
  • 3
  • 23
  • 39
TheBoubou
  • 19,487
  • 54
  • 148
  • 236

32 Answers32

129

I realize this post is old, but there's no marked solution and I just wanted to throw in how I resolved this.

The first Error 5: Access Denied error was resolved by giving permissions to the output directory to the NETWORK SERVICE account.

The second Started and then stopped error seems to be a generic message when something faulted the service. Check the Event Viewer (specifically the 'Windows Logs > Application') for the real error message.

In my case, it was a bad service configuration setting in app.config.

Mangesh
  • 5,491
  • 5
  • 48
  • 71
Justin Skiles
  • 9,373
  • 6
  • 50
  • 61
  • 8
    I modified the permissions by navigating to the folder using Explorer, right clicking, Properties, Security, and then assigning the correct permissions to the NETWORK SERVICE account in the list of user names. – Justin Skiles Dec 05 '11 at 18:23
  • In my case about "Error 5", it was the Network Service has not access rights to the folder of the executable. Since it is for development, I did not want to place the files into folder Program File but a shared folder which I could copy files from the dev machine. Giving Network Service the rights of Read/Execute/List should be good enough. – ZZZ Oct 30 '13 at 04:15
  • 6
    I Add LOCAL SERVICE" and "NETWORK SERVICE" to my bin\Debug folder and works, Thanks! – Hernaldo Gonzalez Nov 03 '15 at 13:39
  • 4
    Just a note to people: if changing permissions does not fix your problem, be sure to check the Event Viewer for errors. I had a completely unrelated SQL error that prevented the service from starting but still gave me the "Error 5: Access is denied." error. – dtryan Nov 04 '16 at 11:48
  • Thanks for the note about "Event viewer" - same with me, unrelated error "Endpoint not found" but masqueraded as "Access Denied (5)" – David Votrubec Feb 23 '17 at 09:38
  • I was unable to add `LOCAL SERVICE` neither `NETWORK SERVICE` to my `bin\Debug` folder because my solution was in the Google Drive File Stream. So I need to move my project to my local disk and everything is fine now. Btw, this is not the first time I got issues with Google Drive File Stream when using Visual Studio = / – Daniel Bonetti Jul 17 '18 at 15:18
32

Computer -> Manage -> Service -> [your service] properties. Then the the tab with the account information. Play with those settings, like run the service with administrator account or so.

That did it for me.

EDIT: What also can be the problem is that, most services are run as LOCAL SERVICE or LOCAL SYSTEM accounts. Now when you run C:/my-admin-dir/service.exe with those accounts but they are not allowed to execute anything in that directory, you will get error 5. So locate the executable of the service, RMB the directory -> Properties -> Security and make sure that the account the service is run with, is in the list of users that are alloewd to have full control over the directory.

Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
24

This worked for me.

  1. Right-click on top-level folder containing the service executable. Go to Properties
  2. Go to "Security" Tab
  3. Click "EDIT"
  4. Click "ADD"
  5. Enter the name "SYSTEM", click OK
  6. Highlight SYSTEM user, and click ALLOW check-box next to "Full control"
  7. Click OK twice
cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • 14
    "SYSTEM" did not work for me, I just gambled and tried "SERVICE" - that did the trick. – Exter Apr 14 '16 at 12:05
  • 7
    Adding "SERVICE" and giving it "Full Control" worked for me - Windows 10. – Fredrik Sep 11 '16 at 09:55
  • Since my ServiceProcessInstaller's Account property (in my ProjectInstaller) was set to LocalService, I granted full permissions to the Local Service account and that did the trick! – Dave Apr 12 '19 at 12:30
19

Make sure the Path to executable points to an actual executable (Right click service -> Properties -> General tab). Via powershell (and sc.exe) you can install a service without pointing it to an actual executable... ahem.

Aage
  • 5,932
  • 2
  • 32
  • 57
  • 1
    I accidentally left there a folder instead of .exe file. To fix this issue I had to "sc delete servicenameXYZ" + restart server to delete the service completely and reinstall correct service .exe file. Then it starts like a charm. Thanx for this post. – Honza P. Nov 15 '18 at 13:41
  • You saved me! Assigning folder, instead of the executable full path... Arghhhh! Should have looked better... – ilter May 06 '20 at 21:01
  • It happens to all of us :) – hector-j-rivas Nov 02 '21 at 14:53
  • Yeah I didn't put the exe in the service installation path. Thanks. – Datboydozy Oct 11 '22 at 23:19
17

I also got the same error , It resolved by Right click on Service > Properties >Log On > log on as : Local System Account.

Asmita Chavan
  • 171
  • 3
  • 7
  • Thanks. I had a problem with tor.exe --service start, and now it works like a charm! – Arman Karimi Dec 22 '18 at 11:27
  • Worked for me. However, is there anyway to specify this programmatically? – Sisir Jun 22 '19 at 07:49
  • Got it. Right Click serviceProcessInstaller -> Properties -> Account and set it to "LocalSystem" instead of the default "User". Install the service and voila. – Sisir Jun 22 '19 at 08:00
17

I was getting this error because I misread the accepted answer from here: Create Windows service from executable.

sc.exe create <new_service_name> binPath= "<path_to_the_service_executable>"

For <path_to_service_executable>, I was using the path of the executable's folder, e.g. C:\Folder.

It needs to be the path of the executable, e.g. C:\Folder\Executable.exe.

11

I got the solution:

1. Go to local service window(where all services found)
2. Just right click on your service name: 
3. click on "properties" 
4. go to "log on" tab
5. select "local system account"
6. click "ok"

now you can try to start the service.

Nur Uddin
  • 1,798
  • 1
  • 28
  • 38
7

In my case following was not checked.

enter image description here

Akshay Anand
  • 434
  • 5
  • 12
  • 3
    For other readers like me: This screenshot is part of service properties window! Open Services window (Run: services.msc) and select Properties from context menu which appear after right-click on the service! – MohaMad Jan 08 '18 at 09:00
  • Not a general solution to this, but might get you a hint – Henry Troup Sep 15 '21 at 16:09
4

if you are a having an access denied error code 5. then probably in your code your service is trying to interact with some files in the system like writing to a log file

open the services properties select log on tab and check option to allow service to interact with the desktop, click allow service to interact with desktop

kudzai zishumba
  • 652
  • 2
  • 7
  • 12
3

For me - the folder from which the service was to run, and the files in it, were encrypted using the Windows "Encrypt" option. Removing that and - voila!

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • This turned out to be the issue for me - the clown sysadmins on the server I was using had twiddled some knob to make all directories have NTFS encryption by default, and so LocalSystem doesn't actually have the encryption key to read them... – KJ Tsanaktsidis Nov 07 '16 at 05:55
3

I had windows service hosted using OWIN and TopShelf. I was not able to start it. Same error - "Access denied 5"

I ended up giving all the perms to my bin/Debug.

The issue was still not resolved.

So I had a look in the event logs and it turned out that the Microsoft.Owin.Host.HttpListener was not included in the class library containing the OWIN start up class.

So, please make sure you check the event log to identify the root cause before beginning to get into perms, etc.

Rashmi Pandit
  • 23,230
  • 17
  • 71
  • 111
  • 2
    Good point - "access denied" error may have nothing to do with permissions; always check the application log in Event Viewer. – mhenry1384 Apr 02 '18 at 21:06
  • hi, i am also checking on this, where is the `.\logs\` path I am logging to this from topshelf but its not there – Transformer Dec 23 '18 at 07:49
3

In my case, I had to add 'Authenticated Users' in the list of 'Group or User Names' in the folder where the executable was installed.

Sankar
  • 51
  • 6
3

One of the causes for this error is insufficient permissions (Authenticated Users) in your local folder. To give permission for 'Authenticated Users' Open the security tab in properties of your folder, Edit and Add 'Authenticated Users' group and Apply changes.

Once this was done I was able to run services even through network service account (before this I was only able to run with Local system account).

3

This error happens when there is a error in your OnStart method. You cannot open a host directly in OnStart method because it will not actually open when it is called, but instead it will wait for the control. So you have to use a thread. This is my example.

public partial class Service1 : ServiceBase
{
    ServiceHost host;
    Thread hostThread;
    public Service1()
    {
        InitializeComponent();
         hostThread= new Thread(new ThreadStart(StartHosting));

    }

    protected override void OnStart(string[] args)
    {
        hostThread.Start();
    }

    protected void StartHosting()
    {
        host = new ServiceHost(typeof(WCFAuth.Service.AuthService));
        host.Open();
    }

    protected override void OnStop()
    {
        if (host != null)
            host.Close();
    }
}
Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
santhosh
  • 41
  • 1
2

Right click on the service in service.msc and select property.

You will see a folder path under Path to executable like C:\Users\Me\Desktop\project\Tor\Tor\tor.exe

Navigate to C:\Users\Me\Desktop\project\Tor and right click on Tor.

Select property, security, edit and then add. In the text field enter LOCAL SERVICE, click ok and then check the box FULL CONTROL

Click on add again then enter NETWORK SERVICE, click ok, check the box FULL CONTROL

Then click ok (at the bottom)

MagTun
  • 5,619
  • 5
  • 63
  • 104
1

I have monitored sppsvc.exe using process monitor and found out that it was trying to write to the HKEY_LOCAL_MACHINE\SYSTEM\WPA key. After giving permissions to NETWORK SERVICE on this key, I was able to start the service and Windows suddenly recognized that it was activated again.

1

Use LocalSystem Account instead of LocalService Account in Service Installer.

You can do this either from doing below change in design view of your service installer:
Properties of Service Process Installer -> Set Account to LocalSystem.

or by doing below change in in designer.cs file of your service installer:

this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
Cubicle.Jockey
  • 3,288
  • 1
  • 19
  • 31
Jay Shah
  • 3,553
  • 1
  • 27
  • 26
1

Your code may be running in the security context of a user that is not allowed to start a service.

Since you are using WCF, I am guessing that you are in the context of NETWORK SERVICE.

see: http://support.microsoft.com/kb/256299

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
0

For the error 5, i did the opposite to the solution above. "The first Error 5: Access Denied error was resolved by giving permissions to the output directory to the NETWORK SERVICE account."

I changed mine to local account, instead of network service account, and because i was logged in as administrator it worked

Chris
  • 1
0

If you are getting this error on a server machine try give access to the folder you got the real windows service exe. You should go to the security tab and select the Local Service as user and should give full access. You should do the same for the exe too.

Darshana
  • 564
  • 1
  • 4
  • 13
0

I accidentally set my service to run as Local service solution was to switch to Local System

meda
  • 45,103
  • 14
  • 92
  • 122
0

After banging my had against my desk for a few hours trying to figure this out, somehow my "Main" method got emptied of it's code!

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
{ 
    new DMTestService()
};
ServiceBase.Run(ServicesToRun);

Other solutions I found:

  • Updating the .NET framework to 4.0
  • Making sure the service name inside the InitializeComponent() matches the installer service name property

    private void InitializeComponent()
    ...
    this.ServiceName = "DMTestService";
    
  • And a nice server restart doesn't hurt

Szhlopp

Szhlopp
  • 21
  • 1
0

In may case system run out of free space on local disk.

Alexander Puchkov
  • 5,913
  • 4
  • 34
  • 48
0

I had this issue today on a service that I was developing, and none of the other suggestions on this question worked. In my case, I had a missing .dll dependency in the folder where the service ran from.

When I added the dependencies, the issue went away.

Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
0

In my case I kept the project on desktop and to access the desktop we need to add permission to the folder so I simply moved my project folder to C:\ directory now its working like a charm.

smali
  • 4,687
  • 7
  • 38
  • 60
0

Have a look at Process Utilities > Process monitor from http://www.sysinternals.com.

This is tool that allows you monitor what a process does. If you monitor this service process, you should see an access denied somewhere, and on what resource the access denied is given.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
0

I don't know if my answer would make sense to many, but I too faced the same issue and the solution was outrageously simple. All I had to do was to open the program which I used to run the code as an administrator. (right-click --> Run as Administrator).

That was all.

Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
0

check windows event log for detailed error message. I resolved the same after checking event log.

Amrik Singh
  • 503
  • 5
  • 4
0

All other answers talk about permissions issues - which make sense, given that's what the error message refers to.

However, in my case, it was caused by a simple exception in my service code (System.IndexOutOfRangeException, but it could be anything).

Hence, when this error occurs, one should look inside their log and look for exceptions.

OfirD
  • 9,442
  • 5
  • 47
  • 90
-1

I had this issue on a service that I was deploying, and none of the other suggestions on this question worked. In my case, it was because my .config (xml) wasn't valid. I made a copy and paste error when copying from qualif to prod.

SabineA
  • 73
  • 1
  • 1
  • 5
-1

As the error popup suggests this is related to permission. So run the service as "LocalSystem" account.

To do the same, Right Click on serviceProcessInstaller -> Properties -> Account and set it to "LocalSystem" instead of the default "User". Install the service and voila.

Sisir
  • 4,584
  • 4
  • 26
  • 37
  • This is a proven working fix and not any work around. Can the down-voter please explain the reason of dislike for others to understand – Sisir Sep 09 '19 at 09:10
  • No, this is not a fix. Implicitly granting total system control to a service as a way to get around "permission denied" is a workaround. A *fix* would be to identify what specific permission it needs and assign that to an otherwise limited account. Very few services really need total system control. Blindly giving it that, without actual understand, harms the overall system security. – Oskar Berggren May 11 '22 at 13:20
-2

Click the Start menu and choose Run or use the keyboard shortcut of Win+R.

In the dialog box, type lusrmgr.msc. When this application opens, click Users in the left-hand panel and then right click Administrator in the right-hand panel. Click Properties in the menu.

In the Administrator Properties dialog, choose the Member Of tab, then click the Add... button at the lower right. From the next dialog, choose Advanced...

Another dialog will appear. From there click Find Now on the right. A list of search results will appear at the bottom of the dialog. Select Network Services from this list and click OK on each of the open dialogs.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
milan
  • 1
  • 1
    Welcome to Stack Overflow! Additional explanation would improve your answer. Additionally, any better formatting would make these actions much more clearly followed. – ryanyuyu Jun 04 '15 at 14:01