views:

1700

answers:

7

I have a windows service set to copy files from a local directory to a samba share.

The service connects via a UNC path to the server (i.e. \remoteserver\shareddir).

I have logged in as the user under which the service is running, and was able to both copy files and create files on the samba share, using the UNC path.

However, when running the service I am getting io exceptions that suggest a login error. I have run process monitor on that box, watching the service and I can see it finding the UNC path but getting a login error when trying to write to the share.

This is using plain vanilla .net System.IO.File.Copy call.

I don't understand the errors, as when logged in under the same user I am able to copy and create files on the share.

Any ideas?

Edit: Exception text

Error in main processing loop (DoWork()): Logon failure: unknown user name or bad password.

System.Exception: Error on MyService.ProcessExportTransferfileDirectories and values: D:\Export, \RemoteServer\intray, D:\Export\Archive ---> System.IO.IOException: Logon failure: unknown user name or bad password.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.__Error.WinIOError() at System.IO.File.Move(String sourceFileName, String destFileName)
at MyService.Consumer.BusinessLogic.WmsFacade.MoveAndArchiveFile(String sourceFolder, String targetFolder, String archiveFolder, String rootFolder, LogCategory logCategory, LogCounter logCounter) --- End of inner exception stack trace --- at MyService.Consumer.BusinessLogic.WmsFacade.MoveAndArchiveFile(String sourceFolder, String targetFolder, String archiveFolder, String rootFolder, LogCategory logCategory, LogCounter logCounter) at MyService.Consumer.BusinessLogic.WmsFacade.ProcessExportTransfer(IDictionary`2 fileDirectories) at MyService.Core.TimedWorkerProvider.DoWork()

A: 

What context is the service running as? The account the service is running as probably doesn't have access to that share.

David McEwing
As mentioned, when I log in with the same account that the service is running as, I have no problems copying to and creating files on the share.
Oded
A: 

A very helpful tool in your situation is process monitor. Just run it as administrator and add the following filter:

Process Name is "your_process.exe" AND Operation is "CreateFile"

You will see detailed information about the files your process is trying to use.

Darin Dimitrov
Thanks - I have indeed been running it, with the same results.
Oded
+1  A: 

I would recommend mapping a drive first and then doing the file copy to the mapped drive instead of the share.

You can map a drive using the dos net use command. For example:

System.Diagnostics.Process.Start("net", "use h: \remoteserver\shareddir /User:domian\me password")

This should resolve any login issues.

Chris Franz
Can't use a mapped drive, as the service does not support that usage.
Oded
A: 

As mentioned, when I log in with the same account that the service is running as, I have no problems copying to and creating files on the share. – Oded 1 min ago

Did you grant the user account the service is running as "Logon as Service" permissions in the local security policy??

Serapth
I have other services setup the same way (which do other work) - they all start and run without a problem, so I didn't look at this permission. Will check.
Oded
Checked and yes, the user account does have those permissions.
Oded
A: 

Anything in the eventlog?

JensG

Just the exception above
Oded
A: 

It ended up being a permissions issue - after running the service as a different user that was setup on the samba share it ran without a problem, copying files across.

Still don't understand why when using a different account I could copy/create files on the share using windows explorer but when the service was running under the same account it couldn't access the share at all :(

Oded
A: 

Hi, I have the same problem; I cannot access the files in UNIX from windows service while I can access it from normal windows application Actually, I do not have experience in UNIX, could you advice how you solved the problem

Thanks

Weal - you should read the answers to this question, or write a new question with the details. As you can see, the problem in my case was that the service was installed with incorrect credentials.
Oded