views:

788

answers:

3

hey guys i want to write a small c# ftp client class library which basically needs to transfer files to a ftp location

what i want is a 100% foolproof code where i can get some sort of acknowledgement that the ftp file transfer has been either 100% successful or failed

no resume support is required

good to have (but secondary):

some sort of distributed transaction where only if the file transfer is successful for a file, i update my db for that particular file with 1 (true)... if its failed then db to be updated with 0 (false)

but suppose the ftp file transfer was successful, but for whatever reasons the db could not be updated, then the file over ftp should be deleted - i can easily do this using dirty c# code (where i manually try to delete the file if db update failed)

but what i am exactly looking for is file system based transaction over ftp... so both the file transfer as well as db update is not committed until both are successful (hence no need for manual delete)

any clues?

A: 

try using Ftp with WCF

Tinku
can u please give a more comprehensive answer? just fyi: i can only host things at my end - i do not have any liberty to do anything on remote server (ftp server) - that belongs to my client
Raj
A: 

For the transaction part, you can use TransactionScope. Import System.Transactions as a project reference, then you can do something like this (which will result in all the DB records being committed, or none of them):

try {
    using (TransactionScope scope = new TransactionScope)
    {
        loop over the files to FTP
        {
            // do ftp

            // update DB
        }

        // inform transaction manager that the transaction can be committed
        scope.Complete();
    }
}
catch (Exception e)
{
    // no DB records will be saved if we get here, handle exception accordingly
}

For the FTP part, google is your friend. Here's one such example (first google hit): http://aspalliance.com/1187%5FBuilding%5Fa%5FSimple%5FFTP%5FApplication%5FUsing%5FC%5F20.all

dcp
I don't believe that ftp recognizes the transaction scope. Do you have any documentation to this?
Stefan Steinegger
@dave - are you sure ftp supports transaction scope? if yes can you pass me some links?
Raj
Sorry, I didn't mean the transaction scope would work for the ftp, I meant for writing out the DB records. The transaction scope will ensure that all of the DB records are written, or none of them. For the ftp, I don't know of a way to do it. Sorry for the confusion.
dcp
+5  A: 

Having had the "joy" of writing a FTP library myself here is my advice

1) Its NOT gonna be easy, because FTP servers return different return from the same command ( Like directory information,regular ftp commands and pretty much everything).
2) This is gonna take more time then you think
3) The dream about 100% foolproof transfer is not gonna happen, unless you control the FTP server and add a new FTP command so you can compare file hashes.


Pretty much if i was gonna do this again, and my goal was to transfer files ( And not learn from making the library) i would buy a already done library,

EKS