FTP loading in Windows Service

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I will try to explain the problem.
I'm doing an asynchronous copy file from local station to my FTP according
to the MSDN example:
http://msdn2.microsoft.com/en-us/library/system.net.ftpwebrequest(VS.80).aspx

see the 3-rd example ("The following code example demonstrates using
asynchronous operations to upload a file to an FTP server.")

In the main part of my service I use System.Threading.Timer.

After files copied they should be moved from one folder to another.

But the system crashes after files copied to FTP when I try any operation
with file which is already sent to ftp.
So, it's blocked, locked or whatever.

I close all streams and tried stream.Lock/Unlock.

But still the problem exists.

When I place the same code, with the same Threading Timer inside non-service
project, just WinForm, it works fine, but under the Service doesn't.

Could you give me any clue, please?
 
Robert,

Well, you didn't show any code, and you didn't show any details of the
exception. Could you provide some of those? It would help.
 
Nicolas,
The exception is:
UnauthorizedAccessException

Timer in the main service body is from System.Threading library.

The example link I mentioned is the code.

Inside timer callback after files transfered I just move files from Folder1
to Folder2.

The FTP transfer goes fast, actually there are just 2 files only for now.

I can see them inside remote folder... but Ftp request blocks them only if I
use this code inside service.

As I mentioned before if placed in standalone aplication - works fine.
 
Peter,

Here is the code snap where the exception is:

public void GlobalTick_Tick(Object stateInfo)

{
Action act = new Action(); <--FTP transfer here Ok
string from = "FolderFrom&File"; <= One of the transfered
files (LocalPC)
string where = "FolderTo&File";<= (local PC)
if (File.Exists(from) && !File.Exists(where))
{
Logger.Record("Moved from: " + from + " To: " + where);
try
{
File.SetAttributes(from, FileAttributes.Normal);<--
here's the Exception
File.Copy(from, where);<-- here's the Exception
File.Delete(from);<-- here's the Exception

}
catch (UnauthorizedAccessException ex)
{
throw ex;
}
}
}

Files are blocked for FTP request, though I close request stream and file
stream, and even I lock and unlock the file stream during FTP transfer, and
Timer doesn't come to that point yet.....as the transfer itself takes only 2
seconds and timer set up for every 10 seconds.

How to set up the credentials for remote? I use the right username and
password to login the FTP server folder, and I see how files are moved there.

Anything I lost? Please, clearify the idea.

Thanks
 
As previously stated, you need to be aware of the UserAccount under which
the service runs.

PS

Here is a very very crappy "Who Am I" script (see below).

I'd use it for debugging, but not production code.

You can also consider the EMAB or EnterpriseLibrary Exception block, which
wraps up alot of this info for you automatically.



private string FindIIdentity()

{


try {

string returnValue = string.Empty;



WindowsIdentity ident = WindowsIdentity.GetCurrent();

returnValue = ident.Name;


try {

returnValue += " on " + System.Environment.MachineName;

}

catch (Exception ex) {


}


return returnValue;

}



catch (Exception ex) {


return "Error Finding Identity";

}


}
 
Back
Top