Run Windows service by installutil and facing permission issue c# - c#

i developed a small windows service where sql dependency classes to monitor table changes. if table change occur then service call a web service. this service create a folder and file to save the log and also service send mail to user.
i try to copy service exe file in a folder of program files folder and issue this command installutil C:\BBA-Reman\PartIndexer\MyService.exe
getting error related message and i understood that permission is getting a issue. so i open the command prompt as run as admin and then i was my service is running fine and also could create folder & file for saving log data.
so what i did now. i add a Manifest.xml file in my project and make a association that file with my project. Manifest.xml file content here.
this area i change in manifest file for running my service with admin privilege
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
i again compile my service and just open command prompt without run as admin and issue the same command installutil C:\BBA-Reman\PartIndexer\PartIndexerService.exe getting the same permission issue related error message as before.
so please guide me how could i run my service with admin privilege by manifest file. what i am doing wrong for which i am getting error even after adding manifest file. looking for suggestion. thanks

I propose to use Topshelf in your C# application, it eases the process of windows services installation.
Here you can find more details about it:
http://docs.topshelf-project.com/en/latest/overview/commandline.html

Related

Windows service running as local service - input files?

I have a windows service program running in C#. I have configured to run it as a local service with my config files stored in \bin\release\config\configvalues.txt. But, it does not recognize this file path and throws the "System.UnauthorizedAccessException".
I believe its looking for the files in the System32 folder and since it does not have the privilege, throws out the exception. For the workaround, my service is running with "local system" to recognize the System32\config folder. To run as a local service, which file path should the config folder be available?
Check the location of your project folder. Most likely, you'll find that it is inside your User profile folders, which the local service account does not have access to.
Deploy your project to a folder outside of any User profiles and you'll have better luck.

How come my Windows Service cannot access a folder with files in them?

I have setup a Console, Library, and Service project. The Library project loads up files in a folder and stores the text in variables.
The console project will then manipulate those variables. I am able to run the Console project successfully and it loads the files without a problem. The files are set to Copy Always in the DLL and are specified as Content for the Build Action.
When I try to run the exact same code in the service.
File.ReadAllText(#"MyFolder\SomeFile.txt");
It throws the following exception.
The type initializer for 'MyLibrary.Data.ReadFiles' threw an exception.
I am using the Setup Project type to create the installer. After the service installs the folder MyFolder does exist in the Application Folder of the service, as well as the files. What else could be causing my problem for the service not being able to read those files?
EDIT 1
public class ReadFiles {
public static string DataFile = File.ReadAllText(#"MyFolder\SomeFile.txt");
}
The service account that you're running the Windows service under, doesn't have rights to read from the location you're trying to access. The error you're seeing has to do with the fact that the code you showed likely exists in the construction of the type ReadFiles.
Was facing similar issue in one of the windows service for running node js application, for sending emails.
It was running fine when we run from the same directory, but running same application as windows service was having issues and couldn't send emails. Comment from #meanbunny did help, so adding it as an answer.
Windows service can't access the directory/files if they are mentioned in the code as relative path, better use the absolute path or be more flexible as #Mike mentioned in his comment to use app.config for each service.

Visual Studio Deployment Package Change File Permissions

Maybe this isn't possible due to Security implications...
I have a Deployment Package created in Visual Studio for a Win Form. The application references an app.config file locally and allows the user to change the settings in it, through the application.
When I deploy the application and attempt to save the changes I have made, Windows is saying "Permission denied". Is there a command I can run as part of the installation that will give the "Everyone" security permission to my app.config file....and thus allow my Application to edit it?
Thank you.
If you want to be able to write to the app.config file you need to move it to the Shared Application Data directory and modify your application to read the configuration information from there. On Win7 for example:
C:\ProgramData\YourCompany\YourApp\app.config
In C#, to get the common app data directory:
// using System.IO;
string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
You might be able to use a custom action in your installer to run icacls in order give full access to Everyone:
icacls {Path to App.config} /grant Everyone:(F)

Issue in Creating ZIP files using WCF Service from a anonymous user

I have a WCF service which will will be called from mobile devices and that service will zip some folders in the machine and will send it back to the caller.I use dotnet zip for zipping the folders in my WCF service.
I am getting the following error trace from the open source dotnet utility , that i have used in my project.
"Request for the permission of Type 'System.security.Permissions.FileIOpermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublickeyToken=dfsdfsdfsdfds' Failed"
I am using the dotnet zip utility to zip my folders and files avaiable in my machine.
I tired one option, by cleaning the solution and building it with Administratice privilege it started working but after sometime the same problem comes again.
Please help me in resolving this.
I don't know which WCF hosting option is your choice, but if this is Internet Information Services, it seems that these are some reasons of having this exception:
1) IIS worker process doesn't have permissions over the folders to zip or the temporary folder where you place the Zip file that the service sends.
2) Web configuration has a very low trust in service code. Check that this is in medium or full-trust mode.
3) The way you're opening the file is wrong. Check how you open the file stream and see if this is doing with the necessary file mode (OpenRead or like this).
I don't think this is a compilation problem. It's a run-time issue.

Event Log SecurityException for Web Application?

I have an app that writes messages to the event log. The source I'm passing in to EventLog.WriteEntry does not exist, so the Framework tries to create the source by adding it to the registry. It works fine if the user is an Admin by I get the following whe the user is not an admin:
"System.Security.SecurityException : Requested registry access is not allowed." message.
How can I fix that?
Update
I have create the registry with the Admin account manually in the registry. Now, I have the error : System.Security.SecurityException: Requested registry access is not allowed.
I do not understand because I have create a user in the Group Administrator... what do I have to do more?
For your update I have found something that might help you :
Run regedt32
Navigate to the following key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Security
Right click on this entry and select Permissions
Add the ASPNET user
Give it Read permission
2. Change settings in machine.config file
Run Explorer
Navigate to WINDOWS or WINNT folder
Open Microsoft.NET folder
Open Framework folder
Open v1.1.4322 folder (folder name may be different, depending on what dotnet version is installed)
Open CONFIG folder
Open machine.config file using notepad (make a backup of this file first)
Locate processmodel tag (approx. at line 441)
Locate userName="machine" (approx. at line 452)
Change it to userName="SYSTEM"
Save and close the file
Close Explorer
3. Restart IIS
Run IISReset
The source is here
The "non-programming way" is to grant the user that user your web application/web service with access to registry (Event Log are written in the event log).
Nothing of these worked out for me. What did the trick was alter the Application Pool Identity from "ApplicationPoolIdentity" to "LocalSystem". Then put LocalSystem Windows account into the Administrators group.

Categories

Resources