3

I have some c# code that is doing some file uploads to my apache server via HttpWebRequests. While the upload is in progress, I am able to use ls -la to see the growing file size.

Now, if I for example pull my computers network cable, the partial file upload remains on the server.

However, if I simply close my c# app, the partial file is deleted!

I assume this is being caused by my streams being closed gracefully. How can I prevent this behavior? I want my partial file uploads to remain regardless of how the uploading app behaves.

I have attempted to use a destructor to abort my request stream, as well as call System.Environment.Exit(1), neither of which had any effect.

Julien
  • 212
  • 1
  • 18
  • 53
  • 2
    This would depend on the server-side code. – SLaks Mar 16 '12 at 20:24
  • There is no server side code, it's just an HTTP Put – Julien Mar 16 '12 at 20:24
  • 2
    In other words, the server-side code is in Apache itself. – SLaks Mar 16 '12 at 20:41
  • Can you specify? I am unable to find any setting that would seem to control how partially uploaded files are dealt with, and since I can force the behavior I want there must be a solution. – Julien Mar 16 '12 at 20:44
  • I don't know; I know nothing about Apache. – SLaks Mar 16 '12 at 21:02
  • Not sure if this could help: http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data – Strillo Mar 23 '12 at 18:20
  • Have you tried analyzing what's going on with WireShark/Ethereal or an equivalent? You might get some pointers to how the resulting network trafic differs between the two scenarios. – Kenned Mar 26 '12 at 08:53

6 Answers6

3

Pulling the network cable will never be equivalent to aborting the stream or closing the socket, as it is a failure in a lower OSI level.

Whenever the application is closed, the networking session is aborted and any pending operation cancelled. I don't think there's any workaround, unless you programmatically split the file transfer in smaller chunks and save them as you go along (this way you'd have a manual incremental transfer, but it requires some code server-side).

Strillo
  • 2,952
  • 13
  • 15
  • I have an iOS app which is able to abort uploads in progress. The file remains on the server. Only my c# app behaves like this. I have noticed that the file is not deleted until AFTER my destructor is complete. – Julien Mar 23 '12 at 17:47
  • This is the library I have been using: https://github.com/kvdb/WebDAVClient/blob/master/WebDAVClient/WebDAVClient.cs – Julien Mar 23 '12 at 18:38
  • Also I tried the IT Hit demo (pay-for webdav library) just now and an aborted upload does not delete the file! Clearly I am missing a key piece somewhere. – Julien Mar 23 '12 at 18:56
1

Write a very simple HTTP proxy that keeps accepting connections but never closes a connection to your server

Even simpler, using netcat 1.10 (though this will accept just one connection)

nc -q $FOREVER -l -p 12345 -c 'nc $YOUR_SERVER 80'

Then connect your C# client to localhost:12345

Hans Lub
  • 5,513
  • 1
  • 23
  • 43
0

This might be a silly suggestion but what if you call Process.GetCurrentProcess().Kill(); while the application is being closed?

Mataniko
  • 2,212
  • 16
  • 18
  • No effect. The file is not deleted from the server until after I step out of my app's destructor, or in this case the Kill command. – Julien Mar 26 '12 at 15:13
0

Before looking at processing of partial uploads, start by testing whether turning keepalives on in Apache configuration solves your problem of receiving partial uploads.

This may have the effect of seeing fewer disconnects and thus less need to process their partial data. Such disconnects may be due to the client, the server, but often they are due to an intermediate node such as a firewall. The keepalives option has the effect of maintaining steady "dummy" traffic (0 byte long data payload), thus advertising to all parties that the connection is still alive.

For a large site with heavy concurrent load, keepalives are a bad thing which is why they are off by default. The option makes connection management for Apache much more complicated, preventing optimized connection reuse, and there is also a little bit of extra network traffic. But maybe you have a specialized use case where this is not a concern.

Keepalives will never help you at all if your clients simply tend to crash too soon (that is, if you see steady progress on the uploads at all times). They may help you considerably if the issue is network related.

They will help you tremendously if your clients generate the data gradually, with long delays in between uploaded chunks.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
  • Thank you for your input. I have an iPhone app, as well as as the IT Hit trial (http://www.webdavsystem.com/client) performing exactly as I need. That is, one can force close the iphone app (being run in a simulator), or force close IT Hit's c# app (via ctrl-alt-del, or simply the X), and the partial file being uploaded remains on the server. This demonstrates that what I need is possible. My own custom c# app does not behave this way. Any partial uploads are immediately deleted from the server when it is closed. I am confident Apache is not the issue. – Julien Mar 26 '12 at 22:01
0

Have you checked, if your application steps into

void FinishUpload(IAsyncResult result) {…}

(line 240) when aborting/killing the app? If so, you may consider to not enter the callback. This is a bit dirty but may give you a location to start digging.

fragmentedreality
  • 1,287
  • 9
  • 31
0

Does Apache support the SendChunked property of HTTPRequest?

If so it is worth trying out.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.sendchunked.aspx

AnthonyBlake
  • 2,334
  • 1
  • 25
  • 39