title: Access Denied when running Windows Service
body: <p>I have created a Windows Service using ASP.Net Core 3.x and C#.  I started with the new Windows Service template when I built the project.  When I run it from my development environment or from a console window  it runs fine.  When I install it as a Windows Service and attempt to start the service I get an 
  "Error 5: Access is denied." error.</p>
  
  <p>I tried numerous things which I will outline below to eliminate the error but nothing seemed to work so I downloaded the sample app provided by Microsoft, at <a href="https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/host-and-deploy/windows-service/samples/3.x/BackgroundWorkerServiceSample" rel="nofollow noreferrer">sample</a></p>
  
  <p>Same result...when I run the sample app from within Visual Studio it runs fine, when running as a service I get the Access Denied error.</p>
  
  <ul>
  <li><p>I am running all of this on my local machine, which I am an admin on.</p></li>
  <li><p>I originally tried to run it using the default Local System account; got the Access Denied error.
  I changed the Log On As to my domain account, the same one I use to log into my local machine which is an admin on this machine; got the same Access Denied error.  </p></li>
  <li><p>My account has the privilege set to run as a service.</p></li>
  <li><p>The Event Viewer just shows the one message which says "Access Denied", no other messages are created.</p></li>
  <li><p>I believe the Access Denied error is occurring before the C# code is even executed.  What makes me believe this is that I added one line to the very top of the Program.Main.... <code>File.WriteAllText("C:\\temp\\ws.log", $"Test of Worker Service @ {DateTime.Now}. Content Root Path: {AppContext.BaseDirectory}");</code>.  My account has full access to the temp folder.  This file gets created when I run the app from Visual Studio but it does not get created when I run the app as a service.</p></li>
  <li><p>I have read numerous web sites, include this <a href="https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-3.1&amp;tabs=visual-studio" rel="nofollow noreferrer">one</a> and this <a href="https://csharp.christiannagel.com/2019/10/15/windowsservice/" rel="nofollow noreferrer">one</a>.  No luck, everything I tried from these sites still produce the Access Denied error.</p></li>
  </ul>
  
  <p>I have run out of ideas and am hoping someone here can provide me the answer.  Thanks!</p>