I have developed a WCF service which uses the net.tcp binding and read and write files to the file system. When I start the windows service (in a console) I get following error even if the sample.dll is in the folder:
Unable to find assembly file sample.dll
This specific WCF service is hosted in a windows service which runs on windows 7 64bit and I have following questions:
Which account should I use for my application:
User / NetworkService / LocalSystem / LocalService
And why can’t the OS load the dll even if it is there?
Thank you in advance for your help!
For the dll you should add the following line of code right at the beginning of the OnStart method.
System.Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory
That should allow your service to load dlls from the actual directory.
According to the MSDN documentation on the ServiceAccount, the User member prompts for a username and password. I'd guess that if you're using the User account and it doesn't have read permissions for windows/system32, that would result in your assembly failing to be found.
Perhaps change the ServiceAccount up a step to a LocalService?
Related
I'm creating ASP.NET Web Application. I need in it to extract some '7z' files.
My code:
SevenZipExtractor.SetLibraryPath(#"<some_path>\7z.dll");
var zipExtractor = new SevenZipExtractor(zipPath);
zipExtractor.ExtractArchive(path);
SetLibraryPath, according to SevenZipSharp documentation, should resolve the problem, but it doesn't.
During my tests in console app everything works fine but when I'm trying to run my web Application I get "Can not load 7-zip library or internal COM error! Message: failed to load library.". In console app it's easier because dll files can be placed in Debug folder and it works. Web Application is handled by IIS Express which I guess is the problem.
It turned out that 7z.dll needs to be in temporary folder created by IIS. It can be accessed using via Assembly:
var assemblyDllPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "7z.dll");
Using that we can copy original 7z.dll to the assembly directory and set SevenZipSharp library path like that:
File.Copy(originalDllPath, assemblyDllPath, overwrite: true);
SevenZipBase.SetLibraryPath(assemblyDllPath);
Note that overwrite is set to true. It is because sometimes assembly happens to be the same that one before.
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
I install a windows service using installutil and the project installer Account property is set to User.
I install the service as the user who is currently logged on (domain/username).
The service does some stuff and creates a text file. I cannot see the text file in windows explorer but I know it is there based on the output of the service.
If I am logged in as the user who is running the service why can I not see the files the service creates?
Thanks,
Andrew
In my experience the current working directory for services defaults to the system folder instead of the folder where the entry assembly is located. Check for the file here. If you want to change the working directory you can do so in code as follows.
Assembly assembly = Assembly.GetEntryAssembly();
string assemblyFolder = Path.GetDirectoryName(assembly.Location);
Environment.CurrentDirectory = assemblyFolder;
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.
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.